comparison thread/Thread.h @ 62:b63f1ccbc9b6

* oops... add thread abstraction
author cannam
date Tue, 12 May 2009 17:56:58 +0000
parents
children 0dcbce5d7dce
comparison
equal deleted inserted replaced
61:d5b6b88e2025 62:b63f1ccbc9b6
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 QM DSP Library
5
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright Chris Cannam, used with permission.
8 */
9
10 #ifndef _THREAD_H_
11 #define _THREAD_H_
12
13 #ifdef _WIN32
14 #include <windows.h>
15 #else /* !_WIN32 */
16 #ifdef USE_PTHREADS
17 #include <pthread.h>
18 #endif /* USE_PTHREADS */
19 #endif /* !_WIN32 */
20
21 #include <string>
22
23 //#define DEBUG_THREAD 1
24 //#define DEBUG_MUTEX 1
25 //#define DEBUG_CONDITION 1
26
27 class Thread
28 {
29 public:
30 #ifdef _WIN32
31 typedef HANDLE Id;
32 #else
33 #ifdef USE_PTHREADS
34 typedef pthread_t Id;
35 #endif
36 #endif
37
38 Thread();
39 virtual ~Thread();
40
41 Id id();
42
43 void start();
44 void wait();
45
46 static bool threadingAvailable();
47
48 protected:
49 virtual void run() = 0;
50
51 private:
52 #ifdef _WIN32
53 HANDLE m_id;
54 bool m_extant;
55 static DWORD WINAPI staticRun(LPVOID lpParam);
56 #else
57 #ifdef USE_PTHREADS
58 pthread_t m_id;
59 bool m_extant;
60 static void *staticRun(void *);
61 #endif
62 #endif
63 };
64
65 class Mutex
66 {
67 public:
68 Mutex();
69 ~Mutex();
70
71 void lock();
72 void unlock();
73 bool trylock();
74
75 private:
76 #ifdef _WIN32
77 HANDLE m_mutex;
78 #ifndef NO_THREAD_CHECKS
79 DWORD m_lockedBy;
80 #endif
81 #else
82 #ifdef USE_PTHREADS
83 pthread_mutex_t m_mutex;
84 #ifndef NO_THREAD_CHECKS
85 pthread_t m_lockedBy;
86 bool m_locked;
87 #endif
88 #endif
89 #endif
90 };
91
92 class MutexLocker
93 {
94 public:
95 MutexLocker(Mutex *);
96 ~MutexLocker();
97
98 private:
99 Mutex *m_mutex;
100 };
101
102 class Condition
103 {
104 public:
105 Condition(std::string name);
106 ~Condition();
107
108 //!!! NO -- reproducing more conventional lock/wait
109
110 // To wait on a condition, either simply call wait(), or call
111 // lock() and then wait() (perhaps testing some state in between).
112 // To signal a condition, call signal().
113
114 // Although any thread may signal on a given condition, only one
115 // thread should ever wait on any given condition object --
116 // otherwise there will be a race conditions in the logic that
117 // avoids the thread code having to track whether the condition's
118 // mutex is locked or not. If that is your requirement, this
119 // Condition wrapper is not for you.
120 void lock();
121 void unlock();
122 void wait(int us = 0);
123
124 void signal();
125
126 private:
127
128 #ifdef _WIN32
129 HANDLE m_mutex;
130 HANDLE m_condition;
131 bool m_locked;
132 #else
133 #ifdef USE_PTHREADS
134 pthread_mutex_t m_mutex;
135 pthread_cond_t m_condition;
136 bool m_locked;
137 #endif
138 #endif
139 #ifdef DEBUG_CONDITION
140 std::string m_name;
141 #endif
142 };
143
144 #endif