1 /* src/interfaces/ecpg/include/ecpg-pthread-win32.h */
2 /*
3  * pthread mapping macros for win32 native thread implementation
4  */
5 #ifndef _ECPG_PTHREAD_WIN32_H
6 #define _ECPG_PTHREAD_WIN32_H
7 
8 #ifdef ENABLE_THREAD_SAFETY
9 
10 #ifndef WIN32
11 
12 #include <pthread.h>
13 #else
14 
15 typedef struct pthread_mutex_t
16 {
17 	HANDLE		handle;
18 	LONG		initlock;
19 } pthread_mutex_t;
20 
21 typedef DWORD pthread_key_t;
22 typedef bool pthread_once_t;
23 
24 #define PTHREAD_MUTEX_INITIALIZER	{ NULL, 0 }
25 #define PTHREAD_ONCE_INIT			false
26 
27 void		win32_pthread_mutex(volatile pthread_mutex_t *mutex);
28 void		win32_pthread_once(volatile pthread_once_t *once, void (*fn) (void));
29 
30 #define pthread_mutex_lock(mutex) \
31 	do { \
32 		if ((mutex)->handle == NULL) \
33 			win32_pthread_mutex((mutex)); \
34 		WaitForSingleObject((mutex)->handle, INFINITE); \
35 	} while(0)
36 
37 #define pthread_mutex_unlock(mutex) \
38 	ReleaseMutex((mutex)->handle)
39 
40 #define pthread_getspecific(key) \
41 	TlsGetValue((key))
42 
43 #define pthread_setspecific(key, value) \
44 	TlsSetValue((key), (value))
45 
46 /* FIXME: destructor is never called in Win32. */
47 #define pthread_key_create(key, destructor) \
48 	do { *(key) = TlsAlloc(); ((void)(destructor)); } while(0)
49 
50 #define pthread_once(once, fn) \
51 	do { \
52 		if (!*(once)) \
53 			win32_pthread_once((once), (fn)); \
54 	} while(0)
55 #endif							/* WIN32 */
56 #endif							/* ENABLE_THREAD_SAFETY */
57 
58 #endif							/* _ECPG_PTHREAD_WIN32_H */
59