From 9e00aaa5be9c92f1cee93bca3f0e473009cc3541 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Fri, 14 Jan 2011 15:17:55 +0100 Subject: [PATCH] Add simple thread and mutex wrappers. --- src/threads.h | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/threads.h diff --git a/src/threads.h b/src/threads.h new file mode 100644 index 0000000..3243907 --- /dev/null +++ b/src/threads.h @@ -0,0 +1,53 @@ +#ifndef __THREADS_H__ +#define __THREADS_H__ + +typedef struct event { + int foo; +} event_t; + + +#ifdef HAVE_MINGW +typedef HANDLE thread_t; +typedef CRITICAL_SECTION mutex_t; + +static inline bool thread_create(thread_t *tid, void (*func)(void *), void *arg) { + *tid = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, NULL); + return tid; +} +static inline void thread_destroy(thread_t *tid) { + WaitForSingleObject(tid, 0); + CloseHandle(tid); +} +static inline void mutex_create(mutex_t *mutex) { + InitializeCriticalSection(mutex); +} +static inline void mutex_lock(mutex_t *mutex) { + EnterCriticalSection(mutex); +} +static inline void mutex_unlock(mutex_t *mutex) { + LeaveCriticalSection(mutex); +} +#else +#include + +typedef pthread_t thread_t; +typedef pthread_mutex_t mutex_t; + +static inline void thread_create(thread_t *tid, void (*func)(void *), void *arg) { + return !pthread_create(tid, NULL, (void *(*)(void *))func, arg); +} +static inline void thread_destroy(thread_t *tid) { + pthread_join(tid); +} +static inline void mutex_create(mutex_t *mutex) { + pthread_mutex_init(mutex, NULL); +} +static inline void mutex_lock(mutex_t *mutex) { + pthread_mutex_lock(mutex); +} +static inline void mutex_unlock(mutex_t *mutex) { + pthread_mutex_unlock(mutex); +} +#endif + +#endif -- 2.25.1