File: watchdog.cpp

package info (click to toggle)
libtunepimp 0.5.3-7
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,668 kB
  • ctags: 3,470
  • sloc: ansic: 15,652; cpp: 12,752; sh: 11,929; perl: 1,179; python: 720; makefile: 310; pascal: 33
file content (149 lines) | stat: -rw-r--r-- 3,565 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*----------------------------------------------------------------------------

   libtunepimp -- The MusicBrainz tagging library.
                  Let a thousand taggers bloom!

   Copyright (C) Robert Kaye 2003

   This file is part of libtunepimp.

   libtunepimp is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   libtunepimp is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with libtunepimp; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   $Id: watchdog.cpp 1279 2004-04-21 23:57:57Z robert $

----------------------------------------------------------------------------*/
#ifdef WIN32
#	if _MSC_VER == 1200
#		pragma warning(disable:4786)
#	endif
#else
#	include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <time.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <errno.h>
#include <string.h>

#include "watchdog.h"
#include "tunepimp.h"

#define DB printf("%s:%d\n", __FILE__, __LINE__);

#ifndef WIN32
extern "C"
{
    void handler(int sig)
    {
        signal(sig, handler);
        pthread_exit(NULL);
    }
}
#endif

//---------------------------------------------------------------------------

WatchdogThread::WatchdogThread(TunePimp *tunePimpArg) : Thread()
{
#ifndef WIN32
    signal(SIGSEGV, handler);
    signal(SIGBUS, handler);
    signal(SIGFPE, handler);
#endif

    tunePimp = tunePimpArg;
    analyzerThread = NULL;
    analyzerFile = -1;
    exitThread = false;
    sem = new Semaphore();
}

//---------------------------------------------------------------------------

WatchdogThread::~WatchdogThread(void)
{
    if (!exitThread)
    {
        exitThread = true;
        sem->signal();
        join();
    }
    delete sem;
}

//---------------------------------------------------------------------------

void WatchdogThread::stop(void)
{
    exitThread = true;
    sem->signal();
    join();
}

//---------------------------------------------------------------------------

void WatchdogThread::setAnalyzerThread(void *threadId)
{
    analyzerThread = threadId;
}

//---------------------------------------------------------------------------

void WatchdogThread::setAnalyzerTask(int fileId)
{
    mutex.acquire();
    analyzerFile = fileId;
    mutex.release();
}

//---------------------------------------------------------------------------

void WatchdogThread::threadMain(void)
{
    void *thread;
    int   file;

    for(; !exitThread;)
    {
        // Wait for 1/10th of a second.
        if (!sem->timedWait(100))
        {
            mutex.acquire();
            thread = analyzerThread;
            file = analyzerFile;
            mutex.release();

            if (thread == NULL)
                continue;

            if (!isThreadAlive(thread))
            {
                printf("Analyzer thread died!!!\n");
                mutex.acquire();
                analyzerThread = NULL;
                analyzerFile = -1;
                mutex.release();

                tunePimp->analyzerDied(file);
            }
        }
    }
}