Skip to content

Commit 5456ef3

Browse files
committed
Use polling instead of boost's broken semaphore on OSX
1 parent c59abe2 commit 5456ef3

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/util.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,47 @@ typedef CMutexLock<CCriticalSection> CCriticalBlock;
292292
LeaveCritical(); \
293293
}
294294

295+
#ifdef MAC_OSX
296+
// boost::interprocess::interprocess_semaphore seems to spinlock on OSX; prefer polling instead
297+
class CSemaphore
298+
{
299+
private:
300+
CCriticalSection cs;
301+
int val;
302+
303+
public:
304+
CSemaphore(int init) : val(init) {}
305+
306+
void wait() {
307+
do {
308+
{
309+
LOCK(cs);
310+
if (val>0) {
311+
val--;
312+
return;
313+
}
314+
}
315+
Sleep(100);
316+
} while(1);
317+
}
318+
319+
bool try_wait() {
320+
LOCK(cs);
321+
if (val>0) {
322+
val--;
323+
return true;
324+
}
325+
return false;
326+
}
327+
328+
void post() {
329+
LOCK(cs);
330+
val++;
331+
}
332+
};
333+
#else
295334
typedef boost::interprocess::interprocess_semaphore CSemaphore;
335+
#endif
296336

297337
/** RAII-style semaphore lock */
298338
class CSemaphoreGrant

0 commit comments

Comments
 (0)