From 822199040a310eba449ba60cc485182669e29291 Mon Sep 17 00:00:00 2001 From: Vic Lee Date: Fri, 8 Jul 2011 11:25:51 +0800 Subject: [PATCH] utils/semaphore: abstract type definition and add win32 support. --- include/freerdp/utils/semaphore.h | 9 ++-- libfreerdp-utils/semaphore.c | 81 +++++++++++++++++++++++-------- 2 files changed, 66 insertions(+), 24 deletions(-) diff --git a/include/freerdp/utils/semaphore.h b/include/freerdp/utils/semaphore.h index cdb6da3eb..33f693354 100644 --- a/include/freerdp/utils/semaphore.h +++ b/include/freerdp/utils/semaphore.h @@ -20,8 +20,11 @@ #ifndef __SEMAPHORE_UTILS_H #define __SEMAPHORE_UTILS_H -void freerdp_sem_create(void * sem_struct, int iv); -void freerdp_sem_signal(void * sem_struct); -void freerdp_sem_wait(void * sem_struct); +typedef void* freerdp_sem; + +freerdp_sem freerdp_sem_new(int iv); +void freerdp_sem_free(freerdp_sem sem); +void freerdp_sem_signal(freerdp_sem sem); +void freerdp_sem_wait(freerdp_sem sem); #endif /* __SEMAPHORE_UTILS_H */ diff --git a/libfreerdp-utils/semaphore.c b/libfreerdp-utils/semaphore.c index 6effcc1c4..b898bc5ea 100644 --- a/libfreerdp-utils/semaphore.c +++ b/libfreerdp-utils/semaphore.c @@ -17,41 +17,80 @@ * limitations under the License. */ -#include -#include +#include #include -#ifdef __APPLE__ +#if defined __APPLE__ + +#include +#include #include #include #include +#define freerdp_sem_t semaphore_t + +#elif defined _WIN32 + +#include +#define freerdp_sem_t HANDLE + +#else + +#include +#include +#define freerdp_sem_t sem_t + #endif -void freerdp_sem_create(void * sem_struct, int iv) +freerdp_sem freerdp_sem_new(int iv) { -#ifdef __APPLE__ - semaphore_create(mach_task_self(), (semaphore_t *)sem_struct, SYNC_POLICY_FIFO, iv); + freerdp_sem_t* sem; + + sem = (freerdp_sem_t*)xmalloc(sizeof(freerdp_sem_t)); + +#if defined __APPLE__ + semaphore_create(mach_task_self(), sem, SYNC_POLICY_FIFO, iv); +#elif defined _WIN32 + *sem = CreateSemaphore(NULL, 0, iv, NULL); #else - int pshared = 0; - sem_init((sem_t *)sem_struct, pshared, iv); + sem_init(sem, 0, iv); +#endif + + return sem; +} + +void freerdp_sem_free(freerdp_sem sem) +{ +#if defined __APPLE__ + semaphore_destroy(mach_task_self(), *((freerdp_sem_t*)sem)); +#elif defined _WIN32 + CloseHandle(*((freerdp_sem_t*)sem)); +#else + sem_destroy((freerdp_sem_t*)sem); +#endif + + xfree(sem); +} + +void freerdp_sem_signal(freerdp_sem sem) +{ +#if defined __APPLE__ + semaphore_signal(*((freerdp_sem_t*)sem)); +#elif defined _WIN32 + ReleaseSemaphore(*((freerdp_sem_t*)sem), 1, NULL); +#else + sem_post((freerdp_sem_t*)sem); #endif } -void freerdp_sem_signal(void * sem_struct) +void freerdp_sem_wait(freerdp_sem sem) { -#ifdef __APPLE__ - semaphore_signal(*((semaphore_t *)sem_struct)); +#if defined __APPLE__ + semaphore_wait(*((freerdp_sem_t*)sem)); +#elif defined _WIN32 + WaitForSingleObject(*((freerdp_sem_t*)sem), INFINITE); #else - sem_post((sem_t *)sem_struct); -#endif -} - -void freerdp_sem_wait(void * sem_struct) -{ -#ifdef __APPLE__ - semaphore_wait(*((semaphore_t *)sem_struct)); -#else - sem_wait((sem_t *)sem_struct); + sem_wait((freerdp_sem_t*)sem); #endif }