#ifndef _WIN32
+#include <errno.h>
#include "../handle/handle.h"
#include "../log.h"
#define TAG WINPR_TAG("synch.semaphore")
-static pthread_once_t semaphore_initialized = PTHREAD_ONCE_INIT;
-
-static HANDLE_CLOSE_CB _SemaphoreHandleCloseCb;
static BOOL SemaphoreCloseHandle(HANDLE handle);
return TRUE;
}
-static void SemaphoreInitialize(void)
+static int SemaphoreGetFd(HANDLE handle)
+{
+ WINPR_SEMAPHORE *sem = (WINPR_SEMAPHORE *)handle;
+
+ if (!SemaphoreIsHandled(handle))
+ return -1;
+
+ return sem->pipe_fd[0];
+}
+
+static DWORD SemaphoreCleanupHandle(HANDLE handle)
{
- _SemaphoreHandleCloseCb.IsHandled = SemaphoreIsHandled;
- _SemaphoreHandleCloseCb.CloseHandle = SemaphoreCloseHandle;
- RegisterHandleCloseCb(&_SemaphoreHandleCloseCb);
+ int length;
+ WINPR_SEMAPHORE *sem = (WINPR_SEMAPHORE *)handle;
+
+ if (!SemaphoreIsHandled(handle))
+ return WAIT_FAILED;
+
+ length = read(sem->pipe_fd[0], &length, 1);
+
+ if (length != 1)
+ {
+ WLog_ERR(TAG, "semaphore read() failure [%d] %s", errno, strerror(errno));
+ return WAIT_FAILED;
+ }
+
+ return WAIT_OBJECT_0;
}
BOOL SemaphoreCloseHandle(HANDLE handle) {
HANDLE handle;
WINPR_SEMAPHORE* semaphore;
- if (pthread_once(&semaphore_initialized, SemaphoreInitialize))
- return NULL;
-
semaphore = (WINPR_SEMAPHORE*) malloc(sizeof(WINPR_SEMAPHORE));
if (!semaphore)
semaphore->pipe_fd[0] = -1;
semaphore->pipe_fd[0] = -1;
semaphore->sem = (winpr_sem_t*) NULL;
+ semaphore->cb.IsHandled = SemaphoreIsHandled;
+ semaphore->cb.CloseHandle = SemaphoreCloseHandle;
+ semaphore->cb.GetFd = SemaphoreGetFd;
+ semaphore->cb.CleanupHandle = SemaphoreCleanupHandle;
if (semaphore)
{