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.
|
|
|
|
*
|
2002-02-23 13:55:57 +01:00
|
|
|
* $Id: thread.h 1.12 2002/02/23 13:53:38 kls Exp $
|
2000-10-08 09:25:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __THREAD_H
|
|
|
|
#define __THREAD_H
|
|
|
|
|
|
|
|
#include <pthread.h>
|
2001-09-15 13:00:58 +02:00
|
|
|
#include <stdio.h>
|
2000-10-08 09:25:20 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2001-08-03 14:18:08 +02:00
|
|
|
class cMutex;
|
|
|
|
|
|
|
|
class cCondVar {
|
|
|
|
private:
|
|
|
|
pthread_cond_t cond;
|
|
|
|
public:
|
|
|
|
cCondVar(void);
|
|
|
|
~cCondVar();
|
2001-08-05 12:23:24 +02:00
|
|
|
bool Wait(cMutex &Mutex);
|
|
|
|
//bool TimedWait(cMutex &Mutex, unsigned long tmout);
|
2001-08-03 14:18:08 +02:00
|
|
|
void Broadcast(void);
|
2001-08-05 12:23:24 +02:00
|
|
|
//void Signal(void);
|
2001-08-03 14:18:08 +02:00
|
|
|
};
|
|
|
|
|
2000-11-18 13:57:32 +01:00
|
|
|
class cMutex {
|
2001-08-03 14:18:08 +02:00
|
|
|
friend class cCondVar;
|
2000-11-18 13:57:32 +01:00
|
|
|
private:
|
|
|
|
pthread_mutex_t mutex;
|
2001-06-02 10:47:40 +02:00
|
|
|
pid_t lockingPid;
|
|
|
|
int locked;
|
2000-11-18 13:57:32 +01:00
|
|
|
public:
|
2001-06-02 10:47:40 +02:00
|
|
|
cMutex(void);
|
|
|
|
~cMutex();
|
|
|
|
void Lock(void);
|
|
|
|
void Unlock(void);
|
2000-11-18 13:57:32 +01:00
|
|
|
};
|
|
|
|
|
2000-10-08 09:25:20 +02:00
|
|
|
class cThread {
|
|
|
|
friend class cThreadLock;
|
|
|
|
private:
|
|
|
|
pthread_t thread;
|
2001-10-27 13:23:06 +02:00
|
|
|
cMutex mutex;
|
|
|
|
pid_t parentPid, threadPid;
|
2000-10-08 09:25:20 +02:00
|
|
|
bool running;
|
2001-07-12 10:27:18 +02:00
|
|
|
static time_t lastPanic;
|
|
|
|
static int panicLevel;
|
2001-06-02 10:47:40 +02:00
|
|
|
static bool emergencyExitRequested;
|
2000-10-08 09:25:20 +02:00
|
|
|
static bool signalHandlerInstalled;
|
|
|
|
static void SignalHandler(int signum);
|
|
|
|
static void *StartThread(cThread *Thread);
|
2001-10-27 13:23:06 +02:00
|
|
|
void Lock(void) { mutex.Lock(); }
|
|
|
|
void Unlock(void) { mutex.Unlock(); }
|
2000-10-08 09:25:20 +02:00
|
|
|
protected:
|
|
|
|
void WakeUp(void);
|
|
|
|
virtual void Action(void) = 0;
|
2000-12-08 16:23:32 +01:00
|
|
|
void Cancel(int WaitSeconds = 0);
|
2000-10-08 09:25:20 +02:00
|
|
|
public:
|
|
|
|
cThread(void);
|
|
|
|
virtual ~cThread();
|
|
|
|
bool Start(void);
|
2000-12-08 16:23:32 +01:00
|
|
|
bool Active(void);
|
2001-07-12 10:27:18 +02:00
|
|
|
static void RaisePanic(void);
|
2001-06-02 10:47:40 +02:00
|
|
|
static bool EmergencyExit(bool Request = false);
|
2000-10-08 09:25:20 +02:00
|
|
|
};
|
2002-02-23 13:55:57 +01:00
|
|
|
|
|
|
|
// cMutexLock can be used to easily set a lock on mutex 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
|
|
|
|
// cMutexLock may itself use a cMutexLock to make one longer lock instead of many
|
|
|
|
// short ones.
|
|
|
|
|
|
|
|
class cMutexLock {
|
|
|
|
private:
|
|
|
|
cMutex *mutex;
|
|
|
|
bool locked;
|
|
|
|
public:
|
|
|
|
cMutexLock(cMutex *Mutex = NULL);
|
|
|
|
~cMutexLock();
|
|
|
|
bool Lock(cMutex *Mutex);
|
|
|
|
};
|
2000-10-08 09:25:20 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
};
|
|
|
|
|
|
|
|
#define LOCK_THREAD cThreadLock ThreadLock(this)
|
|
|
|
|
2001-09-15 13:00:58 +02:00
|
|
|
// cPipe implements a pipe that closes all unnecessary file descriptors in
|
|
|
|
// the child process.
|
|
|
|
|
|
|
|
class cPipe {
|
|
|
|
private:
|
|
|
|
pid_t pid;
|
|
|
|
FILE *f;
|
|
|
|
public:
|
|
|
|
cPipe(void);
|
|
|
|
~cPipe();
|
|
|
|
operator FILE* () { return f; }
|
|
|
|
bool Open(const char *Command, const char *Mode);
|
|
|
|
int Close(void);
|
|
|
|
};
|
|
|
|
|
2001-10-20 10:39:27 +02:00
|
|
|
// SystemExec() implements a 'system()' call that closes all unnecessary file
|
|
|
|
// descriptors in the child process.
|
|
|
|
|
|
|
|
int SystemExec(const char *Command);
|
|
|
|
|
2000-10-08 09:25:20 +02:00
|
|
|
#endif //__THREAD_H
|