diff options
author | Pieter Wuille <pieter.wuille@gmail.com> | 2012-05-10 20:45:35 +0200 |
---|---|---|
committer | Pieter Wuille <pieter.wuille@gmail.com> | 2012-05-11 17:02:11 +0200 |
commit | 5456ef30926fa38b0d1705f9dcc6aa8def8a802d (patch) | |
tree | 0559266ebfba329c089a77ef9dcd19581fb2770f /src | |
parent | c59abe25892e32c803ec527c40e9a74ab31ea58a (diff) |
Use polling instead of boost's broken semaphore on OSX
Diffstat (limited to 'src')
-rw-r--r-- | src/util.h | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h index 61ff553539..f25a030f19 100644 --- a/src/util.h +++ b/src/util.h @@ -292,7 +292,47 @@ typedef CMutexLock<CCriticalSection> 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 |