From 5d071ef1c9ea2d39c03ca64b6a0c3bb9c67bd8cf Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 1 Feb 2012 01:40:05 +0000 Subject: Windows portability fixes. git-svn-id: http://svn.drobilla.net/zix/trunk@58 df6676b4-ccc9-40e5-b5d6-7c4628a128e3 --- zix/sem.h | 43 ++++++++++++++++++++++++++++++++++++++++--- zix/thread.h | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 5 deletions(-) (limited to 'zix') diff --git a/zix/sem.h b/zix/sem.h index 88fe17c..48115d5 100644 --- a/zix/sem.h +++ b/zix/sem.h @@ -17,10 +17,10 @@ #ifndef ZIX_SEM_H #define ZIX_SEM_H -#include - #ifdef __APPLE__ # include +#elif defined(_WIN32) +# include #else # include #endif @@ -129,7 +129,44 @@ zix_sem_try_wait(ZixSem* sem) return semaphore_timedwait(sem->sem, zero) == KERN_SUCCESS; } -#else /* !defined(__APPLE__) */ +#elif defined(_WIN32) + +struct ZixSemImpl { + HANDLE sem; +}; + +static inline ZixStatus +zix_sem_init(ZixSem* sem, unsigned initial) +{ + sem->sem = CreateSemaphore(NULL, initial, LONG_MAX, NULL); + return (sem->sem) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; +} + +static inline void +zix_sem_destroy(ZixSem* sem) +{ + CloseHandle(sem->sem); +} + +static inline void +zix_sem_post(ZixSem* sem) +{ + ReleaseSemaphore(sem->sem, 1, NULL); +} + +static inline void +zix_sem_wait(ZixSem* sem) +{ + WaitForSingleObject(sem->sem, INFINITE); +} + +static inline bool +zix_sem_try_wait(ZixSem* sem) +{ + WaitForSingleObject(sem->sem, 0); +} + +#else /* !defined(__APPLE__) && !defined(_WIN32) */ struct ZixSemImpl { sem_t sem; diff --git a/zix/thread.h b/zix/thread.h index b1fcf97..ff5a727 100644 --- a/zix/thread.h +++ b/zix/thread.h @@ -17,8 +17,12 @@ #ifndef ZIX_THREAD_H #define ZIX_THREAD_H -#include -#include +#ifdef _WIN32 +# include +#else +# include +# include +#endif #include "zix/common.h" @@ -35,7 +39,11 @@ extern "C" { @{ */ +#ifdef _WIN32 +typedef HANDLE ZixThread; +#else typedef pthread_t ZixThread; +#endif /** Initialize @c thread to a new thread. @@ -55,6 +63,29 @@ zix_thread_create(ZixThread* thread, static inline ZixStatus zix_thread_join(ZixThread thread, void** retval); +#ifdef _WIN32 + +static inline ZixStatus +zix_thread_create(ZixThread* thread, + size_t stack_size, + void* (*function)(void*), + void* arg) +{ + *thread = CreateThread(NULL, stack_size, + (LPTHREAD_START_ROUTINE)function, arg, + 0, NULL); + return *thread ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR; +} + +static inline ZixStatus +zix_thread_join(ZixThread thread, void** retval) +{ + return WaitForSingleObject(thread, INFINITE) + ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR; +} + +#else /* !defined(_WIN32) */ + static inline ZixStatus zix_thread_create(ZixThread* thread, size_t stack_size, @@ -88,6 +119,8 @@ zix_thread_join(ZixThread thread, void** retval) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS; } +#endif + /** @} @} -- cgit v1.2.1