3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
* Copyright (C) 2005 - 2012 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
27 |
|
|
28 |
#include "global_private.h" |
#include "global_private.h" |
29 |
|
|
30 |
|
#if CONFIG_PTHREAD_TESTCANCEL |
31 |
|
#include "Thread.h" |
32 |
|
#endif |
33 |
|
|
34 |
namespace LinuxSampler { |
namespace LinuxSampler { |
35 |
|
|
36 |
// *************** Internal data types (for Windows only) *************** |
// *************** Internal data types (for Windows only) *************** |
268 |
#endif |
#endif |
269 |
} |
} |
270 |
|
|
271 |
|
#ifndef WIN32 |
272 |
|
namespace { |
273 |
|
// If the thread is cancelled while waiting for the condition |
274 |
|
// variable, the mutex will be locked. It needs to be unlocked so |
275 |
|
// the Condition can be reused if the thread is restarted. |
276 |
|
void condition_cleanup(void* c) { |
277 |
|
static_cast<Condition*>(c)->Unlock(); |
278 |
|
} |
279 |
|
} |
280 |
|
#endif |
281 |
|
|
282 |
int Condition::WaitIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) { |
int Condition::WaitIf(bool bCondition, long TimeoutSeconds, long TimeoutNanoSeconds) { |
283 |
dmsg(7,("Condition::WaitIf: bCondition=%d TimeoutSeconds=%d TimeoutNanoSeconds=%d\n",bCondition, TimeoutSeconds, TimeoutNanoSeconds)); |
dmsg(7,("Condition::WaitIf: bCondition=%d TimeoutSeconds=%d TimeoutNanoSeconds=%d\n",bCondition, TimeoutSeconds, TimeoutNanoSeconds)); |
284 |
dmsg(7,("Condition::Waitif() -> LOCK()\n")); |
dmsg(7,("Condition::Waitif() -> LOCK()\n")); |
285 |
Lock(); |
Lock(); |
286 |
dmsg(7,("Condition::Waitif() -> LOCK() passed\n")); |
dmsg(7,("Condition::Waitif() -> LOCK() passed\n")); |
287 |
int res = 0; |
int res = 0; |
288 |
|
#ifndef WIN32 |
289 |
|
pthread_cleanup_push(condition_cleanup, this); |
290 |
|
#endif |
291 |
if (this->bCondition == bCondition) { |
if (this->bCondition == bCondition) { |
292 |
if (bCondition) { // wait until condition turned 'false' |
if (bCondition) { // wait until condition turned 'false' |
293 |
#if defined(WIN32) |
#if defined(WIN32) |
342 |
#endif |
#endif |
343 |
} |
} |
344 |
} |
} |
345 |
|
#ifndef WIN32 |
346 |
|
pthread_cleanup_pop(0); |
347 |
|
#endif |
348 |
return res; |
return res; |
349 |
} |
} |
350 |
|
|
365 |
if (bCondition) { |
if (bCondition) { |
366 |
dmsg(7,("Condition::Set() -> broadcasting 'true' condition\n")); |
dmsg(7,("Condition::Set() -> broadcasting 'true' condition\n")); |
367 |
#if defined(WIN32) |
#if defined(WIN32) |
368 |
win32thread_cond_broadcast(&__win32_true_condition); |
ConditionInternal::win32thread_cond_broadcast(&__win32_true_condition); |
369 |
#else |
#else |
370 |
pthread_cond_broadcast(&__posix_true_condition); |
pthread_cond_broadcast(&__posix_true_condition); |
371 |
#endif |
#endif |
373 |
else { |
else { |
374 |
dmsg(7,("Condition::Set() -> broadcasting 'false' condition\n")); |
dmsg(7,("Condition::Set() -> broadcasting 'false' condition\n")); |
375 |
#if defined(WIN32) |
#if defined(WIN32) |
376 |
win32thread_cond_broadcast(&__win32_false_condition); |
ConditionInternal::win32thread_cond_broadcast(&__win32_false_condition); |
377 |
#else |
#else |
378 |
pthread_cond_broadcast(&__posix_false_condition); |
pthread_cond_broadcast(&__posix_false_condition); |
379 |
#endif |
#endif |
386 |
return bCondition; |
return bCondition; |
387 |
} |
} |
388 |
|
|
389 |
|
#ifdef WIN32 |
390 |
|
void Condition::Reset() { |
391 |
|
__win32_true_condition.waiters_count_ = 0; |
392 |
|
__win32_true_condition.was_broadcast_ = 0; |
393 |
|
__win32_false_condition.waiters_count_ = 0; |
394 |
|
__win32_false_condition.was_broadcast_ = 0; |
395 |
|
} |
396 |
|
#endif |
397 |
|
|
398 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |