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);
221static inline int tds_thread_join(
tds_thread th,
void **ret)
223 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
226 if (!GetExitCodeThread(th, &r))
228 *ret = (
void*) (((
char*)0) + r);
238static inline tds_thread_id tds_thread_get_current_id(
void)
240 return GetCurrentThreadId();
243static inline int tds_thread_is_current(tds_thread_id th)
245 return th == GetCurrentThreadId();
250#include <tds_sysdep_public.h>
257#define TDS_RAW_MUTEX_INITIALIZER {}
293#define tds_raw_cond_signal(cond) \
294 FreeTDS_Condition_not_compiled
296#define tds_raw_cond_wait(cond, mtx) \
297 FreeTDS_Condition_not_compiled
299#define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \
300 FreeTDS_Condition_not_compiled
305typedef int tds_thread_id;
307typedef void *(*tds_thread_proc)(
void *arg);
308#define TDS_THREAD_PROC_DECLARE(name, arg) \
309 void *name(void *arg)
310#define TDS_THREAD_RESULT(n) ((void*)(TDS_INTPTR)(n))
312#define tds_thread_create(ret, proc, arg) \
313 FreeTDS_Thread_not_compiled
315#define tds_thread_create_detached(proc, arg) \
316 FreeTDS_Thread_not_compiled
318#define tds_thread_join(th, ret) \
319 FreeTDS_Thread_not_compiled
321static inline tds_thread_id tds_thread_get_current_id(
void)
326static inline int tds_thread_is_current(tds_thread_id th)
333# define tds_cond_init tds_raw_cond_init
334# define tds_cond_destroy tds_raw_cond_destroy
335# define tds_cond_signal tds_raw_cond_signal
336# if !ENABLE_EXTRA_CHECKS || !defined(TDS_HAVE_MUTEX)
337# define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER
338# define tds_mutex tds_raw_mutex
339# define tds_mutex_lock tds_raw_mutex_lock
340# define tds_mutex_trylock tds_raw_mutex_trylock
341# define tds_mutex_unlock tds_raw_mutex_unlock
342# define tds_mutex_check_owned(mtx) do {} while(0)
343# define tds_mutex_init tds_raw_mutex_init
344# define tds_mutex_free tds_raw_mutex_free
345# define tds_cond_wait tds_raw_cond_wait
346# define tds_cond_timedwait tds_raw_cond_timedwait
350typedef struct tds_mutex
354 volatile tds_thread_id locked_by;
357# define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 }
359static inline void tds_mutex_lock(tds_mutex *mtx)
362 tds_raw_mutex_lock(&mtx->mtx);
363 assert(!mtx->locked);
365 mtx->locked_by = tds_thread_get_current_id();
368static inline int tds_mutex_trylock(tds_mutex *mtx)
372 ret = tds_raw_mutex_trylock(&mtx->mtx);
374 assert(!mtx->locked);
376 mtx->locked_by = tds_thread_get_current_id();
381static inline void tds_mutex_unlock(tds_mutex *mtx)
383 assert(mtx && mtx->locked);
385 tds_raw_mutex_unlock(&mtx->mtx);
388static inline void tds_mutex_check_owned(tds_mutex *mtx)
392 ret = tds_raw_mutex_trylock(&mtx->mtx);
395 assert(tds_thread_is_current(mtx->locked_by));
398static inline int tds_mutex_init(tds_mutex *mtx)
401 return tds_raw_mutex_init(&mtx->mtx);
404static inline void tds_mutex_free(tds_mutex *mtx)
406 assert(mtx && !mtx->locked);
407 tds_raw_mutex_free(&mtx->mtx);
410static inline int tds_cond_wait(
tds_condition *cond, tds_mutex *mtx)
413 assert(mtx && mtx->locked);
415 ret = tds_raw_cond_wait(cond, &mtx->mtx);
417 mtx->locked_by = tds_thread_get_current_id();
421static inline int tds_cond_timedwait(
tds_condition *cond, tds_mutex *mtx,
int timeout_sec)
424 assert(mtx && mtx->locked);
426 ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
428 mtx->locked_by = tds_thread_get_current_id();
Definition ptw32_MCS_lock.c:98