00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef TDSTHREAD_H
00023 #define TDSTHREAD_H 1
00024
00025
00026
00027 #undef TDS_HAVE_MUTEX
00028
00029 #if defined(_THREAD_SAFE) && defined(TDS_HAVE_PTHREAD_MUTEX)
00030
00031 #include <pthread.h>
00032
00033 #define TDS_MUTEX_DEFINE(name) pthread_mutex_t name = PTHREAD_MUTEX_INITIALIZER
00034 #define TDS_MUTEX_LOCK(mtx) do { pthread_mutex_lock(mtx); } while(0)
00035 #define TDS_MUTEX_TRYLOCK(mtx) pthread_mutex_trylock(mtx)
00036 #define TDS_MUTEX_UNLOCK(mtx) do { pthread_mutex_unlock(mtx); } while(0)
00037 #define TDS_MUTEX_DECLARE(name) pthread_mutex_t name
00038 #define TDS_MUTEX_INIT(mtx) pthread_mutex_init(mtx, NULL)
00039 #define TDS_MUTEX_FREE(mtx) do { pthread_mutex_destroy(mtx); } while(0)
00040
00041 typedef pthread_cond_t tds_condition;
00042
00043 static inline int tds_cond_init(tds_condition *cond)
00044 {
00045 return pthread_cond_init(cond, NULL);
00046 }
00047 static inline int tds_cond_destroy(tds_condition *cond)
00048 {
00049 return pthread_cond_destroy(cond);
00050 }
00051 static inline int tds_cond_signal(tds_condition *cond)
00052 {
00053 return pthread_cond_signal(cond);
00054 }
00055 static inline int tds_cond_wait(tds_condition *cond, pthread_mutex_t *mtx)
00056 {
00057 return pthread_cond_wait(cond, mtx);
00058 }
00059 #if 0
00060 int tds_cond_timedwait(tds_condition *cond, pthread_mutex_t *mtx, int milli);
00061 #endif
00062
00063 #define TDS_HAVE_MUTEX 1
00064
00065 typedef pthread_t tds_thread;
00066 typedef void *(*tds_thread_proc)(void *arg);
00067 #define TDS_THREAD_PROC_DECLARE(name, arg) \
00068 void *name(void *arg)
00069
00070 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc, void *arg)
00071 {
00072 return pthread_create(ret, NULL, proc, arg);
00073 }
00074
00075 static inline int tds_thread_join(tds_thread th, void **ret)
00076 {
00077 return pthread_join(th, ret);
00078 }
00079
00080 #elif defined(_WIN32)
00081
00082 struct ptw32_mcs_node_t_;
00083
00084 typedef struct tds_win_mutex_t_ {
00085 struct ptw32_mcs_node_t_ *lock;
00086 LONG done;
00087 CRITICAL_SECTION crit;
00088 } tds_win_mutex_t;
00089
00090 void tds_win_mutex_lock(tds_win_mutex_t *mutex);
00091 int tds_win_mutex_trylock(tds_win_mutex_t *mutex);
00092 static inline int tds_win_mutex_init(tds_win_mutex_t *mtx)
00093 {
00094 mtx->lock = NULL;
00095 mtx->done = 0;
00096 return 0;
00097 }
00098
00099
00100 #define TDS_MUTEX_DEFINE(name) tds_win_mutex_t name = { NULL, 0 }
00101 #define TDS_MUTEX_LOCK(mtx) \
00102 do { if ((mtx)->done) EnterCriticalSection(&(mtx)->crit); else tds_win_mutex_lock(mtx); } while(0)
00103 #define TDS_MUTEX_TRYLOCK(mtx) tds_win_mutex_trylock(mtx)
00104 #define TDS_MUTEX_UNLOCK(mtx) LeaveCriticalSection(&(mtx)->crit)
00105 #define TDS_MUTEX_DECLARE(name) tds_win_mutex_t name
00106 #define TDS_MUTEX_INIT(mtx) tds_win_mutex_init(mtx)
00107 #define TDS_MUTEX_FREE(mtx) do { if ((mtx)->done) { DeleteCriticalSection(&(mtx)->crit); (mtx)->done = 0; } } while(0)
00108
00109 #define TDS_HAVE_MUTEX 1
00110
00111
00112 typedef void *TDS_CONDITION_VARIABLE;
00113 typedef union {
00114 HANDLE ev;
00115 TDS_CONDITION_VARIABLE cv;
00116 } tds_condition;
00117
00118 extern int (*tds_cond_init)(tds_condition *cond);
00119 extern int (*tds_cond_destroy)(tds_condition *cond);
00120 extern int (*tds_cond_signal)(tds_condition *cond);
00121 extern int (*tds_cond_wait)(tds_condition *cond, tds_win_mutex_t *mtx);
00122
00123 typedef HANDLE tds_thread;
00124 typedef void *(WINAPI *tds_thread_proc)(void *arg);
00125 #define TDS_THREAD_PROC_DECLARE(name, arg) \
00126 void *WINAPI name(void *arg)
00127
00128 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc, void *arg)
00129 {
00130 *ret = CreateThread(NULL, 0, (DWORD_PTR (WINAPI *)(void*)) proc, arg, 0, NULL);
00131 return *ret != NULL ? 0 : 11 ;
00132 }
00133
00134 static inline int tds_thread_join(tds_thread th, void **ret)
00135 {
00136 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
00137 DWORD_PTR r;
00138 if (GetExitCodeThread(th, &r)) {
00139 *ret = (void*) r;
00140 CloseHandle(th);
00141 return 0;
00142 }
00143 }
00144 CloseHandle(th);
00145 return 22 ;
00146 }
00147
00148 #else
00149
00150
00151 #define TDS_MUTEX_DEFINE(name) int name
00152 #define TDS_MUTEX_LOCK(mtx) do { ; } while(0)
00153 #define TDS_MUTEX_TRYLOCK(mtx) 0
00154 #define TDS_MUTEX_UNLOCK(mtx) do { ; } while(0)
00155 #define TDS_MUTEX_DECLARE(name) int name
00156 #define TDS_MUTEX_INIT(mtx) 0
00157 #define TDS_MUTEX_FREE(mtx) do { ; } while(0)
00158
00159 #error Condition not supported!
00160
00161 #endif
00162
00163 #endif