summaryrefslogtreecommitdiff
path: root/thread.h
blob: b47f6d715a76a92be715d1fb6b2a033e0bb32689 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * thread.h: A simple thread base class
 *
 * See the main source file 'vdr.c' for copyright information and
 * how to reach the author.
 *
 * $Id: thread.h 1.2 2000/10/28 15:08:09 kls Exp $
 */

#ifndef __THREAD_H
#define __THREAD_H

#include <pthread.h>
#include <sys/types.h>

class cThread {
  friend class cThreadLock;
private:
  pthread_t thread;
  pthread_mutex_t mutex;
  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:
  cThreadLock(cThread *Thread = NULL);
  ~cThreadLock();
  bool Lock(cThread *Thread);
  bool Locked(void);
  };

#define LOCK_THREAD cThreadLock ThreadLock(this)

#endif //__THREAD_H