Add simple thread and mutex wrappers.
[tinc] / src / threads.h
1 #ifndef __THREADS_H__
2 #define __THREADS_H__
3
4 typedef struct event {
5         int foo;
6 } event_t;
7         
8
9 #ifdef HAVE_MINGW
10 typedef HANDLE thread_t;
11 typedef CRITICAL_SECTION mutex_t;
12
13 static inline bool thread_create(thread_t *tid, void (*func)(void *), void *arg) {
14         *tid = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, NULL);
15         return tid;
16 }
17 static inline void thread_destroy(thread_t *tid) {
18         WaitForSingleObject(tid, 0);
19         CloseHandle(tid);
20 }
21 static inline void mutex_create(mutex_t *mutex) {
22         InitializeCriticalSection(mutex);
23 }
24 static inline void mutex_lock(mutex_t *mutex) {
25         EnterCriticalSection(mutex);
26 }
27 static inline void mutex_unlock(mutex_t *mutex) {
28         LeaveCriticalSection(mutex);
29 }
30 #else
31 #include <pthread.h>
32
33 typedef pthread_t thread_t;
34 typedef pthread_mutex_t mutex_t;
35
36 static inline void thread_create(thread_t *tid, void (*func)(void *), void *arg) {
37         return !pthread_create(tid, NULL, (void *(*)(void *))func, arg);
38 }
39 static inline void thread_destroy(thread_t *tid) {
40         pthread_join(tid);
41 }
42 static inline void mutex_create(mutex_t *mutex) {
43         pthread_mutex_init(mutex, NULL);
44 }
45 static inline void mutex_lock(mutex_t *mutex) {
46         pthread_mutex_lock(mutex);
47 }
48 static inline void mutex_unlock(mutex_t *mutex) {
49         pthread_mutex_unlock(mutex);
50 }
51 #endif
52
53 #endif