gwion-util
utilities for the Gwion project
Loading...
Searching...
No Matches
gwion_thread.h
Go to the documentation of this file.
1
4#ifndef __GWION_THREAD
5#define __GWION_THREAD
6#ifdef BUILD_ON_WINDOWS
7#include <windows.h>
8#define THREAD_RETTYPE long unsigned int
9#define THREAD_FUNC(a) THREAD_RETTYPE(a)(void *data)
10#define THREAD_TYPE HANDLE
11#define THREAD_CREATE(thread, func, arg) \
12 thread = CreateThread(NULL, 0, func, arg, 0, NULL)
13#define THREAD_JOIN(thread) WaitForSingleObject(thread, 0)
14#define THREAD_RETURN(arg) return 0;
15#define THREAD_DETACH(arg)
16
17#define MUTEX_TYPE HANDLE
18#define MUTEX_INITIALIZER NULL
19#define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL)
20#define MUTEX_CLEANUP(x) CloseHandle(x)
21#define MUTEX_LOCK(x) emulate_pthread_mutex_lock(&(x))
22#define MUTEX_COND_LOCK(x)
23//#define MUTEX_UNLOCK(x) (ReleaseMutex((x)) == 0)
24#define MUTEX_UNLOCK(x) ReleaseMutex((x))
25#define MUTEX_COND_UNLOCK(x) (void)x
26int emulate_pthread_mutex_lock(volatile MUTEX_TYPE *mx);
27
28#define THREAD_COND_TYPE HANDLE
29#define THREAD_COND_SETUP(x) x = CreateEvent(NULL, FALSE, FALSE, NULL);
30#define THREAD_COND_WAIT(x, mutex) WaitForSingleObject(x, INFINITE)
31#define THREAD_COND_SIGNAL(x) SetEvent(x);
32#define THREAD_COND_BROADCAST(x) SetEvent(x);
33#define THREAD_COND_CLEANUP(x) CloseHandle(x)
34#else
35#include <pthread.h>
36
37#define THREAD_RETTYPE void *
38#define THREAD_FUNC(a) THREAD_RETTYPE(a)(void *data)
39#define THREAD_TYPE pthread_t
40#define THREAD_CREATE(thread, func, arg) \
41 pthread_create(&thread, NULL, func, arg)
42#define THREAD_JOIN(thread) pthread_join(thread, NULL)
43#define THREAD_RETURN(arg) \
44 { \
45 pthread_exit(NULL); \
46 return NULL; \
47 }
48#define THREAD_DETACH(arg) pthread_detach(arg)
49
50#define MUTEX_TYPE pthread_mutex_t *
51#define MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
52#define MUTEX_SETUP(x) \
53 { \
54 pthread_mutexattr_t attr; \
55 pthread_mutexattr_init(&attr); \
56 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); \
57 x = (pthread_mutex_t *)xmalloc(sizeof(pthread_mutex_t)); \
58 pthread_mutex_init(x, &attr); \
59 }
60#define MUTEX_CLEANUP(x) \
61 { \
62 pthread_mutex_destroy((x)); \
63 xfree(x); \
64 }
65#define MUTEX_LOCK(x) pthread_mutex_lock((x))
66#define MUTEX_COND_LOCK(x) pthread_mutex_lock((x))
67#define MUTEX_UNLOCK(x) pthread_mutex_unlock((x))
68#define MUTEX_COND_UNLOCK(x) pthread_mutex_unlock((x))
69
70#define THREAD_COND_TYPE pthread_cond_t*
71#define THREAD_COND_SETUP(x) \
72 { \
73 x = (pthread_cond_t *)xcalloc(1, sizeof(pthread_cond_t)); \
74 pthread_cond_init(x, NULL); \
75 }
76#define THREAD_COND_WAIT(x, mutex) pthread_cond_wait(x, mutex)
77#define THREAD_COND_SIGNAL(x) pthread_cond_signal(x)
78#define THREAD_COND_BROADCAST(x) pthread_cond_broadcast(x)
79#define THREAD_COND_CLEANUP(x) \
80 { \
81 pthread_cond_destroy(x); \
82 xfree(x); \
83 }
84
85#endif
86
87#define STR_EXPAND(tok) #tok
88#define STR(tok) STR_EXPAND(tok)
89#ifndef BUILD_ON_WINDOWS
90#include <dlfcn.h>
91#define DLOPEN(dl, b) dlopen(dl, b)
92#define DLSYM(dl, t, a) (t)(intptr_t) dlsym(dl, a)
93#define DLCLOSE(dl) dlclose(dl)
94#define DLERROR() dlerror()
95#else
96#include "windows.h"
97#define DLOPEN(dl, b) LoadLibrary(dl)
98#define DLSYM(dl, t, a) (t)(intptr_t) GetProcAddress(dl, a)
99#define DLCLOSE(dl) FreeLibrary(dl)
100#define DLERROR() "plugin"
101#endif
102#endif
#define MUTEX_TYPE