comparison thread/Thread.cpp @ 62:b63f1ccbc9b6

* oops... add thread abstraction
author cannam
date Tue, 12 May 2009 17:56:58 +0000
parents
children 6afa0e011f74
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 #include "Thread.h"
11
12 #include <iostream>
13 #include <cstdlib>
14
15 #ifdef USE_PTHREADS
16 #include <sys/time.h>
17 #include <time.h>
18 #endif
19
20 using std::cerr;
21 using std::endl;
22 using std::string;
23
24 #ifdef _WIN32
25
26 Thread::Thread() :
27 m_id(0),
28 m_extant(false)
29 {
30 #ifdef DEBUG_THREAD
31 cerr << "THREAD DEBUG: Created thread object " << this << endl;
32 #endif
33 }
34
35 Thread::~Thread()
36 {
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);
42 }
43 #ifdef DEBUG_THREAD
44 cerr << "THREAD DEBUG: Destroyed thread object " << this << endl;
45 #endif
46 }
47
48 void
49 Thread::start()
50 {
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;
60 }
61 }
62
63 void
64 Thread::wait()
65 {
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;
75 }
76 }
77
78 Thread::Id
79 Thread::id()
80 {
81 return m_id;
82 }
83
84 bool
85 Thread::threadingAvailable()
86 {
87 return true;
88 }
89
90 DWORD
91 Thread::staticRun(LPVOID arg)
92 {
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;
99 }
100
101 Mutex::Mutex()
102 #ifndef NO_THREAD_CHECKS
103 :
104 m_lockedBy(-1)
105 #endif
106 {
107 m_mutex = CreateMutex(NULL, FALSE, NULL);
108 #ifdef DEBUG_MUTEX
109 cerr << "MUTEX DEBUG: " << (void *)GetCurrentThreadId() << ": Initialised mutex " << &m_mutex << endl;
110 #endif
111 }
112
113 Mutex::~Mutex()
114 {
115 #ifdef DEBUG_MUTEX
116 cerr << "MUTEX DEBUG: " << (void *)GetCurrentThreadId() << ": Destroying mutex " << &m_mutex << endl;
117 #endif
118 CloseHandle(m_mutex);
119 }
120
121 void
122 Mutex::lock()
123 {
124 #ifndef NO_THREAD_CHECKS
125 DWORD tid = GetCurrentThreadId();
126 if (m_lockedBy == tid) {
127 cerr << "ERROR: Deadlock on mutex " << &m_mutex << endl;
128 }
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
140 }
141
142 void
143 Mutex::unlock()
144 {
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;
150 }
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);
159 }
160
161 bool
162 Mutex::trylock()
163 {
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;
181 }
182 }
183
184 Condition::Condition(string name) :
185 m_locked(false)
186 #ifdef DEBUG_CONDITION
187 , m_name(name)
188 #endif
189 {
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
195 }
196
197 Condition::~Condition()
198 {
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);
205 }
206
207 void
208 Condition::lock()
209 {
210 if (m_locked) {
211 #ifdef DEBUG_CONDITION
212 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Already locked " << &m_condition << " \"" << m_name << "\"" << endl;
213 #endif
214 return;
215 }
216 #ifdef DEBUG_CONDITION
217 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Want to lock " << &m_condition << " \"" << m_name << "\"" << endl;
218 #endif
219 WaitForSingleObject(m_mutex, INFINITE);
220 m_locked = true;
221 #ifdef DEBUG_CONDITION
222 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Locked " << &m_condition << " \"" << m_name << "\"" << endl;
223 #endif
224 }
225
226 void
227 Condition::unlock()
228 {
229 if (!m_locked) {
230 #ifdef DEBUG_CONDITION
231 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Not locked " << &m_condition << " \"" << m_name << "\"" << endl;
232 #endif
233 return;
234 }
235 #ifdef DEBUG_CONDITION
236 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Unlocking " << &m_condition << " \"" << m_name << "\"" << endl;
237 #endif
238 m_locked = false;
239 ReleaseMutex(m_mutex);
240 }
241
242 void
243 Condition::wait(int us)
244 {
245 if (us == 0) {
246
247 #ifdef DEBUG_CONDITION
248 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Waiting on " << &m_condition << " \"" << m_name << "\"" << endl;
249 #endif
250 SignalObjectAndWait(m_mutex, m_condition, INFINITE, FALSE);
251 WaitForSingleObject(m_mutex, INFINITE);
252
253 } else {
254
255 DWORD ms = us / 1000;
256 if (us > 0 && ms == 0) ms = 1;
257
258 #ifdef DEBUG_CONDITION
259 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Timed waiting on " << &m_condition << " \"" << m_name << "\"" << endl;
260 #endif
261 SignalObjectAndWait(m_mutex, m_condition, ms, FALSE);
262 WaitForSingleObject(m_mutex, INFINITE);
263 }
264
265 #ifdef DEBUG_CONDITION
266 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Wait done on " << &m_condition << " \"" << m_name << "\"" << endl;
267 #endif
268 }
269
270 void
271 Condition::signal()
272 {
273 #ifdef DEBUG_CONDITION
274 cerr << "CONDITION DEBUG: " << (void *)GetCurrentThreadId() << ": Signalling " << &m_condition << " \"" << m_name << "\"" << endl;
275 #endif
276 SetEvent(m_condition);
277 }
278
279 #else /* !_WIN32 */
280
281 #ifdef USE_PTHREADS
282
283 Thread::Thread() :
284 m_id(0),
285 m_extant(false)
286 {
287 #ifdef DEBUG_THREAD
288 cerr << "THREAD DEBUG: Created thread object " << this << endl;
289 #endif
290 }
291
292 Thread::~Thread()
293 {
294 #ifdef DEBUG_THREAD
295 cerr << "THREAD DEBUG: Destroying thread object " << this << ", id " << m_id << endl;
296 #endif
297 if (m_extant) {
298 pthread_join(m_id, 0);
299 }
300 #ifdef DEBUG_THREAD
301 cerr << "THREAD DEBUG: Destroyed thread object " << this << endl;
302 #endif
303 }
304
305 void
306 Thread::start()
307 {
308 if (pthread_create(&m_id, 0, staticRun, this)) {
309 cerr << "ERROR: thread creation failed" << endl;
310 exit(1);
311 } else {
312 #ifdef DEBUG_THREAD
313 cerr << "THREAD DEBUG: Created thread " << m_id << " for thread object " << this << endl;
314 #endif
315 m_extant = true;
316 }
317 }
318
319 void
320 Thread::wait()
321 {
322 if (m_extant) {
323 #ifdef DEBUG_THREAD
324 cerr << "THREAD DEBUG: Waiting on thread " << m_id << " for thread object " << this << endl;
325 #endif
326 pthread_join(m_id, 0);
327 #ifdef DEBUG_THREAD
328 cerr << "THREAD DEBUG: Waited on thread " << m_id << " for thread object " << this << endl;
329 #endif
330 m_extant = false;
331 }
332 }
333
334 Thread::Id
335 Thread::id()
336 {
337 return m_id;
338 }
339
340 bool
341 Thread::threadingAvailable()
342 {
343 return true;
344 }
345
346 void *
347 Thread::staticRun(void *arg)
348 {
349 Thread *thread = static_cast<Thread *>(arg);
350 #ifdef DEBUG_THREAD
351 cerr << "THREAD DEBUG: " << (void *)pthread_self() << ": Running thread " << thread->m_id << " for thread object " << thread << endl;
352 #endif
353 thread->run();
354 return 0;
355 }
356
357 Mutex::Mutex()
358 #ifndef NO_THREAD_CHECKS
359 :
360 m_lockedBy(0),
361 m_locked(false)
362 #endif
363 {
364 pthread_mutex_init(&m_mutex, 0);
365 #ifdef DEBUG_MUTEX
366 cerr << "MUTEX DEBUG: " << (void *)pthread_self() << ": Initialised mutex " << &m_mutex << endl;
367 #endif
368 }
369
370 Mutex::~Mutex()
371 {
372 #ifdef DEBUG_MUTEX
373 cerr << "MUTEX DEBUG: " << (void *)pthread_self() << ": Destroying mutex " << &m_mutex << endl;
374 #endif
375 pthread_mutex_destroy(&m_mutex);
376 }
377
378 void
379 Mutex::lock()
380 {
381 #ifndef NO_THREAD_CHECKS
382 pthread_t tid = pthread_self();
383 if (m_locked && m_lockedBy == tid) {
384 cerr << "ERROR: Deadlock on mutex " << &m_mutex << endl;
385 }
386 #endif
387 #ifdef DEBUG_MUTEX
388 cerr << "MUTEX DEBUG: " << (void *)tid << ": Want to lock mutex " << &m_mutex << endl;
389 #endif
390 pthread_mutex_lock(&m_mutex);
391 #ifndef NO_THREAD_CHECKS
392 m_lockedBy = tid;
393 m_locked = true;
394 #endif
395 #ifdef DEBUG_MUTEX
396 cerr << "MUTEX DEBUG: " << (void *)tid << ": Locked mutex " << &m_mutex << endl;
397 #endif
398 }
399
400 void
401 Mutex::unlock()
402 {
403 #ifndef NO_THREAD_CHECKS
404 pthread_t tid = pthread_self();
405 if (!m_locked) {
406 cerr << "ERROR: Mutex " << &m_mutex << " not locked in unlock" << endl;
407 return;
408 } else if (m_lockedBy != tid) {
409 cerr << "ERROR: Mutex " << &m_mutex << " not owned by unlocking thread" << endl;
410 return;
411 }
412 #endif
413 #ifdef DEBUG_MUTEX
414 cerr << "MUTEX DEBUG: " << (void *)tid << ": Unlocking mutex " << &m_mutex << endl;
415 #endif
416 #ifndef NO_THREAD_CHECKS
417 m_locked = false;
418 #endif
419 pthread_mutex_unlock(&m_mutex);
420 }
421
422 bool
423 Mutex::trylock()
424 {
425 #ifndef NO_THREAD_CHECKS
426 pthread_t tid = pthread_self();
427 #endif
428 if (pthread_mutex_trylock(&m_mutex)) {
429 #ifdef DEBUG_MUTEX
430 cerr << "MUTEX DEBUG: " << (void *)tid << ": Mutex " << &m_mutex << " unavailable" << endl;
431 #endif
432 return false;
433 } else {
434 #ifndef NO_THREAD_CHECKS
435 m_lockedBy = tid;
436 m_locked = true;
437 #endif
438 #ifdef DEBUG_MUTEX
439 cerr << "MUTEX DEBUG: " << (void *)tid << ": Locked mutex " << &m_mutex << " (from trylock)" << endl;
440 #endif
441 return true;
442 }
443 }
444
445 Condition::Condition(string name) :
446 m_locked(false)
447 #ifdef DEBUG_CONDITION
448 , m_name(name)
449 #endif
450 {
451 pthread_mutex_init(&m_mutex, 0);
452 pthread_cond_init(&m_condition, 0);
453 #ifdef DEBUG_CONDITION
454 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Initialised condition " << &m_condition << " \"" << m_name << "\"" << endl;
455 #endif
456 }
457
458 Condition::~Condition()
459 {
460 #ifdef DEBUG_CONDITION
461 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Destroying condition " << &m_condition << " \"" << m_name << "\"" << endl;
462 #endif
463 if (m_locked) pthread_mutex_unlock(&m_mutex);
464 pthread_cond_destroy(&m_condition);
465 pthread_mutex_destroy(&m_mutex);
466 }
467
468 void
469 Condition::lock()
470 {
471 if (m_locked) {
472 #ifdef DEBUG_CONDITION
473 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Already locked " << &m_condition << " \"" << m_name << "\"" << endl;
474 #endif
475 return;
476 }
477 #ifdef DEBUG_CONDITION
478 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Want to lock " << &m_condition << " \"" << m_name << "\"" << endl;
479 #endif
480 pthread_mutex_lock(&m_mutex);
481 m_locked = true;
482 #ifdef DEBUG_CONDITION
483 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Locked " << &m_condition << " \"" << m_name << "\"" << endl;
484 #endif
485 }
486
487 void
488 Condition::unlock()
489 {
490 if (!m_locked) {
491 #ifdef DEBUG_CONDITION
492 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Not locked " << &m_condition << " \"" << m_name << "\"" << endl;
493 #endif
494 return;
495 }
496 #ifdef DEBUG_CONDITION
497 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Unlocking " << &m_condition << " \"" << m_name << "\"" << endl;
498 #endif
499 m_locked = false;
500 pthread_mutex_unlock(&m_mutex);
501 }
502
503 void
504 Condition::wait(int us)
505 {
506 if (us == 0) {
507
508 #ifdef DEBUG_CONDITION
509 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Waiting on " << &m_condition << " \"" << m_name << "\"" << endl;
510 #endif
511 pthread_cond_wait(&m_condition, &m_mutex);
512
513 } else {
514
515 struct timeval now;
516 gettimeofday(&now, 0);
517
518 now.tv_usec += us;
519 while (now.tv_usec > 1000000) {
520 now.tv_usec -= 1000000;
521 ++now.tv_sec;
522 }
523
524 struct timespec timeout;
525 timeout.tv_sec = now.tv_sec;
526 timeout.tv_nsec = now.tv_usec * 1000;
527
528 #ifdef DEBUG_CONDITION
529 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Timed waiting on " << &m_condition << " \"" << m_name << "\"" << endl;
530 #endif
531 pthread_cond_timedwait(&m_condition, &m_mutex, &timeout);
532 }
533
534 #ifdef DEBUG_CONDITION
535 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Wait done on " << &m_condition << " \"" << m_name << "\"" << endl;
536 #endif
537 }
538
539 void
540 Condition::signal()
541 {
542 #ifdef DEBUG_CONDITION
543 cerr << "CONDITION DEBUG: " << (void *)pthread_self() << ": Signalling " << &m_condition << " \"" << m_name << "\"" << endl;
544 #endif
545 pthread_cond_signal(&m_condition);
546 }
547
548 #else /* !USE_PTHREADS */
549
550 Thread::Thread()
551 {
552 }
553
554 Thread::~Thread()
555 {
556 }
557
558 void
559 Thread::start()
560 {
561 abort();
562 }
563
564 void
565 Thread::wait()
566 {
567 abort();
568 }
569
570 Thread::Id
571 Thread::id()
572 {
573 abort();
574 }
575
576 bool
577 Thread::threadingAvailable()
578 {
579 return false;
580 }
581
582 Mutex::Mutex()
583 {
584 }
585
586 Mutex::~Mutex()
587 {
588 }
589
590 void
591 Mutex::lock()
592 {
593 abort();
594 }
595
596 void
597 Mutex::unlock()
598 {
599 abort();
600 }
601
602 bool
603 Mutex::trylock()
604 {
605 abort();
606 }
607
608 Condition::Condition(const char *)
609 {
610 }
611
612 Condition::~Condition()
613 {
614 }
615
616 void
617 Condition::lock()
618 {
619 abort();
620 }
621
622 void
623 Condition::wait(int us)
624 {
625 abort();
626 }
627
628 void
629 Condition::signal()
630 {
631 abort();
632 }
633
634 #endif /* !USE_PTHREADS */
635 #endif /* !_WIN32 */
636
637 MutexLocker::MutexLocker(Mutex *mutex) :
638 m_mutex(mutex)
639 {
640 if (m_mutex) {
641 m_mutex->lock();
642 }
643 }
644
645 MutexLocker::~MutexLocker()
646 {
647 if (m_mutex) {
648 m_mutex->unlock();
649 }
650 }
651