X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fpbd%2Fsemutils.cc;h=496eaf4d913641a8d91b9b704e5b5328c03c6a1e;hb=7af5de2074a5bac7bc43f68d6784e436069fae99;hp=48fdd249f60f9e8879ef1c69a26323ad1dcb0e76;hpb=680c64246e4ee9f3eb53da079dea6a7ecf996e92;p=ardour.git diff --git a/libs/pbd/semutils.cc b/libs/pbd/semutils.cc index 48fdd249f6..496eaf4d91 100644 --- a/libs/pbd/semutils.cc +++ b/libs/pbd/semutils.cc @@ -21,15 +21,20 @@ using namespace PBD; -ProcessSemaphore::ProcessSemaphore (const char* name, int val) +Semaphore::Semaphore (const char* name, int val) { -#ifdef __APPLE__ +#ifdef WINDOWS_SEMAPHORE + if ((_sem = CreateSemaphore(NULL, val, 32767, name)) == NULL) { + throw failed_constructor (); + } + +#elif __APPLE__ if ((_sem = sem_open (name, O_CREAT, 0600, val)) == (sem_t*) SEM_FAILED) { throw failed_constructor (); } /* this semaphore does not exist for IPC */ - + if (sem_unlink (name)) { throw failed_constructor (); } @@ -43,9 +48,42 @@ ProcessSemaphore::ProcessSemaphore (const char* name, int val) #endif } -ProcessSemaphore::~ProcessSemaphore () +Semaphore::~Semaphore () { -#ifdef __APPLE__ +#ifdef WINDOWS_SEMAPHORE + CloseHandle(_sem); +#elif __APPLE__ sem_close (ptr_to_sem()); #endif } + +#ifdef WINDOWS_SEMAPHORE + +int +Semaphore::signal () +{ + // non-zero on success, opposite to posix + return !ReleaseSemaphore(_sem, 1, NULL); +} + +int +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