X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fpbd%2Fsemutils.cc;h=496eaf4d913641a8d91b9b704e5b5328c03c6a1e;hb=7af5de2074a5bac7bc43f68d6784e436069fae99;hp=e5e5898218aad97b34cf259a326b55a668f8fecb;hpb=8c5cff60912c7e0a7256f635641399500d8d00d9;p=ardour.git diff --git a/libs/pbd/semutils.cc b/libs/pbd/semutils.cc index e5e5898218..496eaf4d91 100644 --- a/libs/pbd/semutils.cc +++ b/libs/pbd/semutils.cc @@ -21,9 +21,9 @@ using namespace PBD; -ProcessSemaphore::ProcessSemaphore (const char* name, int val) +Semaphore::Semaphore (const char* name, int val) { -#ifdef PLATFORM_WINDOWS +#ifdef WINDOWS_SEMAPHORE if ((_sem = CreateSemaphore(NULL, val, 32767, name)) == NULL) { throw failed_constructor (); } @@ -34,42 +34,56 @@ ProcessSemaphore::ProcessSemaphore (const char* name, int val) } /* this semaphore does not exist for IPC */ - + if (sem_unlink (name)) { throw failed_constructor (); } #else + (void) name; /* stop gcc warning on !Apple systems */ + if (sem_init (&_sem, 0, val)) { throw failed_constructor (); } #endif } -ProcessSemaphore::~ProcessSemaphore () +Semaphore::~Semaphore () { -#ifdef PLATFORM_WINDOWS +#ifdef WINDOWS_SEMAPHORE CloseHandle(_sem); #elif __APPLE__ sem_close (ptr_to_sem()); #endif } -#ifdef PLATFORM_WINDOWS +#ifdef WINDOWS_SEMAPHORE int -ProcessSemaphore::signal () +Semaphore::signal () { // non-zero on success, opposite to posix return !ReleaseSemaphore(_sem, 1, NULL); } int -ProcessSemaphore::wait () +Semaphore::wait () { DWORD result = 0; result = WaitForSingleObject(_sem, INFINITE); return (result == WAIT_OBJECT_0); } +int +Semaphore::reset () +{ + int rv = -1; + DWORD result; + do { + ++rv; + result = WaitForSingleObject(_sem, 0); + } while (result == WAIT_OBJECT_0); + return rv; +} + #endif