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 136 #define TDS_RAW_MUTEX_INITIALIZER { SRWLOCK_INIT } 144 #define TDS_HAVE_MUTEX 1 147 typedef void *TDS_CONDITION_VARIABLE;
150 TDS_CONDITION_VARIABLE cv;
159 return tds_raw_cond_timedwait(cond, mtx, -1);
163 typedef DWORD tds_thread_id;
164 typedef DWORD (WINAPI *tds_thread_proc)(
void *arg);
165 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 166 DWORD WINAPI name(void *arg) 167 #define TDS_THREAD_RESULT(n) ((DWORD)(int)(n)) 169 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc,
void *arg)
171 *ret = CreateThread(NULL, 0, proc, arg, 0, NULL);
172 return *ret != NULL ? 0 : 11 ;
175 static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
177 HANDLE h = CreateThread(NULL, 0, proc, arg, 0, NULL);
186 static inline int tds_thread_join(tds_thread th,
void **ret)
188 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
191 if (!GetExitCodeThread(th, &r))
193 *ret = (
void*) (((
char*)0) + r);
203 static inline tds_thread_id tds_thread_get_current_id(
void)
205 return GetCurrentThreadId();
208 static inline int tds_thread_is_current(tds_thread_id th)
210 return th == GetCurrentThreadId();
215 #include <tds_sysdep_public.h> 222 #define TDS_RAW_MUTEX_INITIALIZER {} 258 #define tds_raw_cond_signal(cond) \ 259 FreeTDS_Condition_not_compiled 261 #define tds_raw_cond_wait(cond, mtx) \ 262 FreeTDS_Condition_not_compiled 264 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \ 265 FreeTDS_Condition_not_compiled 270 typedef int tds_thread_id;
272 typedef void *(*tds_thread_proc)(
void *arg);
273 #define TDS_THREAD_PROC_DECLARE(name, arg) \ 274 void *name(void *arg) 275 #define TDS_THREAD_RESULT(n) ((void*)(TDS_INTPTR)(n)) 277 #define tds_thread_create(ret, proc, arg) \ 278 FreeTDS_Thread_not_compiled 280 #define tds_thread_create_detached(proc, arg) \ 281 FreeTDS_Thread_not_compiled 283 #define tds_thread_join(th, ret) \ 284 FreeTDS_Thread_not_compiled 286 static inline tds_thread_id tds_thread_get_current_id(
void)
291 static inline int tds_thread_is_current(tds_thread_id th)
298 # define tds_cond_init tds_raw_cond_init 299 # define tds_cond_destroy tds_raw_cond_destroy 300 # define tds_cond_signal tds_raw_cond_signal 301 # if !ENABLE_EXTRA_CHECKS || !defined(TDS_HAVE_MUTEX) 302 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER 303 # define tds_mutex tds_raw_mutex 304 # define tds_mutex_lock tds_raw_mutex_lock 305 # define tds_mutex_trylock tds_raw_mutex_trylock 306 # define tds_mutex_unlock tds_raw_mutex_unlock 307 # define tds_mutex_check_owned(mtx) do {} while(0) 308 # define tds_mutex_init tds_raw_mutex_init 309 # define tds_mutex_free tds_raw_mutex_free 310 # define tds_cond_wait tds_raw_cond_wait 311 # define tds_cond_timedwait tds_raw_cond_timedwait 315 typedef struct tds_mutex
319 volatile tds_thread_id locked_by;
322 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 } 324 static inline void tds_mutex_lock(tds_mutex *mtx)
327 tds_raw_mutex_lock(&mtx->mtx);
328 assert(!mtx->locked);
330 mtx->locked_by = tds_thread_get_current_id();
333 static inline int tds_mutex_trylock(tds_mutex *mtx)
337 ret = tds_raw_mutex_trylock(&mtx->mtx);
339 assert(!mtx->locked);
341 mtx->locked_by = tds_thread_get_current_id();
346 static inline void tds_mutex_unlock(tds_mutex *mtx)
348 assert(mtx && mtx->locked);
350 tds_raw_mutex_unlock(&mtx->mtx);
353 static inline void tds_mutex_check_owned(tds_mutex *mtx)
357 ret = tds_raw_mutex_trylock(&mtx->mtx);
360 assert(tds_thread_is_current(mtx->locked_by));
363 static inline int tds_mutex_init(tds_mutex *mtx)
366 return tds_raw_mutex_init(&mtx->mtx);
369 static inline void tds_mutex_free(tds_mutex *mtx)
371 assert(mtx && !mtx->locked);
372 tds_raw_mutex_free(&mtx->mtx);
375 static inline int tds_cond_wait(
tds_condition *cond, tds_mutex *mtx)
378 assert(mtx && mtx->locked);
380 ret = tds_raw_cond_wait(cond, &mtx->mtx);
382 mtx->locked_by = tds_thread_get_current_id();
386 static inline int tds_cond_timedwait(
tds_condition *cond, tds_mutex *mtx,
int timeout_sec)
389 assert(mtx && mtx->locked);
391 ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
393 mtx->locked_by = tds_thread_get_current_id();