27 #if defined(_THREAD_SAFE) && defined(TDS_HAVE_PTHREAD_MUTEX)
31 #include <freetds/pushvis.h>
34 #define TDS_RAW_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
38 pthread_mutex_lock(mtx);
43 return pthread_mutex_trylock(mtx);
48 pthread_mutex_unlock(mtx);
53 return pthread_mutex_init(mtx, NULL);
58 pthread_mutex_destroy(mtx);
66 return pthread_cond_destroy(cond);
70 return pthread_cond_signal(cond);
74 return pthread_cond_wait(cond, mtx);
78 #define TDS_HAVE_MUTEX 1
81 typedef pthread_t tds_thread_id;
82 typedef void *(*tds_thread_proc)(
void *arg);
83 #define TDS_THREAD_PROC_DECLARE(name, arg) \
86 static inline int tds_thread_create(
tds_thread *ret, tds_thread_proc proc,
void *arg)
88 return pthread_create(ret, NULL, proc, arg);
91 static inline int tds_thread_join(
tds_thread th,
void **ret)
93 return pthread_join(th, ret);
96 static inline tds_thread_id tds_thread_get_current_id(
void)
98 return pthread_self();
101 static inline int tds_thread_is_current(tds_thread_id th)
103 return pthread_equal(th, pthread_self());
106 #include <freetds/popvis.h>
108 #elif defined(_WIN32)
115 CRITICAL_SECTION crit;
118 #define TDS_RAW_MUTEX_INITIALIZER { NULL, 0 }
133 EnterCriticalSection(&(mtx)->crit);
135 tds_win_mutex_lock(mtx);
142 LeaveCriticalSection(&(mtx)->crit);
148 DeleteCriticalSection(&(mtx)->crit);
153 #define TDS_HAVE_MUTEX 1
156 typedef void *TDS_CONDITION_VARIABLE;
159 TDS_CONDITION_VARIABLE cv;
168 return tds_raw_cond_timedwait(cond, mtx, -1);
172 typedef DWORD tds_thread_id;
173 typedef void *(WINAPI *tds_thread_proc)(
void *arg);
174 #define TDS_THREAD_PROC_DECLARE(name, arg) \
175 void *WINAPI name(void *arg)
177 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc,
void *arg)
179 *ret = CreateThread(NULL, 0, (DWORD (WINAPI *)(
void*)) proc, arg, 0, NULL);
180 return *ret != NULL ? 0 : 11 ;
183 static inline int tds_thread_join(tds_thread th,
void **ret)
185 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
187 if (ret && GetExitCodeThread(th, &r))
188 *ret = (
void*) (((
char*)0) + r);
197 static inline tds_thread_id tds_thread_get_current_id(
void)
199 return GetCurrentThreadId();
202 static inline int tds_thread_is_current(tds_thread_id th)
204 return th == GetCurrentThreadId();
213 #define TDS_RAW_MUTEX_INITIALIZER {}
248 #define tds_raw_cond_signal(cond) \
249 FreeTDS_Condition_not_compiled
251 #define tds_raw_cond_wait(cond, mtx) \
252 FreeTDS_Condition_not_compiled
254 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \
255 FreeTDS_Condition_not_compiled
259 typedef int tds_thread_id;
261 typedef void *(*tds_thread_proc)(
void *arg);
262 #define TDS_THREAD_PROC_DECLARE(name, arg) \
263 void *name(void *arg)
265 #define tds_thread_create(ret, proc, arg) \
266 FreeTDS_Thread_not_compiled
268 #define tds_thread_join(th, ret) \
269 FreeTDS_Thread_not_compiled
271 static inline tds_thread_id tds_thread_get_current_id(
void)
276 static inline int tds_thread_is_current(tds_thread_id th)
284 #ifdef TDS_HAVE_MUTEX
285 # define tds_cond_init tds_raw_cond_init
286 # define tds_cond_destroy tds_raw_cond_destroy
287 # define tds_cond_signal tds_raw_cond_signal
288 # if !ENABLE_EXTRA_CHECKS
289 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER
290 # define tds_mutex tds_raw_mutex
291 # define tds_mutex_lock tds_raw_mutex_lock
292 # define tds_mutex_trylock tds_raw_mutex_trylock
293 # define tds_mutex_unlock tds_raw_mutex_unlock
294 # define tds_mutex_check_owned(mtx) do {} while(0)
295 # define tds_mutex_init tds_raw_mutex_init
296 # define tds_mutex_free tds_raw_mutex_free
297 # define tds_cond_wait tds_raw_cond_wait
298 # define tds_cond_timedwait tds_raw_cond_timedwait
302 typedef struct tds_mutex
306 volatile tds_thread_id locked_by;
309 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 }
311 static inline void tds_mutex_lock(tds_mutex *mtx)
314 tds_raw_mutex_lock(&mtx->mtx);
315 assert(!mtx->locked);
317 mtx->locked_by = tds_thread_get_current_id();
320 static inline int tds_mutex_trylock(tds_mutex *mtx)
324 ret = tds_raw_mutex_trylock(&mtx->mtx);
326 assert(!mtx->locked);
328 mtx->locked_by = tds_thread_get_current_id();
333 static inline void tds_mutex_unlock(tds_mutex *mtx)
335 assert(mtx && mtx->locked);
337 tds_raw_mutex_unlock(&mtx->mtx);
340 static inline void tds_mutex_check_owned(tds_mutex *mtx)
344 ret = tds_raw_mutex_trylock(&mtx->mtx);
347 assert(tds_thread_is_current(mtx->locked_by));
350 static inline int tds_mutex_init(tds_mutex *mtx)
353 return tds_raw_mutex_init(&mtx->mtx);
356 static inline void tds_mutex_free(tds_mutex *mtx)
358 assert(mtx && !mtx->locked);
359 tds_raw_mutex_free(&mtx->mtx);
362 static inline int tds_cond_wait(
tds_condition *cond, tds_mutex *mtx)
365 assert(mtx && mtx->locked);
367 ret = tds_raw_cond_wait(cond, &mtx->mtx);
369 mtx->locked_by = tds_thread_get_current_id();
373 static inline int tds_cond_timedwait(
tds_condition *cond, tds_mutex *mtx,
int timeout_sec)
376 assert(mtx && mtx->locked);
378 ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
380 mtx->locked_by = tds_thread_get_current_id();
Definition: ptw32_MCS_lock.c:97