From 5456ef30926fa38b0d1705f9dcc6aa8def8a802d Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 10 May 2012 20:45:35 +0200 Subject: [PATCH] Use polling instead of boost's broken semaphore on OSX --- src/util.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/util.h b/src/util.h index 61ff5535..f25a030f 100644 --- a/src/util.h +++ b/src/util.h @@ -292,7 +292,47 @@ typedef CMutexLock CCriticalBlock; LeaveCritical(); \ } +#ifdef MAC_OSX +// boost::interprocess::interprocess_semaphore seems to spinlock on OSX; prefer polling instead +class CSemaphore +{ +private: + CCriticalSection cs; + int val; + +public: + CSemaphore(int init) : val(init) {} + + void wait() { + do { + { + LOCK(cs); + if (val>0) { + val--; + return; + } + } + Sleep(100); + } while(1); + } + + bool try_wait() { + LOCK(cs); + if (val>0) { + val--; + return true; + } + return false; + } + + void post() { + LOCK(cs); + val++; + } +}; +#else typedef boost::interprocess::interprocess_semaphore CSemaphore; +#endif /** RAII-style semaphore lock */ class CSemaphoreGrant