27 #if defined(_THREAD_SAFE) && defined(TDS_HAVE_PTHREAD_MUTEX) 29 #include <tds_sysdep_public.h> 33 #include <freetds/pushvis.h> 36 #define TDS_RAW_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 40 pthread_mutex_lock(mtx);
45 return pthread_mutex_trylock(mtx);
50 pthread_mutex_unlock(mtx);
55 return pthread_mutex_init(mtx, NULL);
60 pthread_mutex_destroy(mtx);
68 return pthread_cond_destroy(cond);
72 return pthread_cond_signal(cond);
76 return pthread_cond_wait(cond, mtx);
80 #define TDS_HAVE_MUTEX 1 83 typedef pthread_t tds_thread_id;
84 typedef void *(*tds_thread_proc)(
void *arg);
85 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 87 #define TDS_THREAD_RESULT(n) ((void*)(intptr_t)(n)) 89 static inline int tds_thread_create(
tds_thread *ret, tds_thread_proc proc,
void *arg)
91 return pthread_create(ret, NULL, proc, arg);
94 static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
97 int ret = pthread_create(&th, NULL, proc, arg);
103 static inline int tds_thread_join(
tds_thread th,
void **ret)
105 return pthread_join(th, ret);
108 static inline tds_thread_id tds_thread_get_current_id(
void)
110 return pthread_self();
113 static inline int tds_thread_is_current(tds_thread_id th)
115 return pthread_equal(th, pthread_self());
118 #include <freetds/popvis.h> 120 #elif defined(_WIN32) 122 #include <freetds/windows.h> 127 #define ETIMEDOUT 138 136 CRITICAL_SECTION crit;
139 #define TDS_RAW_MUTEX_INITIALIZER { NULL, 0, 0 } 155 EnterCriticalSection(&mtx->crit);
156 mtx->thread_id = GetCurrentThreadId();
158 tds_win_mutex_lock(mtx);
167 LeaveCriticalSection(&mtx->crit);
173 DeleteCriticalSection(&mtx->crit);
178 #define TDS_HAVE_MUTEX 1 181 typedef void *TDS_CONDITION_VARIABLE;
184 TDS_CONDITION_VARIABLE cv;
193 return tds_raw_cond_timedwait(cond, mtx, -1);
197 typedef DWORD tds_thread_id;
198 typedef DWORD (WINAPI *tds_thread_proc)(
void *arg);
199 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 200 DWORD WINAPI name(void *arg) 201 #define TDS_THREAD_RESULT(n) ((DWORD)(int)(n)) 203 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc,
void *arg)
205 *ret = CreateThread(NULL, 0, proc, arg, 0, NULL);
206 return *ret != NULL ? 0 : 11 ;
209 static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
211 HANDLE h = CreateThread(NULL, 0, proc, arg, 0, NULL);
218 static inline int tds_thread_join(tds_thread th,
void **ret)
220 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
222 if (ret && GetExitCodeThread(th, &r))
223 *ret = (
void*) (((
char*)0) + r);
232 static inline tds_thread_id tds_thread_get_current_id(
void)
234 return GetCurrentThreadId();
237 static inline int tds_thread_is_current(tds_thread_id th)
239 return th == GetCurrentThreadId();
244 #include <tds_sysdep_public.h> 250 #define TDS_RAW_MUTEX_INITIALIZER {} 252 static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
256 static inline int tds_raw_mutex_trylock(tds_raw_mutex *mtx)
261 static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
265 static inline int tds_raw_mutex_init(tds_raw_mutex *mtx)
270 static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
277 static inline int tds_raw_cond_init(tds_condition *cond)
281 static inline int tds_raw_cond_destroy(tds_condition *cond)
285 #define tds_raw_cond_signal(cond) \ 286 FreeTDS_Condition_not_compiled 288 #define tds_raw_cond_wait(cond, mtx) \ 289 FreeTDS_Condition_not_compiled 291 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \ 292 FreeTDS_Condition_not_compiled 296 typedef int tds_thread_id;
298 typedef void *(*tds_thread_proc)(
void *arg);
299 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 300 void *name(void *arg) 301 #define TDS_THREAD_RESULT(n) ((void*)(intptr_t)(n)) 303 #define tds_thread_create(ret, proc, arg) \ 304 FreeTDS_Thread_not_compiled 306 #define tds_thread_create_detached(proc, arg) \ 307 FreeTDS_Thread_not_compiled 309 #define tds_thread_join(th, ret) \ 310 FreeTDS_Thread_not_compiled 312 static inline tds_thread_id tds_thread_get_current_id(
void)
317 static inline int tds_thread_is_current(tds_thread_id th)
325 #ifdef TDS_HAVE_MUTEX 326 # define tds_cond_init tds_raw_cond_init 327 # define tds_cond_destroy tds_raw_cond_destroy 328 # define tds_cond_signal tds_raw_cond_signal 329 # if !ENABLE_EXTRA_CHECKS 330 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER 331 # define tds_mutex tds_raw_mutex 332 # define tds_mutex_lock tds_raw_mutex_lock 333 # define tds_mutex_trylock tds_raw_mutex_trylock 334 # define tds_mutex_unlock tds_raw_mutex_unlock 335 # define tds_mutex_check_owned(mtx) do {} while(0) 336 # define tds_mutex_init tds_raw_mutex_init 337 # define tds_mutex_free tds_raw_mutex_free 338 # define tds_cond_wait tds_raw_cond_wait 339 # define tds_cond_timedwait tds_raw_cond_timedwait 343 typedef struct tds_mutex
347 volatile tds_thread_id locked_by;
350 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 } 352 static inline void tds_mutex_lock(tds_mutex *mtx)
355 tds_raw_mutex_lock(&mtx->mtx);
356 assert(!mtx->locked);
358 mtx->locked_by = tds_thread_get_current_id();
361 static inline int tds_mutex_trylock(tds_mutex *mtx)
365 ret = tds_raw_mutex_trylock(&mtx->mtx);
367 assert(!mtx->locked);
369 mtx->locked_by = tds_thread_get_current_id();
374 static inline void tds_mutex_unlock(tds_mutex *mtx)
376 assert(mtx && mtx->locked);
378 tds_raw_mutex_unlock(&mtx->mtx);
381 static inline void tds_mutex_check_owned(tds_mutex *mtx)
385 ret = tds_raw_mutex_trylock(&mtx->mtx);
388 assert(tds_thread_is_current(mtx->locked_by));
391 static inline int tds_mutex_init(tds_mutex *mtx)
394 return tds_raw_mutex_init(&mtx->mtx);
397 static inline void tds_mutex_free(tds_mutex *mtx)
399 assert(mtx && !mtx->locked);
400 tds_raw_mutex_free(&mtx->mtx);
403 static inline int tds_cond_wait(tds_condition *cond, tds_mutex *mtx)
406 assert(mtx && mtx->locked);
408 ret = tds_raw_cond_wait(cond, &mtx->mtx);
410 mtx->locked_by = tds_thread_get_current_id();
414 static inline int tds_cond_timedwait(tds_condition *cond, tds_mutex *mtx,
int timeout_sec)
417 assert(mtx && mtx->locked);
419 ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
421 mtx->locked_by = tds_thread_get_current_id();
Definition: ptw32_MCS_lock.c:97