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.
|
|
|
|
*
|
2000-11-18 13:57:32 +01:00
|
|
|
* $Id: thread.c 1.4 2000/11/14 18:38:25 kls Exp $
|
2000-10-08 09:25:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "thread.h"
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
// --- cThread ---------------------------------------------------------------
|
|
|
|
|
|
|
|
// The signal handler is necessary to be able to use SIGIO to wake up any
|
|
|
|
// pending 'select()' call.
|
|
|
|
|
|
|
|
bool cThread::signalHandlerInstalled = false;
|
|
|
|
|
|
|
|
cThread::cThread(void)
|
|
|
|
{
|
|
|
|
if (!signalHandlerInstalled) {
|
|
|
|
signal(SIGIO, SignalHandler);
|
|
|
|
signalHandlerInstalled = true;
|
|
|
|
}
|
|
|
|
running = false;
|
|
|
|
parentPid = lockingPid = 0;
|
|
|
|
locked = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cThread::~cThread()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void cThread::SignalHandler(int signum)
|
|
|
|
{
|
|
|
|
signal(signum, SignalHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *cThread::StartThread(cThread *Thread)
|
|
|
|
{
|
|
|
|
Thread->Action();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cThread::Start(void)
|
|
|
|
{
|
|
|
|
if (!running) {
|
|
|
|
running = true;
|
|
|
|
parentPid = getpid();
|
|
|
|
pthread_create(&thread, NULL, &StartThread, (void *)this);
|
|
|
|
}
|
|
|
|
return true; //XXX return value of pthread_create()???
|
|
|
|
}
|
|
|
|
|
|
|
|
void cThread::Stop(void)
|
|
|
|
{
|
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()) {
|
2000-11-18 13:57:32 +01:00
|
|
|
Mutex.Lock();
|
2000-10-08 09:25:20 +02:00
|
|
|
lockingPid = getpid();
|
|
|
|
}
|
|
|
|
locked++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cThread::Unlock(void)
|
|
|
|
{
|
|
|
|
if (!--locked) {
|
|
|
|
lockingPid = 0;
|
2000-11-18 13:57:32 +01:00
|
|
|
Mutex.Unlock();
|
2000-10-08 09:25:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cThread::WakeUp(void)
|
|
|
|
{
|
|
|
|
kill(parentPid, SIGIO); // makes any waiting 'select()' call return immediately
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- 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;
|
|
|
|
}
|
|
|
|
|