Use polling instead of boost's broken semaphore on OSX
authorPieter Wuille <pieter.wuille@gmail.com>
Thu, 10 May 2012 18:45:35 +0000 (20:45 +0200)
committerPieter Wuille <pieter.wuille@gmail.com>
Thu, 10 May 2012 20:34:49 +0000 (22:34 +0200)
src/util.h

index f90b95b..15ccf82 100644 (file)
@@ -287,7 +287,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
 
 inline std::string i64tostr(int64 n)
 {