c@287
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@287
|
2
|
c@287
|
3 /*
|
c@287
|
4 QM DSP Library
|
c@287
|
5
|
c@287
|
6 Centre for Digital Music, Queen Mary, University of London.
|
c@287
|
7 This file copyright Chris Cannam, used with permission.
|
c@287
|
8 */
|
c@287
|
9
|
cannam@489
|
10 #ifndef QM_DSP_THREAD_H
|
cannam@489
|
11 #define QM_DSP_THREAD_H
|
c@287
|
12
|
c@287
|
13 #ifdef _WIN32
|
c@287
|
14 #include <windows.h>
|
c@287
|
15 #else /* !_WIN32 */
|
c@287
|
16 #ifdef USE_PTHREADS
|
c@287
|
17 #include <pthread.h>
|
c@359
|
18 #else
|
c@359
|
19 #error Must have either _WIN32 or USE_PTHREADS defined
|
c@287
|
20 #endif /* USE_PTHREADS */
|
c@287
|
21 #endif /* !_WIN32 */
|
c@287
|
22
|
c@287
|
23 #include <string>
|
c@287
|
24
|
c@287
|
25 //#define DEBUG_THREAD 1
|
c@287
|
26 //#define DEBUG_MUTEX 1
|
c@287
|
27 //#define DEBUG_CONDITION 1
|
c@287
|
28
|
c@287
|
29 class Thread
|
c@287
|
30 {
|
c@287
|
31 public:
|
c@287
|
32 #ifdef _WIN32
|
c@287
|
33 typedef HANDLE Id;
|
c@287
|
34 #else
|
c@287
|
35 #ifdef USE_PTHREADS
|
c@287
|
36 typedef pthread_t Id;
|
c@287
|
37 #endif
|
c@287
|
38 #endif
|
c@287
|
39
|
c@287
|
40 Thread();
|
c@287
|
41 virtual ~Thread();
|
c@287
|
42
|
c@287
|
43 Id id();
|
c@287
|
44
|
c@287
|
45 void start();
|
c@287
|
46 void wait();
|
c@287
|
47
|
c@287
|
48 static bool threadingAvailable();
|
c@287
|
49
|
c@287
|
50 protected:
|
c@287
|
51 virtual void run() = 0;
|
c@287
|
52
|
c@287
|
53 private:
|
c@287
|
54 #ifdef _WIN32
|
c@287
|
55 HANDLE m_id;
|
c@287
|
56 bool m_extant;
|
c@287
|
57 static DWORD WINAPI staticRun(LPVOID lpParam);
|
c@287
|
58 #else
|
c@287
|
59 #ifdef USE_PTHREADS
|
c@287
|
60 pthread_t m_id;
|
c@287
|
61 bool m_extant;
|
c@287
|
62 static void *staticRun(void *);
|
c@287
|
63 #endif
|
c@287
|
64 #endif
|
c@287
|
65 };
|
c@287
|
66
|
c@287
|
67 class Mutex
|
c@287
|
68 {
|
c@287
|
69 public:
|
c@287
|
70 Mutex();
|
c@287
|
71 ~Mutex();
|
c@287
|
72
|
c@287
|
73 void lock();
|
c@287
|
74 void unlock();
|
c@287
|
75 bool trylock();
|
c@287
|
76
|
c@287
|
77 private:
|
c@287
|
78 #ifdef _WIN32
|
c@287
|
79 HANDLE m_mutex;
|
c@287
|
80 #ifndef NO_THREAD_CHECKS
|
c@287
|
81 DWORD m_lockedBy;
|
c@287
|
82 #endif
|
c@287
|
83 #else
|
c@287
|
84 #ifdef USE_PTHREADS
|
c@287
|
85 pthread_mutex_t m_mutex;
|
c@287
|
86 #ifndef NO_THREAD_CHECKS
|
c@287
|
87 pthread_t m_lockedBy;
|
c@287
|
88 bool m_locked;
|
c@287
|
89 #endif
|
c@287
|
90 #endif
|
c@287
|
91 #endif
|
c@287
|
92 };
|
c@287
|
93
|
c@287
|
94 class MutexLocker
|
c@287
|
95 {
|
c@287
|
96 public:
|
c@287
|
97 MutexLocker(Mutex *);
|
c@287
|
98 ~MutexLocker();
|
c@287
|
99
|
c@287
|
100 private:
|
c@287
|
101 Mutex *m_mutex;
|
c@287
|
102 };
|
c@287
|
103
|
c@287
|
104 class Condition
|
c@287
|
105 {
|
c@287
|
106 public:
|
c@287
|
107 Condition(std::string name);
|
c@287
|
108 ~Condition();
|
c@287
|
109
|
c@288
|
110 // Condition bundles a pthread-style condition variable and mutex
|
c@288
|
111 // into one class.
|
c@287
|
112
|
c@288
|
113 // To wait on a condition, call lock(), test termination variables
|
c@288
|
114 // as appropriate, and then wait(). The condition will be
|
c@288
|
115 // unlocked for the duration of the wait() call, which will end
|
c@288
|
116 // when the condition is signalled. The condition will be locked
|
c@288
|
117 // again when wait() returns.
|
c@288
|
118 //
|
c@288
|
119 // To signal a condition, call signal(). If the waiting thread
|
c@288
|
120 // will be performing tests between its own lock() and wait(),
|
c@288
|
121 // then the signalling thread should also lock() before it signals
|
c@288
|
122 // (and then unlock afterwards). If the signalling thread always
|
c@288
|
123 // locks the mutex during signalling, then the waiting thread
|
c@288
|
124 // knows that signals will only happen during wait() and not be
|
c@288
|
125 // missed at other times.
|
c@288
|
126
|
c@287
|
127 void lock();
|
c@287
|
128 void unlock();
|
c@287
|
129 void wait(int us = 0);
|
c@287
|
130
|
c@287
|
131 void signal();
|
c@287
|
132
|
c@287
|
133 private:
|
c@287
|
134
|
c@287
|
135 #ifdef _WIN32
|
c@287
|
136 HANDLE m_mutex;
|
c@287
|
137 HANDLE m_condition;
|
c@287
|
138 bool m_locked;
|
c@287
|
139 #else
|
c@287
|
140 #ifdef USE_PTHREADS
|
c@287
|
141 pthread_mutex_t m_mutex;
|
c@287
|
142 pthread_cond_t m_condition;
|
c@287
|
143 bool m_locked;
|
c@287
|
144 #endif
|
c@287
|
145 #endif
|
c@287
|
146 #ifdef DEBUG_CONDITION
|
c@287
|
147 std::string m_name;
|
c@287
|
148 #endif
|
c@287
|
149 };
|
c@287
|
150
|
c@287
|
151 #endif
|