2000-10-08 09:25:20 +02:00
|
|
|
/*
|
|
|
|
* thread.h: 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.h 1.3 2000/11/14 18:38:11 kls Exp $
|
2000-10-08 09:25:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __THREAD_H
|
|
|
|
#define __THREAD_H
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2000-11-18 13:57:32 +01:00
|
|
|
class cMutex {
|
|
|
|
private:
|
|
|
|
pthread_mutex_t mutex;
|
|
|
|
public:
|
|
|
|
cMutex(void) { pthread_mutex_init(&mutex, NULL); }
|
|
|
|
~cMutex() { pthread_mutex_destroy(&mutex); }
|
|
|
|
void Lock(void) { pthread_mutex_lock(&mutex); }
|
|
|
|
void Unlock(void) { pthread_mutex_unlock(&mutex); }
|
|
|
|
};
|
|
|
|
|
2000-10-08 09:25:20 +02:00
|
|
|
class cThread {
|
|
|
|
friend class cThreadLock;
|
|
|
|
private:
|
|
|
|
pthread_t thread;
|
2000-11-18 13:57:32 +01:00
|
|
|
cMutex Mutex;
|
2000-10-08 09:25:20 +02:00
|
|
|
pid_t parentPid, lockingPid;
|
|
|
|
int locked;
|
|
|
|
bool running;
|
|
|
|
static bool signalHandlerInstalled;
|
|
|
|
static void SignalHandler(int signum);
|
|
|
|
static void *StartThread(cThread *Thread);
|
|
|
|
bool Lock(void);
|
|
|
|
void Unlock(void);
|
|
|
|
protected:
|
|
|
|
void WakeUp(void);
|
|
|
|
virtual void Action(void) = 0;
|
|
|
|
void Stop(void);
|
|
|
|
public:
|
|
|
|
cThread(void);
|
|
|
|
virtual ~cThread();
|
|
|
|
bool Start(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
// cThreadLock can be used to easily set a lock in a thread and make absolutely
|
|
|
|
// sure that it will be unlocked when the block will be left. Several locks can
|
|
|
|
// be stacked, so a function that makes many calls to another function which uses
|
|
|
|
// cThreadLock may itself use a cThreadLock to make one longer lock instead of many
|
|
|
|
// short ones.
|
|
|
|
|
|
|
|
class cThreadLock {
|
|
|
|
private:
|
|
|
|
cThread *thread;
|
|
|
|
bool locked;
|
|
|
|
public:
|
2000-10-29 13:17:22 +01:00
|
|
|
cThreadLock(cThread *Thread = NULL);
|
2000-10-08 09:25:20 +02:00
|
|
|
~cThreadLock();
|
2000-10-29 13:17:22 +01:00
|
|
|
bool Lock(cThread *Thread);
|
2000-10-08 09:25:20 +02:00
|
|
|
bool Locked(void);
|
|
|
|
};
|
|
|
|
|
|
|
|
#define LOCK_THREAD cThreadLock ThreadLock(this)
|
|
|
|
|
|
|
|
#endif //__THREAD_H
|