From 97c3bb61482855769f0208062610543475f02006 Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Sun, 8 Oct 2000 09:25:20 +0200 Subject: Remote control uses threads --- thread.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 thread.c (limited to 'thread.c') diff --git a/thread.c b/thread.c new file mode 100644 index 00000000..7e3ee939 --- /dev/null +++ b/thread.c @@ -0,0 +1,105 @@ +/* + * 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.1 2000/10/07 17:31:39 kls Exp $ + */ + +#include "thread.h" +#include +#include + +// --- 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; + } + pthread_mutex_init(&mutex, NULL); + running = false; + parentPid = lockingPid = 0; + locked = 0; +} + +cThread::~cThread() +{ + pthread_mutex_destroy(&mutex); +} + +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) +{ + pthread_exit(NULL); +} + +bool cThread::Lock(void) +{ + if (!lockingPid || lockingPid != getpid()) { + pthread_mutex_lock(&mutex); + lockingPid = getpid(); + } + locked++; + return true; +} + +void cThread::Unlock(void) +{ + if (!--locked) { + lockingPid = 0; + pthread_mutex_unlock(&mutex); + } +} + +void cThread::WakeUp(void) +{ + kill(parentPid, SIGIO); // makes any waiting 'select()' call return immediately +} + +// --- cThreadLock ----------------------------------------------------------- + +cThreadLock::cThreadLock(cThread *Thread) +{ + thread = Thread; + locked = Thread->Lock(); +} + +cThreadLock::~cThreadLock() +{ + if (locked) + thread->Unlock(); +} + +bool cThreadLock::Locked(void) +{ + return locked; +} + -- cgit v1.2.3