Header And Logo

PostgreSQL
| The world's most advanced open source database.

ecpg-pthread-win32.h

Go to the documentation of this file.
00001 /* src/interfaces/ecpg/include/ecpg-pthread-win32.h */
00002 /*
00003  * pthread mapping macros for win32 native thread implementation
00004  */
00005 #ifndef _ECPG_PTHREAD_WIN32_H
00006 #define _ECPG_PTHREAD_WIN32_H
00007 
00008 #ifdef ENABLE_THREAD_SAFETY
00009 
00010 #ifndef WIN32
00011 
00012 #include <pthread.h>
00013 #else
00014 
00015 typedef struct pthread_mutex_t
00016 {
00017     HANDLE      handle;
00018     LONG        initlock;
00019 } pthread_mutex_t;
00020 
00021 typedef DWORD pthread_key_t;
00022 typedef bool pthread_once_t;
00023 
00024 #define PTHREAD_MUTEX_INITIALIZER   { NULL, 0 }
00025 #define PTHREAD_ONCE_INIT           false
00026 
00027 void        win32_pthread_mutex(volatile pthread_mutex_t *mutex);
00028 void        win32_pthread_once(volatile pthread_once_t *once, void (*fn) (void));
00029 
00030 #define pthread_mutex_lock(mutex) \
00031     do { \
00032         if ((mutex)->handle == NULL) \
00033             win32_pthread_mutex((mutex)); \
00034         WaitForSingleObject((mutex)->handle, INFINITE); \
00035     } while(0)
00036 
00037 #define pthread_mutex_unlock(mutex) \
00038     ReleaseMutex((mutex)->handle)
00039 
00040 #define pthread_getspecific(key) \
00041     TlsGetValue((key))
00042 
00043 #define pthread_setspecific(key, value) \
00044     TlsSetValue((key), (value))
00045 
00046 /* FIXME: destructor is never called in Win32. */
00047 #define pthread_key_create(key, destructor) \
00048     do { *(key) = TlsAlloc(); ((void)(destructor)); } while(0)
00049 
00050 #define pthread_once(once, fn) \
00051     do { \
00052         if (!*(once)) \
00053             win32_pthread_once((once), (fn)); \
00054     } while(0)
00055 #endif   /* WIN32 */
00056 #endif   /* ENABLE_THREAD_SAFETY */
00057 
00058 #endif   /* _ECPG_PTHREAD_WIN32_H */