when tabbing between track/bus name fields, skip rec-enabled tracks to avoid an annoy...
[ardour2.git] / libs / qm-dsp / thread / Thread.cpp
blob85dcec3155e26a3c3254642f91b3ea5fe5a88593
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
3 /*
4 QM DSP Library
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright Chris Cannam, used with permission.
8 */
10 #include "Thread.h"
12 #include <iostream>
13 #include <cstdlib>
15 #ifdef USE_PTHREADS
16 #include <sys/time.h>
17 #include <time.h>
18 #endif
20 using std::cerr;
21 using std::endl;
22 using std::string;
24 #ifdef _WIN32
26 Thread::Thread() :
27 m_id(0),
28 m_extant(false)
30 #ifdef DEBUG_THREAD
31 cerr << "THREAD DEBUG: Created thread object " << this << endl;
32 #endif
35 Thread::~Thread()
37 #ifdef DEBUG_THREAD
38 cerr << "THREAD DEBUG: Destroying thread object " << this << ", id " << m_id << endl;
39 #endif
40 if (m_extant) {
41 WaitForSingleObject(m_id, INFINITE);
43 #ifdef DEBUG_THREAD
44 cerr << "THREAD DEBUG: Destroyed thread object " << this << endl;
45 #endif
48 void
49 Thread::start()
51 m_id = CreateThread(NULL, 0, staticRun, this, 0, 0);
52 if (!m_id) {
53 cerr << "ERROR: thread creation failed" << endl;
54 exit(1);
55 } else {
56 #ifdef DEBUG_THREAD
57 cerr << "THREAD DEBUG: Created thread " << m_id << " for thread object " << this << endl;
58 #endif
59 m_extant = true;
63 void
64 Thread::wait()
66 if (m_extant) {
67 #ifdef DEBUG_THREAD
68 cerr << "THREAD DEBUG: Waiting on thread " << m_id << " for thread object " << this << endl;
69 #endif
70 WaitForSingleObject(m_id, INFINITE);
71 #ifdef DEBUG_THREAD
72 cerr << "THREAD DEBUG: Waited on thread " << m_id << " for thread object " << this << endl;
73 #endif
74 m_extant = false;
78 Thread::Id
79 Thread::id()
81 return m_id;
84 bool
85 Thread::threadingAvailable()
87 return true;
90 DWORD
91 Thread::staticRun(LPVOID arg)
93 Thread *thread = static_cast<Thread *>(arg);
94 #ifdef DEBUG_THREAD
95 cerr << "THREAD DEBUG: " << (void *)GetCurrentThreadId() << ": Running thread " << thread->m_id << " for thread object " << thread << endl;
96 #endif
97 thread->run();
98 return 0;
101 Mutex::Mutex()
102 #ifndef NO_THREAD_CHECKS
104 m_lockedBy(-1)
105 #endif
107 m_mutex = CreateMutex(NULL, FALSE, NULL);
108 #ifdef DEBUG_MUTEX
109 cerr << "MUTEX DEBUG: " << (void *)GetCurrentThreadId() << ": Initialised mutex " << &m_mutex << endl;
110 #endif
113 Mutex::~Mutex()
115 #ifdef DEBUG_MUTEX
116 cerr << "MUTEX DEBUG: " << (void *)GetCurrentThreadId() << ": Destroying mutex " << &m_mutex << endl;
117 #endif
118 CloseHandle(m_mutex);
121 void
122 Mutex::lock()
124 #ifndef NO_THREAD_CHECKS
125 DWORD tid = GetCurrentThreadId();
126 if (m_lockedBy == tid) {
127 cerr << "ERROR: Deadlock on mutex " << &m_mutex << endl;
129 #endif
130 #ifdef DEBUG_MUTEX
131 cerr << "MUTEX DEBUG: " << (void *)tid << ": Want to lock mutex " << &m_mutex << endl;
132 #endif
133 WaitForSingleObject(m_mutex, INFINITE);
134 #ifndef NO_THREAD_CHECKS
135 m_lockedBy = tid;
136 #endif
137 #ifdef DEBUG_MUTEX
138 cerr << "MUTEX DEBUG: " << (void *)tid << ": Locked mutex " << &m_mutex << endl;
139 #endif
142 void
143 Mutex::unlock()
145 #ifndef NO_THREAD_CHECKS
146 DWORD tid = GetCurrentThreadId();
147 if (m_lockedBy != tid) {
148 cerr << "ERROR: Mutex " << &m_mutex << " not owned by unlocking thread" << endl;
149 return;
151 #endif
152 #ifdef DEBUG_MUTEX
153 cerr << "MUTEX DEBUG: " << (void *)tid << ": Unlocking mutex " << &m_mutex << endl;
154 #endif
155 #ifndef NO_THREAD_CHECKS
156 m_lockedBy = -1;
157 #endif
158 ReleaseMutex(m_mutex);
161 bool
162 Mutex::trylock()
164 #ifndef NO_THREAD_CHECKS
165 DWORD tid = GetCurrentThreadId();
166 #endif
167 DWORD result = WaitForSingleObject(m_mutex, 0);
168 if (result == WAIT_TIMEOUT || result == WAIT_FAILED) {
169 #ifdef DEBUG_MUTEX
170 cerr << "MUTEX DEBUG: " << (void *)tid << ": Mutex " << &m_mutex << " unavailable" << endl;
171 #endif
172 return false;
173 } else {
174 #ifndef NO_THREAD_CHECKS
175 m_lockedBy = tid;
176 #endif
177 #ifdef DEBUG_MUTEX
178 cerr << "MUTEX DEBUG: " << (void *)tid << ": Locked mutex " << &m_mutex << " (from trylock)" << endl;
179 #endif
180 return true;
184 Condition::Condition(string name) :
185 m_locked(false)
186 #ifdef DEBUG_CONDITION
187 , m_name(name)
188 #endif
190 m_mutex = CreateMutex(NULL, FALSE, NULL);
191 m_condition = CreateEvent(NULL, FALSE, FALSE, NULL);
192 #ifdef DEBUG_CONDITION
193 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Initialised condition " << &m_condition << " \"" << m_name << "\"" << endl;
194 #endif
197 Condition::~Condition()
199 #ifdef DEBUG_CONDITION
200 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Destroying condition " << &m_condition << " \"" << m_name << "\"" << endl;
201 #endif
202 if (m_locked) ReleaseMutex(m_mutex);
203 CloseHandle(m_condition);
204 CloseHandle(m_mutex);
207 void
208 Condition::lock()
210 #ifdef DEBUG_CONDITION
211 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Want to lock " << &m_condition << " \"" << m_name << "\"" << endl;
212 #endif
213 WaitForSingleObject(m_mutex, INFINITE);
214 m_locked = true;
215 #ifdef DEBUG_CONDITION
216 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Locked " << &m_condition << " \"" << m_name << "\"" << endl;
217 #endif
220 void
221 Condition::unlock()
223 if (!m_locked) {
224 #ifdef DEBUG_CONDITION
225 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Not locked " << &m_condition << " \"" << m_name << "\"" << endl;
226 #endif
227 return;
229 #ifdef DEBUG_CONDITION
230 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Unlocking " << &m_condition << " \"" << m_name << "\"" << endl;
231 #endif
232 m_locked = false;
233 ReleaseMutex(m_mutex);
236 void
237 Condition::wait(int us)
239 if (us == 0) {
241 #ifdef DEBUG_CONDITION
242 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Waiting on " << &m_condition << " \"" << m_name << "\"" << endl;
243 #endif
244 SignalObjectAndWait(m_mutex, m_condition, INFINITE, FALSE);
245 WaitForSingleObject(m_mutex, INFINITE);
247 } else {
249 DWORD ms = us / 1000;
250 if (us > 0 && ms == 0) ms = 1;
252 #ifdef DEBUG_CONDITION
253 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Timed waiting on " << &m_condition << " \"" << m_name << "\"" << endl;
254 #endif
255 SignalObjectAndWait(m_mutex, m_condition, ms, FALSE);
256 WaitForSingleObject(m_mutex, INFINITE);
259 #ifdef DEBUG_CONDITION
260 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Wait done on " << &m_condition << " \"" << m_name << "\"" << endl;
261 #endif
263 m_locked = true;
266 void
267 Condition::signal()
269 #ifdef DEBUG_CONDITION
270 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Signalling " << &m_condition << " \"" << m_name << "\"" << endl;
271 #endif
272 SetEvent(m_condition);
275 #else /* !_WIN32 */
277 #ifdef USE_PTHREADS
279 Thread::Thread() :
280 m_id(0),
281 m_extant(false)
283 #ifdef DEBUG_THREAD
284 cerr << "THREAD DEBUG: Created thread object " << this << endl;
285 #endif
288 Thread::~Thread()
290 #ifdef DEBUG_THREAD
291 cerr << "THREAD DEBUG: Destroying thread object " << this << ", id " << m_id << endl;
292 #endif
293 if (m_extant) {
294 pthread_join(m_id, 0);
296 #ifdef DEBUG_THREAD
297 cerr << "THREAD DEBUG: Destroyed thread object " << this << endl;
298 #endif
301 void
302 Thread::start()
304 if (pthread_create(&m_id, 0, staticRun, this)) {
305 cerr << "ERROR: thread creation failed" << endl;
306 exit(1);
307 } else {
308 #ifdef DEBUG_THREAD
309 cerr << "THREAD DEBUG: Created thread " << m_id << " for thread object " << this << endl;
310 #endif
311 m_extant = true;
315 void
316 Thread::wait()
318 if (m_extant) {
319 #ifdef DEBUG_THREAD
320 cerr << "THREAD DEBUG: Waiting on thread " << m_id << " for thread object " << this << endl;
321 #endif
322 pthread_join(m_id, 0);
323 #ifdef DEBUG_THREAD
324 cerr << "THREAD DEBUG: Waited on thread " << m_id << " for thread object " << this << endl;
325 #endif
326 m_extant = false;
330 Thread::Id
331 Thread::id()
333 return m_id;
336 bool
337 Thread::threadingAvailable()
339 return true;
342 void *
343 Thread::staticRun(void *arg)
345 Thread *thread = static_cast<Thread *>(arg);
346 #ifdef DEBUG_THREAD
347 cerr << "THREAD DEBUG: " << (void *)pthread_self() << ": Running thread " << thread->m_id << " for thread object " << thread << endl;
348 #endif
349 thread->run();
350 return 0;
353 Mutex::Mutex()
354 #ifndef NO_THREAD_CHECKS
356 m_lockedBy(0),
357 m_locked(false)
358 #endif
360 pthread_mutex_init(&m_mutex, 0);
361 #ifdef DEBUG_MUTEX
362 cerr << "MUTEX DEBUG: " << (void *)pthread_self() << ": Initialised mutex " << &m_mutex << endl;
363 #endif
366 Mutex::~Mutex()
368 #ifdef DEBUG_MUTEX
369 cerr << "MUTEX DEBUG: " << (void *)pthread_self() << ": Destroying mutex " << &m_mutex << endl;
370 #endif
371 pthread_mutex_destroy(&m_mutex);
374 void
375 Mutex::lock()
377 #ifndef NO_THREAD_CHECKS
378 pthread_t tid = pthread_self();
379 if (m_locked && m_lockedBy == tid) {
380 cerr << "ERROR: Deadlock on mutex " << &m_mutex << endl;
382 #endif
383 #ifdef DEBUG_MUTEX
384 cerr << "MUTEX DEBUG: " << (void *)tid << ": Want to lock mutex " << &m_mutex << endl;
385 #endif
386 pthread_mutex_lock(&m_mutex);
387 #ifndef NO_THREAD_CHECKS
388 m_lockedBy = tid;
389 m_locked = true;
390 #endif
391 #ifdef DEBUG_MUTEX
392 cerr << "MUTEX DEBUG: " << (void *)tid << ": Locked mutex " << &m_mutex << endl;
393 #endif
396 void
397 Mutex::unlock()
399 #ifndef NO_THREAD_CHECKS
400 pthread_t tid = pthread_self();
401 if (!m_locked) {
402 cerr << "ERROR: Mutex " << &m_mutex << " not locked in unlock" << endl;
403 return;
404 } else if (m_lockedBy != tid) {
405 cerr << "ERROR: Mutex " << &m_mutex << " not owned by unlocking thread" << endl;
406 return;
408 #endif
409 #ifdef DEBUG_MUTEX
410 cerr << "MUTEX DEBUG: " << (void *)tid << ": Unlocking mutex " << &m_mutex << endl;
411 #endif
412 #ifndef NO_THREAD_CHECKS
413 m_locked = false;
414 #endif
415 pthread_mutex_unlock(&m_mutex);
418 bool
419 Mutex::trylock()
421 #ifndef NO_THREAD_CHECKS
422 pthread_t tid = pthread_self();
423 #endif
424 if (pthread_mutex_trylock(&m_mutex)) {
425 #ifdef DEBUG_MUTEX
426 cerr << "MUTEX DEBUG: " << (void *)tid << ": Mutex " << &m_mutex << " unavailable" << endl;
427 #endif
428 return false;
429 } else {
430 #ifndef NO_THREAD_CHECKS
431 m_lockedBy = tid;
432 m_locked = true;
433 #endif
434 #ifdef DEBUG_MUTEX
435 cerr << "MUTEX DEBUG: " << (void *)tid << ": Locked mutex " << &m_mutex << " (from trylock)" << endl;
436 #endif
437 return true;
441 Condition::Condition(string name) :
442 m_locked(false)
443 #ifdef DEBUG_CONDITION
444 , m_name(name)
445 #endif
447 pthread_mutex_init(&m_mutex, 0);
448 pthread_cond_init(&m_condition, 0);
449 #ifdef DEBUG_CONDITION
450 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Initialised condition " << &m_condition << " \"" << m_name << "\"" << endl;
451 #endif
454 Condition::~Condition()
456 #ifdef DEBUG_CONDITION
457 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Destroying condition " << &m_condition << " \"" << m_name << "\"" << endl;
458 #endif
459 if (m_locked) pthread_mutex_unlock(&m_mutex);
460 pthread_cond_destroy(&m_condition);
461 pthread_mutex_destroy(&m_mutex);
464 void
465 Condition::lock()
467 #ifdef DEBUG_CONDITION
468 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Want to lock " << &m_condition << " \"" << m_name << "\"" << endl;
469 #endif
470 pthread_mutex_lock(&m_mutex);
471 m_locked = true;
472 #ifdef DEBUG_CONDITION
473 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Locked " << &m_condition << " \"" << m_name << "\"" << endl;
474 #endif
477 void
478 Condition::unlock()
480 if (!m_locked) {
481 #ifdef DEBUG_CONDITION
482 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Not locked " << &m_condition << " \"" << m_name << "\"" << endl;
483 #endif
484 return;
486 #ifdef DEBUG_CONDITION
487 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Unlocking " << &m_condition << " \"" << m_name << "\"" << endl;
488 #endif
489 m_locked = false;
490 pthread_mutex_unlock(&m_mutex);
493 void
494 Condition::wait(int us)
496 if (us == 0) {
498 #ifdef DEBUG_CONDITION
499 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Waiting on " << &m_condition << " \"" << m_name << "\"" << endl;
500 #endif
501 pthread_cond_wait(&m_condition, &m_mutex);
503 } else {
505 struct timeval now;
506 gettimeofday(&now, 0);
508 now.tv_usec += us;
509 while (now.tv_usec > 1000000) {
510 now.tv_usec -= 1000000;
511 ++now.tv_sec;
514 struct timespec timeout;
515 timeout.tv_sec = now.tv_sec;
516 timeout.tv_nsec = now.tv_usec * 1000;
518 #ifdef DEBUG_CONDITION
519 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Timed waiting on " << &m_condition << " \"" << m_name << "\"" << endl;
520 #endif
521 pthread_cond_timedwait(&m_condition, &m_mutex, &timeout);
524 #ifdef DEBUG_CONDITION
525 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Wait done on " << &m_condition << " \"" << m_name << "\"" << endl;
526 #endif
528 m_locked = true;
531 void
532 Condition::signal()
534 #ifdef DEBUG_CONDITION
535 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Signalling " << &m_condition << " \"" << m_name << "\"" << endl;
536 #endif
537 pthread_cond_signal(&m_condition);
540 #else /* !USE_PTHREADS */
542 Thread::Thread()
546 Thread::~Thread()
550 void
551 Thread::start()
553 abort();
556 void
557 Thread::wait()
559 abort();
562 Thread::Id
563 Thread::id()
565 abort();
568 bool
569 Thread::threadingAvailable()
571 return false;
574 Mutex::Mutex()
578 Mutex::~Mutex()
582 void
583 Mutex::lock()
585 abort();
588 void
589 Mutex::unlock()
591 abort();
594 bool
595 Mutex::trylock()
597 abort();
600 Condition::Condition(const char *)
604 Condition::~Condition()
608 void
609 Condition::lock()
611 abort();
614 void
615 Condition::wait(int us)
617 abort();
620 void
621 Condition::signal()
623 abort();
626 #endif /* !USE_PTHREADS */
627 #endif /* !_WIN32 */
629 MutexLocker::MutexLocker(Mutex *mutex) :
630 m_mutex(mutex)
632 if (m_mutex) {
633 m_mutex->lock();
637 MutexLocker::~MutexLocker()
639 if (m_mutex) {
640 m_mutex->unlock();