27 #if defined(_THREAD_SAFE) && defined(TDS_HAVE_PTHREAD_MUTEX) 32 #include <freetds/pushvis.h> 35 #define TDS_RAW_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 39 pthread_mutex_lock(mtx);
44 return pthread_mutex_trylock(mtx);
49 pthread_mutex_unlock(mtx);
54 return pthread_mutex_init(mtx, NULL);
59 pthread_mutex_destroy(mtx);
67 return pthread_cond_destroy(cond);
71 return pthread_cond_signal(cond);
75 return pthread_cond_wait(cond, mtx);
79 #define TDS_HAVE_MUTEX 1 82 typedef pthread_t tds_thread_id;
83 typedef void *(*tds_thread_proc)(
void *arg);
84 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 87 static inline int tds_thread_create(
tds_thread *ret, tds_thread_proc proc,
void *arg)
89 return pthread_create(ret, NULL, proc, arg);
92 static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
95 int ret = pthread_create(&th, NULL, proc, arg);
101 static inline int tds_thread_join(
tds_thread th,
void **ret)
103 return pthread_join(th, ret);
106 static inline tds_thread_id tds_thread_get_current_id(
void)
108 return pthread_self();
111 static inline int tds_thread_is_current(tds_thread_id th)
113 return pthread_equal(th, pthread_self());
116 #include <freetds/popvis.h> 118 #elif defined(_WIN32) 125 #define ETIMEDOUT 138 133 CRITICAL_SECTION crit;
136 #define TDS_RAW_MUTEX_INITIALIZER { NULL, 0 } 151 EnterCriticalSection(&(mtx)->crit);
153 tds_win_mutex_lock(mtx);
160 LeaveCriticalSection(&(mtx)->crit);
166 DeleteCriticalSection(&(mtx)->crit);
171 #define TDS_HAVE_MUTEX 1 174 typedef void *TDS_CONDITION_VARIABLE;
177 TDS_CONDITION_VARIABLE cv;
186 return tds_raw_cond_timedwait(cond, mtx, -1);
190 typedef DWORD tds_thread_id;
191 typedef void *(WINAPI *tds_thread_proc)(
void *arg);
192 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 193 void *WINAPI name(void *arg) 195 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc,
void *arg)
197 *ret = CreateThread(NULL, 0, (DWORD (WINAPI *)(
void*)) proc, arg, 0, NULL);
198 return *ret != NULL ? 0 : 11 ;
201 static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
203 HANDLE h = CreateThread(NULL, 0, (DWORD (WINAPI *)(
void*)) proc, arg, 0, NULL);
210 static inline int tds_thread_join(tds_thread th,
void **ret)
212 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
214 if (ret && GetExitCodeThread(th, &r))
215 *ret = (
void*) (((
char*)0) + r);
224 static inline tds_thread_id tds_thread_get_current_id(
void)
226 return GetCurrentThreadId();
229 static inline int tds_thread_is_current(tds_thread_id th)
231 return th == GetCurrentThreadId();
240 #define TDS_RAW_MUTEX_INITIALIZER {} 242 static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
246 static inline int tds_raw_mutex_trylock(tds_raw_mutex *mtx)
251 static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
255 static inline int tds_raw_mutex_init(tds_raw_mutex *mtx)
260 static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
267 static inline int tds_raw_cond_init(tds_condition *cond)
271 static inline int tds_raw_cond_destroy(tds_condition *cond)
275 #define tds_raw_cond_signal(cond) \ 276 FreeTDS_Condition_not_compiled 278 #define tds_raw_cond_wait(cond, mtx) \ 279 FreeTDS_Condition_not_compiled 281 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \ 282 FreeTDS_Condition_not_compiled 286 typedef int tds_thread_id;
288 typedef void *(*tds_thread_proc)(
void *arg);
289 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 290 void *name(void *arg) 292 #define tds_thread_create(ret, proc, arg) \ 293 FreeTDS_Thread_not_compiled 295 #define tds_thread_create_detached(proc, arg) \ 296 FreeTDS_Thread_not_compiled 298 #define tds_thread_join(th, ret) \ 299 FreeTDS_Thread_not_compiled 301 static inline tds_thread_id tds_thread_get_current_id(
void)
306 static inline int tds_thread_is_current(tds_thread_id th)
314 #ifdef TDS_HAVE_MUTEX 315 # define tds_cond_init tds_raw_cond_init 316 # define tds_cond_destroy tds_raw_cond_destroy 317 # define tds_cond_signal tds_raw_cond_signal 318 # if !ENABLE_EXTRA_CHECKS 319 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER 320 # define tds_mutex tds_raw_mutex 321 # define tds_mutex_lock tds_raw_mutex_lock 322 # define tds_mutex_trylock tds_raw_mutex_trylock 323 # define tds_mutex_unlock tds_raw_mutex_unlock 324 # define tds_mutex_check_owned(mtx) do {} while(0) 325 # define tds_mutex_init tds_raw_mutex_init 326 # define tds_mutex_free tds_raw_mutex_free 327 # define tds_cond_wait tds_raw_cond_wait 328 # define tds_cond_timedwait tds_raw_cond_timedwait 332 typedef struct tds_mutex
336 volatile tds_thread_id locked_by;
339 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 } 341 static inline void tds_mutex_lock(tds_mutex *mtx)
344 tds_raw_mutex_lock(&mtx->mtx);
345 assert(!mtx->locked);
347 mtx->locked_by = tds_thread_get_current_id();
350 static inline int tds_mutex_trylock(tds_mutex *mtx)
354 ret = tds_raw_mutex_trylock(&mtx->mtx);
356 assert(!mtx->locked);
358 mtx->locked_by = tds_thread_get_current_id();
363 static inline void tds_mutex_unlock(tds_mutex *mtx)
365 assert(mtx && mtx->locked);
367 tds_raw_mutex_unlock(&mtx->mtx);
370 static inline void tds_mutex_check_owned(tds_mutex *mtx)
374 ret = tds_raw_mutex_trylock(&mtx->mtx);
377 assert(tds_thread_is_current(mtx->locked_by));
380 static inline int tds_mutex_init(tds_mutex *mtx)
383 return tds_raw_mutex_init(&mtx->mtx);
386 static inline void tds_mutex_free(tds_mutex *mtx)
388 assert(mtx && !mtx->locked);
389 tds_raw_mutex_free(&mtx->mtx);
392 static inline int tds_cond_wait(tds_condition *cond, tds_mutex *mtx)
395 assert(mtx && mtx->locked);
397 ret = tds_raw_cond_wait(cond, &mtx->mtx);
399 mtx->locked_by = tds_thread_get_current_id();
403 static inline int tds_cond_timedwait(tds_condition *cond, tds_mutex *mtx,
int timeout_sec)
406 assert(mtx && mtx->locked);
408 ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
410 mtx->locked_by = tds_thread_get_current_id();
Definition: ptw32_MCS_lock.c:97