27 #if defined(_THREAD_SAFE) && defined(TDS_HAVE_PTHREAD_MUTEX) 31 #include <freetds/pushvis.h> 34 #define TDS_RAW_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 38 pthread_mutex_lock(mtx);
43 return pthread_mutex_trylock(mtx);
48 pthread_mutex_unlock(mtx);
53 return pthread_mutex_init(mtx, NULL);
58 pthread_mutex_destroy(mtx);
66 return pthread_cond_destroy(cond);
70 return pthread_cond_signal(cond);
74 return pthread_cond_wait(cond, mtx);
78 #define TDS_HAVE_MUTEX 1 81 typedef pthread_t tds_thread_id;
82 typedef void *(*tds_thread_proc)(
void *arg);
83 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 86 static inline int tds_thread_create(
tds_thread *ret, tds_thread_proc proc,
void *arg)
88 return pthread_create(ret, NULL, proc, arg);
91 static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
94 int ret = pthread_create(&th, NULL, proc, arg);
100 static inline int tds_thread_join(
tds_thread th,
void **ret)
102 return pthread_join(th, ret);
105 static inline tds_thread_id tds_thread_get_current_id(
void)
107 return pthread_self();
110 static inline int tds_thread_is_current(tds_thread_id th)
112 return pthread_equal(th, pthread_self());
115 #include <freetds/popvis.h> 117 #elif defined(_WIN32) 124 #define ETIMEDOUT 138 132 CRITICAL_SECTION crit;
135 #define TDS_RAW_MUTEX_INITIALIZER { NULL, 0 } 150 EnterCriticalSection(&(mtx)->crit);
152 tds_win_mutex_lock(mtx);
159 LeaveCriticalSection(&(mtx)->crit);
165 DeleteCriticalSection(&(mtx)->crit);
170 #define TDS_HAVE_MUTEX 1 173 typedef void *TDS_CONDITION_VARIABLE;
176 TDS_CONDITION_VARIABLE cv;
185 return tds_raw_cond_timedwait(cond, mtx, -1);
189 typedef DWORD tds_thread_id;
190 typedef void *(WINAPI *tds_thread_proc)(
void *arg);
191 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 192 void *WINAPI name(void *arg) 194 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc,
void *arg)
196 *ret = CreateThread(NULL, 0, (DWORD (WINAPI *)(
void*)) proc, arg, 0, NULL);
197 return *ret != NULL ? 0 : 11 ;
200 static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
202 HANDLE h = CreateThread(NULL, 0, (DWORD (WINAPI *)(
void*)) proc, arg, 0, NULL);
209 static inline int tds_thread_join(tds_thread th,
void **ret)
211 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
213 if (ret && GetExitCodeThread(th, &r))
214 *ret = (
void*) (((
char*)0) + r);
223 static inline tds_thread_id tds_thread_get_current_id(
void)
225 return GetCurrentThreadId();
228 static inline int tds_thread_is_current(tds_thread_id th)
230 return th == GetCurrentThreadId();
239 #define TDS_RAW_MUTEX_INITIALIZER {} 241 static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
245 static inline int tds_raw_mutex_trylock(tds_raw_mutex *mtx)
250 static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
254 static inline int tds_raw_mutex_init(tds_raw_mutex *mtx)
259 static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
266 static inline int tds_raw_cond_init(tds_condition *cond)
270 static inline int tds_raw_cond_destroy(tds_condition *cond)
274 #define tds_raw_cond_signal(cond) \ 275 FreeTDS_Condition_not_compiled 277 #define tds_raw_cond_wait(cond, mtx) \ 278 FreeTDS_Condition_not_compiled 280 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \ 281 FreeTDS_Condition_not_compiled 285 typedef int tds_thread_id;
287 typedef void *(*tds_thread_proc)(
void *arg);
288 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 289 void *name(void *arg) 291 #define tds_thread_create(ret, proc, arg) \ 292 FreeTDS_Thread_not_compiled 294 #define tds_thread_create_detached(proc, arg) \ 295 FreeTDS_Thread_not_compiled 297 #define tds_thread_join(th, ret) \ 298 FreeTDS_Thread_not_compiled 300 static inline tds_thread_id tds_thread_get_current_id(
void)
305 static inline int tds_thread_is_current(tds_thread_id th)
313 #ifdef TDS_HAVE_MUTEX 314 # define tds_cond_init tds_raw_cond_init 315 # define tds_cond_destroy tds_raw_cond_destroy 316 # define tds_cond_signal tds_raw_cond_signal 317 # if !ENABLE_EXTRA_CHECKS 318 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER 319 # define tds_mutex tds_raw_mutex 320 # define tds_mutex_lock tds_raw_mutex_lock 321 # define tds_mutex_trylock tds_raw_mutex_trylock 322 # define tds_mutex_unlock tds_raw_mutex_unlock 323 # define tds_mutex_check_owned(mtx) do {} while(0) 324 # define tds_mutex_init tds_raw_mutex_init 325 # define tds_mutex_free tds_raw_mutex_free 326 # define tds_cond_wait tds_raw_cond_wait 327 # define tds_cond_timedwait tds_raw_cond_timedwait 331 typedef struct tds_mutex
335 volatile tds_thread_id locked_by;
338 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 } 340 static inline void tds_mutex_lock(tds_mutex *mtx)
343 tds_raw_mutex_lock(&mtx->mtx);
344 assert(!mtx->locked);
346 mtx->locked_by = tds_thread_get_current_id();
349 static inline int tds_mutex_trylock(tds_mutex *mtx)
353 ret = tds_raw_mutex_trylock(&mtx->mtx);
355 assert(!mtx->locked);
357 mtx->locked_by = tds_thread_get_current_id();
362 static inline void tds_mutex_unlock(tds_mutex *mtx)
364 assert(mtx && mtx->locked);
366 tds_raw_mutex_unlock(&mtx->mtx);
369 static inline void tds_mutex_check_owned(tds_mutex *mtx)
373 ret = tds_raw_mutex_trylock(&mtx->mtx);
376 assert(tds_thread_is_current(mtx->locked_by));
379 static inline int tds_mutex_init(tds_mutex *mtx)
382 return tds_raw_mutex_init(&mtx->mtx);
385 static inline void tds_mutex_free(tds_mutex *mtx)
387 assert(mtx && !mtx->locked);
388 tds_raw_mutex_free(&mtx->mtx);
391 static inline int tds_cond_wait(tds_condition *cond, tds_mutex *mtx)
394 assert(mtx && mtx->locked);
396 ret = tds_raw_cond_wait(cond, &mtx->mtx);
398 mtx->locked_by = tds_thread_get_current_id();
402 static inline int tds_cond_timedwait(tds_condition *cond, tds_mutex *mtx,
int timeout_sec)
405 assert(mtx && mtx->locked);
407 ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
409 mtx->locked_by = tds_thread_get_current_id();
Definition: ptw32_MCS_lock.c:97