vdr/thread.c

201 lines
4.0 KiB
C
Raw Normal View History

2000-10-08 09:25:20 +02:00
/*
* thread.c: A simple thread base class
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* $Id: thread.c 1.9 2001/06/27 11:34:41 kls Exp $
2000-10-08 09:25:20 +02:00
*/
#include "thread.h"
2000-12-08 16:23:32 +01:00
#include <errno.h>
2000-10-08 09:25:20 +02:00
#include <signal.h>
2000-12-08 16:23:32 +01:00
#include <sys/wait.h>
2000-10-08 09:25:20 +02:00
#include <unistd.h>
2000-12-08 16:23:32 +01:00
#include "tools.h"
2000-10-08 09:25:20 +02:00
// --- cMutex ----------------------------------------------------------------
cMutex::cMutex(void)
{
lockingPid = 0;
locked = 0;
pthread_mutex_init(&mutex, NULL);
}
cMutex::~cMutex()
{
pthread_mutex_destroy(&mutex);
}
void cMutex::Lock(void)
{
if (getpid() != lockingPid || !locked)
pthread_mutex_lock(&mutex);
lockingPid = getpid();
locked++;
}
void cMutex::Unlock(void)
{
if (!--locked)
pthread_mutex_unlock(&mutex);
}
2000-10-08 09:25:20 +02:00
// --- cThread ---------------------------------------------------------------
// The signal handler is necessary to be able to use SIGIO to wake up any
// pending 'select()' call.
time_t cThread::lastPanic = 0;
int cThread::panicLevel = 0;
2000-10-08 09:25:20 +02:00
bool cThread::signalHandlerInstalled = false;
bool cThread::emergencyExitRequested = false;
2000-10-08 09:25:20 +02:00
cThread::cThread(void)
{
if (!signalHandlerInstalled) {
signal(SIGIO, SignalHandler);
signalHandlerInstalled = true;
}
running = false;
2000-12-08 16:23:32 +01:00
parentPid = threadPid = lockingPid = 0;
2000-10-08 09:25:20 +02:00
locked = 0;
}
cThread::~cThread()
{
}
void cThread::SignalHandler(int signum)
{
signal(signum, SignalHandler);
}
void *cThread::StartThread(cThread *Thread)
{
2000-12-08 16:23:32 +01:00
Thread->threadPid = getpid();
2000-10-08 09:25:20 +02:00
Thread->Action();
return NULL;
}
bool cThread::Start(void)
{
if (!running) {
running = true;
parentPid = getpid();
2000-11-22 17:11:04 +01:00
pthread_create(&thread, NULL, (void *(*) (void *))&StartThread, (void *)this);
2000-12-28 12:57:16 +01:00
usleep(10000); // otherwise calling Active() immediately after Start() causes a "pure virtual method called" error
2000-10-08 09:25:20 +02:00
}
return true; //XXX return value of pthread_create()???
}
2000-12-08 16:23:32 +01:00
bool cThread::Active(void)
2000-10-08 09:25:20 +02:00
{
2000-12-08 16:23:32 +01:00
if (threadPid) {
if (kill(threadPid, SIGIO) < 0) { // couldn't find another way of checking whether the thread is still running - any ideas?
if (errno == ESRCH)
threadPid = 0;
else
LOG_ERROR;
}
else
return true;
}
return false;
}
void cThread::Cancel(int WaitSeconds)
{
if (WaitSeconds > 0) {
for (time_t t0 = time(NULL) + WaitSeconds; time(NULL) < t0; ) {
if (!Active())
return;
usleep(10000);
}
esyslog(LOG_ERR, "ERROR: thread %d won't end (waited %d seconds) - cancelling it...", threadPid, WaitSeconds);
}
2000-10-08 16:49:41 +02:00
pthread_cancel(thread);
2000-10-08 09:25:20 +02:00
}
bool cThread::Lock(void)
{
if (!lockingPid || lockingPid != getpid()) {
Mutex.Lock();
2000-10-08 09:25:20 +02:00
lockingPid = getpid();
}
locked++;
return true;
}
void cThread::Unlock(void)
{
if (!--locked) {
lockingPid = 0;
Mutex.Unlock();
2000-10-08 09:25:20 +02:00
}
}
void cThread::WakeUp(void)
{
kill(parentPid, SIGIO); // makes any waiting 'select()' call return immediately
}
#define MAXPANICLEVEL 10
void cThread::RaisePanic(void)
{
if (lastPanic > 0) {
if (time(NULL) - lastPanic < 5)
panicLevel++;
else if (panicLevel > 0)
panicLevel--;
}
lastPanic = time(NULL);
if (panicLevel > MAXPANICLEVEL) {
esyslog(LOG_ERR, "ERROR: max. panic level exceeded");
EmergencyExit(true);
}
else
dsyslog(LOG_INFO, "panic level: %d", panicLevel);
}
bool cThread::EmergencyExit(bool Request)
{
if (!Request)
return emergencyExitRequested;
esyslog(LOG_ERR, "initiating emergency exit");
return emergencyExitRequested = true; // yes, it's an assignment, not a comparison!
}
2000-10-08 09:25:20 +02:00
// --- cThreadLock -----------------------------------------------------------
cThreadLock::cThreadLock(cThread *Thread)
{
2000-10-29 13:17:22 +01:00
thread = NULL;
locked = false;
Lock(Thread);
2000-10-08 09:25:20 +02:00
}
cThreadLock::~cThreadLock()
{
2000-10-29 13:17:22 +01:00
if (thread && locked)
2000-10-08 09:25:20 +02:00
thread->Unlock();
}
2000-10-29 13:17:22 +01:00
bool cThreadLock::Lock(cThread *Thread)
{
if (Thread && !thread) {
thread = Thread;
locked = Thread->Lock();
return locked;
}
return false;
}
2000-10-08 09:25:20 +02:00
bool cThreadLock::Locked(void)
{
return locked;
}