13
0

Add implementation for ProcessSemaphore on windows

This commit is contained in:
Paul Davis 2013-07-11 12:46:05 -04:00
parent 8b63e005a1
commit e70db68fac
2 changed files with 43 additions and 3 deletions

View File

@ -19,13 +19,20 @@
#ifndef __pbd_semutils_h__
#define __pbd_semutils_h__
#ifdef WIN32
#include <windows.h>
#else
#include <semaphore.h>
#endif
namespace PBD {
class ProcessSemaphore {
private:
#ifdef __APPLE__
#ifdef WIN32
HANDLE _sem;
#elif __APPLE__
sem_t* _sem;
sem_t* ptr_to_sem() const { return _sem; }
#else
@ -37,8 +44,15 @@ class ProcessSemaphore {
ProcessSemaphore (const char* name, int val);
~ProcessSemaphore ();
#ifdef WIN32
int signal ();
int wait ();
#else
int signal () { return sem_post (ptr_to_sem()); }
int wait () { return sem_wait (ptr_to_sem()); }
#endif
};
}

View File

@ -23,7 +23,12 @@ using namespace PBD;
ProcessSemaphore::ProcessSemaphore (const char* name, int val)
{
#ifdef __APPLE__
#ifdef WIN32
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 ();
}
@ -43,7 +48,28 @@ ProcessSemaphore::ProcessSemaphore (const char* name, int val)
ProcessSemaphore::~ProcessSemaphore ()
{
#ifdef __APPLE__
#ifdef WIN32
CloseHandle(_sem);
#elif __APPLE__
sem_close (ptr_to_sem());
#endif
}
#ifdef WIN32
int
ProcessSemaphore::signal ()
{
// non-zero on success, opposite to posix
return !ReleaseSemaphore(_sem, 1, NULL);
}
int
ProcessSemaphore::wait ()
{
DWORD result = 0;
result = WaitForSingleObject(_sem, INFINITE);
return (result == WAIT_OBJECT_0);
}
#endif