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
84typedef pthread_t tds_thread_id;
85typedef void *(*tds_thread_proc)(
void *arg);
86#define TDS_THREAD_PROC_DECLARE(name, arg) \
88#define TDS_THREAD_RESULT(n) ((void*)(TDS_INTPTR)(n))
90static inline int tds_thread_create(
tds_thread *ret, tds_thread_proc proc,
void *arg)
92 return pthread_create(ret, NULL, proc, arg);
95static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
98 int ret = pthread_create(&th, NULL, proc, arg);
104static inline int tds_thread_join(
tds_thread th,
void **ret)
106 return pthread_join(th, ret);
109static inline tds_thread_id tds_thread_get_current_id(
void)
111 return pthread_self();
114static inline int tds_thread_is_current(tds_thread_id th)
116 return pthread_equal(th, pthread_self());
119#include <freetds/popvis.h>
123#include <freetds/windows.h>
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
182typedef void *TDS_CONDITION_VARIABLE;
185 TDS_CONDITION_VARIABLE cv;
194 return tds_raw_cond_timedwait(cond, mtx, -1);
198typedef DWORD tds_thread_id;
199typedef 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))
204static 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 ;
210static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
212 HANDLE h = CreateThread(NULL, 0, proc, arg, 0, NULL);
219static 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);
236static inline tds_thread_id tds_thread_get_current_id(
void)
238 return GetCurrentThreadId();
241static 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
303typedef int tds_thread_id;
305typedef 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
319static inline tds_thread_id tds_thread_get_current_id(
void)
324static 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
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
348typedef struct tds_mutex
352 volatile tds_thread_id locked_by;
355# define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 }
357static 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();
366static 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();
379static inline void tds_mutex_unlock(tds_mutex *mtx)
381 assert(mtx && mtx->locked);
383 tds_raw_mutex_unlock(&mtx->mtx);
386static 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));
396static inline int tds_mutex_init(tds_mutex *mtx)
399 return tds_raw_mutex_init(&mtx->mtx);
402static inline void tds_mutex_free(tds_mutex *mtx)
404 assert(mtx && !mtx->locked);
405 tds_raw_mutex_free(&mtx->mtx);
408static 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();
419static 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:98