22 #ifndef _tdsguard_cIfZP7JZiHtLLfanwl7ubP_ 23 #define _tdsguard_cIfZP7JZiHtLLfanwl7ubP_ 27 #if defined(_THREAD_SAFE) && defined(TDS_HAVE_PTHREAD_MUTEX) 29 #include <tds_sysdep_public.h> 30 #include <freetds/sysdep_private.h> 34 #include <freetds/pushvis.h> 37 #define TDS_RAW_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 41 pthread_mutex_lock(mtx);
46 return pthread_mutex_trylock(mtx);
51 pthread_mutex_unlock(mtx);
56 return pthread_mutex_init(mtx, NULL);
61 pthread_mutex_destroy(mtx);
69 return pthread_cond_destroy(cond);
73 return pthread_cond_signal(cond);
77 return pthread_cond_wait(cond, mtx);
81 #define TDS_HAVE_MUTEX 1 84 typedef pthread_t tds_thread_id;
85 typedef void *(*tds_thread_proc)(
void *arg);
86 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 88 #define TDS_THREAD_RESULT(n) ((void*)(TDS_INTPTR)(n)) 90 static inline int tds_thread_create(
tds_thread *ret, tds_thread_proc proc,
void *arg)
92 return pthread_create(ret, NULL, proc, arg);
95 static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
98 int ret = pthread_create(&th, NULL, proc, arg);
104 static inline int tds_thread_join(
tds_thread th,
void **ret)
106 return pthread_join(th, ret);
109 static inline tds_thread_id tds_thread_get_current_id(
void)
111 return pthread_self();
114 static inline int tds_thread_is_current(tds_thread_id th)
116 return pthread_equal(th, pthread_self());
119 #include <freetds/popvis.h> 121 #elif defined(_WIN32) 123 #include <freetds/windows.h> 128 #define ETIMEDOUT 138 137 CRITICAL_SECTION crit;
140 #define TDS_RAW_MUTEX_INITIALIZER { NULL, 0, 0 } 156 EnterCriticalSection(&mtx->crit);
157 mtx->thread_id = GetCurrentThreadId();
159 tds_win_mutex_lock(mtx);
168 LeaveCriticalSection(&mtx->crit);
174 DeleteCriticalSection(&mtx->crit);
179 #define TDS_HAVE_MUTEX 1 182 typedef void *TDS_CONDITION_VARIABLE;
185 TDS_CONDITION_VARIABLE cv;
194 return tds_raw_cond_timedwait(cond, mtx, -1);
198 typedef DWORD tds_thread_id;
199 typedef DWORD (WINAPI *tds_thread_proc)(
void *arg);
200 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 201 DWORD WINAPI name(void *arg) 202 #define TDS_THREAD_RESULT(n) ((DWORD)(int)(n)) 204 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc,
void *arg)
206 *ret = CreateThread(NULL, 0, proc, arg, 0, NULL);
207 return *ret != NULL ? 0 : 11 ;
210 static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
212 HANDLE h = CreateThread(NULL, 0, proc, arg, 0, NULL);
219 static inline int tds_thread_join(tds_thread th,
void **ret)
221 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
224 if (!GetExitCodeThread(th, &r))
226 *ret = (
void*) (((
char*)0) + r);
236 static inline tds_thread_id tds_thread_get_current_id(
void)
238 return GetCurrentThreadId();
241 static inline int tds_thread_is_current(tds_thread_id th)
243 return th == GetCurrentThreadId();
248 #include <tds_sysdep_public.h> 255 #define TDS_RAW_MUTEX_INITIALIZER {} 291 #define tds_raw_cond_signal(cond) \ 292 FreeTDS_Condition_not_compiled 294 #define tds_raw_cond_wait(cond, mtx) \ 295 FreeTDS_Condition_not_compiled 297 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \ 298 FreeTDS_Condition_not_compiled 303 typedef int tds_thread_id;
305 typedef void *(*tds_thread_proc)(
void *arg);
306 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 307 void *name(void *arg) 308 #define TDS_THREAD_RESULT(n) ((void*)(TDS_INTPTR)(n)) 310 #define tds_thread_create(ret, proc, arg) \ 311 FreeTDS_Thread_not_compiled 313 #define tds_thread_create_detached(proc, arg) \ 314 FreeTDS_Thread_not_compiled 316 #define tds_thread_join(th, ret) \ 317 FreeTDS_Thread_not_compiled 319 static inline tds_thread_id tds_thread_get_current_id(
void)
324 static inline int tds_thread_is_current(tds_thread_id th)
331 # define tds_cond_init tds_raw_cond_init 332 # define tds_cond_destroy tds_raw_cond_destroy 333 # define tds_cond_signal tds_raw_cond_signal 334 # if !ENABLE_EXTRA_CHECKS || !defined(TDS_HAVE_MUTEX) 335 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER 336 # define tds_mutex tds_raw_mutex 337 # define tds_mutex_lock tds_raw_mutex_lock 338 # define tds_mutex_trylock tds_raw_mutex_trylock 339 # define tds_mutex_unlock tds_raw_mutex_unlock 340 # define tds_mutex_check_owned(mtx) do {} while(0) 341 # define tds_mutex_init tds_raw_mutex_init 342 # define tds_mutex_free tds_raw_mutex_free 343 # define tds_cond_wait tds_raw_cond_wait 344 # define tds_cond_timedwait tds_raw_cond_timedwait 348 typedef struct tds_mutex
352 volatile tds_thread_id locked_by;
355 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 } 357 static inline void tds_mutex_lock(tds_mutex *mtx)
360 tds_raw_mutex_lock(&mtx->mtx);
361 assert(!mtx->locked);
363 mtx->locked_by = tds_thread_get_current_id();
366 static inline int tds_mutex_trylock(tds_mutex *mtx)
370 ret = tds_raw_mutex_trylock(&mtx->mtx);
372 assert(!mtx->locked);
374 mtx->locked_by = tds_thread_get_current_id();
379 static inline void tds_mutex_unlock(tds_mutex *mtx)
381 assert(mtx && mtx->locked);
383 tds_raw_mutex_unlock(&mtx->mtx);
386 static inline void tds_mutex_check_owned(tds_mutex *mtx)
390 ret = tds_raw_mutex_trylock(&mtx->mtx);
393 assert(tds_thread_is_current(mtx->locked_by));
396 static inline int tds_mutex_init(tds_mutex *mtx)
399 return tds_raw_mutex_init(&mtx->mtx);
402 static inline void tds_mutex_free(tds_mutex *mtx)
404 assert(mtx && !mtx->locked);
405 tds_raw_mutex_free(&mtx->mtx);
408 static inline int tds_cond_wait(
tds_condition *cond, tds_mutex *mtx)
411 assert(mtx && mtx->locked);
413 ret = tds_raw_cond_wait(cond, &mtx->mtx);
415 mtx->locked_by = tds_thread_get_current_id();
419 static inline int tds_cond_timedwait(
tds_condition *cond, tds_mutex *mtx,
int timeout_sec)
422 assert(mtx && mtx->locked);
424 ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
426 mtx->locked_by = tds_thread_get_current_id();
Definition: ptw32_MCS_lock.c:97