From 6eb06945e4115f5f7c67368dfb05646e8ee72caa Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 12 May 2012 15:49:11 +0000 Subject: Add Semaphore::timed_wait(). git-svn-id: http://svn.drobilla.net/lad/trunk/raul@4379 a436a847-0d15-0410-975c-d299462d15a1 --- raul/Semaphore.hpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'raul') diff --git a/raul/Semaphore.hpp b/raul/Semaphore.hpp index 8c07f3a..c45f92d 100644 --- a/raul/Semaphore.hpp +++ b/raul/Semaphore.hpp @@ -70,12 +70,15 @@ public: /** Post/Increment/Signal */ inline void post(); - /** Wait/Decrement. Returns false on error. */ + /** Wait/Decrement. Return false on error. */ inline bool wait(); - /** Attempt Wait/Decrement. Returns true iff a decrement occurred. */ + /** Attempt Wait/Decrement. Return true iff decremented. */ inline bool try_wait(); + /** Wait for at most @p ms milliseconds. Return true iff decremented. */ + inline bool timed_wait(unsigned ms); + private: inline bool init(unsigned initial); inline void destroy(); @@ -128,6 +131,14 @@ Semaphore::try_wait() return semaphore_timedwait(_sem, zero) == KERN_SUCCESS; } +inline bool +Semaphore::timed_wait(unsigned ms) +{ + const unsigned seconds = ms / 1000; + const mach_timespec_t t = { seconds, (ms - (seconds * 1000)) * 1000000 }; + return semaphore_timedwait(_sem, t) == KERN_SUCCESS; +} + #elif defined(_WIN32) inline bool @@ -166,6 +177,12 @@ Semaphore::try_wait() return WaitForSingleObject(_sem, 0) == WAIT_OBJECT_0; } +inline bool +Semaphore::timed_wait(unsigned ms) +{ + return WaitForSingleObject(_sem, ms) == WAIT_OBJECT_0; +} + #else /* !defined(__APPLE__) && !defined(_WIN32) */ inline bool @@ -208,6 +225,14 @@ Semaphore::try_wait() return (sem_trywait(&_sem) == 0); } +inline bool +Semaphore::timed_wait(unsigned ms) +{ + const unsigned seconds = ms / 1000; + const struct timespec_t t = { seconds, (ms - (seconds * 1000)) * 1000000 }; + return (sem_timedwait(_sem, &t) == 0); +} + #endif } // namespace Raul -- cgit v1.2.1