PostgreSQL Source Code  git master
ecpg-pthread-win32.h
Go to the documentation of this file.
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 #ifndef WIN32
9 
10 #include <pthread.h>
11 #else
12 
13 typedef struct pthread_mutex_t
14 {
15  HANDLE handle;
16  LONG initlock;
18 
19 typedef DWORD pthread_key_t;
20 typedef bool pthread_once_t;
21 
22 #define PTHREAD_MUTEX_INITIALIZER { NULL, 0 }
23 #define PTHREAD_ONCE_INIT false
24 
25 void win32_pthread_mutex(volatile pthread_mutex_t *mutex);
26 void win32_pthread_once(volatile pthread_once_t *once, void (*fn) (void));
27 
28 #define pthread_mutex_lock(mutex) \
29  do { \
30  if ((mutex)->handle == NULL) \
31  win32_pthread_mutex((mutex)); \
32  WaitForSingleObject((mutex)->handle, INFINITE); \
33  } while(0)
34 
35 #define pthread_mutex_unlock(mutex) \
36  ReleaseMutex((mutex)->handle)
37 
38 #define pthread_getspecific(key) \
39  TlsGetValue((key))
40 
41 #define pthread_setspecific(key, value) \
42  TlsSetValue((key), (value))
43 
44 /* FIXME: destructor is never called in Win32. */
45 #define pthread_key_create(key, destructor) \
46  do { *(key) = TlsAlloc(); ((void)(destructor)); } while(0)
47 
48 #define pthread_once(once, fn) \
49  do { \
50  if (!*(once)) \
51  win32_pthread_once((once), (fn)); \
52  } while(0)
53 #endif /* WIN32 */
54 
55 #endif /* _ECPG_PTHREAD_WIN32_H */
ULONG pthread_key_t
Definition: pthread-win32.h:7
int pthread_once_t
Definition: pthread-win32.h:9
CRITICAL_SECTION * pthread_mutex_t
Definition: pthread-win32.h:8
static void * fn(void *arg)
Definition: thread-alloc.c:119