summaryrefslogtreecommitdiffstats
path: root/zix
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2012-02-01 01:40:05 +0000
committerDavid Robillard <d@drobilla.net>2012-02-01 01:40:05 +0000
commit5d071ef1c9ea2d39c03ca64b6a0c3bb9c67bd8cf (patch)
tree6b2d088aef4ed5099fbbc140c594a0ee1b7e67fa /zix
parentdc7628a8eec281add045eb123ae06dbc94c1b950 (diff)
downloadzix-5d071ef1c9ea2d39c03ca64b6a0c3bb9c67bd8cf.tar.gz
zix-5d071ef1c9ea2d39c03ca64b6a0c3bb9c67bd8cf.tar.bz2
zix-5d071ef1c9ea2d39c03ca64b6a0c3bb9c67bd8cf.zip
Windows portability fixes.
git-svn-id: http://svn.drobilla.net/zix/trunk@58 df6676b4-ccc9-40e5-b5d6-7c4628a128e3
Diffstat (limited to 'zix')
-rw-r--r--zix/sem.h43
-rw-r--r--zix/thread.h37
2 files changed, 75 insertions, 5 deletions
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 <stdbool.h>
-
#ifdef __APPLE__
# include <mach/mach.h>
+#elif defined(_WIN32)
+# include <windows.h>
#else
# include <semaphore.h>
#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 <errno.h>
-#include <pthread.h>
+#ifdef _WIN32
+# include <windows.h>
+#else
+# include <errno.h>
+# include <pthread.h>
+#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
+
/**
@}
@}