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_create_detached(tds_thread_proc proc,
void *arg)
94 int ret = pthread_create(&th, NULL, proc, arg);
100 static inline int tds_thread_join(
tds_thread th,
void **ret)
102 return pthread_join(th, ret);
105 static inline tds_thread_id tds_thread_get_current_id(
void)
107 return pthread_self();
110 static inline int tds_thread_is_current(tds_thread_id th)
112 return pthread_equal(th, pthread_self());
115 #include <freetds/popvis.h>
117 #elif defined(_WIN32)
124 CRITICAL_SECTION crit;
127 #define TDS_RAW_MUTEX_INITIALIZER { NULL, 0 }
142 EnterCriticalSection(&(mtx)->crit);
144 tds_win_mutex_lock(mtx);
151 LeaveCriticalSection(&(mtx)->crit);
157 DeleteCriticalSection(&(mtx)->crit);
162 #define TDS_HAVE_MUTEX 1
165 typedef void *TDS_CONDITION_VARIABLE;
168 TDS_CONDITION_VARIABLE cv;
177 return tds_raw_cond_timedwait(cond, mtx, -1);
181 typedef DWORD tds_thread_id;
182 typedef void *(WINAPI *tds_thread_proc)(
void *arg);
183 #define TDS_THREAD_PROC_DECLARE(name, arg) \
184 void *WINAPI name(void *arg)
186 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc,
void *arg)
188 *ret = CreateThread(NULL, 0, (DWORD (WINAPI *)(
void*)) proc, arg, 0, NULL);
189 return *ret != NULL ? 0 : 11 ;
192 static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
194 HANDLE h = CreateThread(NULL, 0, (DWORD (WINAPI *)(
void*)) proc, arg, 0, NULL);
201 static inline int tds_thread_join(tds_thread th,
void **ret)
203 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
205 if (ret && GetExitCodeThread(th, &r))
206 *ret = (
void*) (((
char*)0) + r);
215 static inline tds_thread_id tds_thread_get_current_id(
void)
217 return GetCurrentThreadId();
220 static inline int tds_thread_is_current(tds_thread_id th)
222 return th == GetCurrentThreadId();
231 #define TDS_RAW_MUTEX_INITIALIZER {}
266 #define tds_raw_cond_signal(cond) \
267 FreeTDS_Condition_not_compiled
269 #define tds_raw_cond_wait(cond, mtx) \
270 FreeTDS_Condition_not_compiled
272 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \
273 FreeTDS_Condition_not_compiled
277 typedef int tds_thread_id;
279 typedef void *(*tds_thread_proc)(
void *arg);
280 #define TDS_THREAD_PROC_DECLARE(name, arg) \
281 void *name(void *arg)
283 #define tds_thread_create(ret, proc, arg) \
284 FreeTDS_Thread_not_compiled
286 #define tds_thread_create_detached(proc, arg) \
287 FreeTDS_Thread_not_compiled
289 #define tds_thread_join(th, ret) \
290 FreeTDS_Thread_not_compiled
292 static inline tds_thread_id tds_thread_get_current_id(
void)
297 static inline int tds_thread_is_current(tds_thread_id th)
305 #ifdef TDS_HAVE_MUTEX
306 # define tds_cond_init tds_raw_cond_init
307 # define tds_cond_destroy tds_raw_cond_destroy
308 # define tds_cond_signal tds_raw_cond_signal
309 # if !ENABLE_EXTRA_CHECKS
310 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER
311 # define tds_mutex tds_raw_mutex
312 # define tds_mutex_lock tds_raw_mutex_lock
313 # define tds_mutex_trylock tds_raw_mutex_trylock
314 # define tds_mutex_unlock tds_raw_mutex_unlock
315 # define tds_mutex_check_owned(mtx) do {} while(0)
316 # define tds_mutex_init tds_raw_mutex_init
317 # define tds_mutex_free tds_raw_mutex_free
318 # define tds_cond_wait tds_raw_cond_wait
319 # define tds_cond_timedwait tds_raw_cond_timedwait
323 typedef struct tds_mutex
327 volatile tds_thread_id locked_by;
330 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 }
332 static inline void tds_mutex_lock(tds_mutex *mtx)
335 tds_raw_mutex_lock(&mtx->mtx);
336 assert(!mtx->locked);
338 mtx->locked_by = tds_thread_get_current_id();
341 static inline int tds_mutex_trylock(tds_mutex *mtx)
345 ret = tds_raw_mutex_trylock(&mtx->mtx);
347 assert(!mtx->locked);
349 mtx->locked_by = tds_thread_get_current_id();
354 static inline void tds_mutex_unlock(tds_mutex *mtx)
356 assert(mtx && mtx->locked);
358 tds_raw_mutex_unlock(&mtx->mtx);
361 static inline void tds_mutex_check_owned(tds_mutex *mtx)
365 ret = tds_raw_mutex_trylock(&mtx->mtx);
368 assert(tds_thread_is_current(mtx->locked_by));
371 static inline int tds_mutex_init(tds_mutex *mtx)
374 return tds_raw_mutex_init(&mtx->mtx);
377 static inline void tds_mutex_free(tds_mutex *mtx)
379 assert(mtx && !mtx->locked);
380 tds_raw_mutex_free(&mtx->mtx);
383 static inline int tds_cond_wait(
tds_condition *cond, tds_mutex *mtx)
386 assert(mtx && mtx->locked);
388 ret = tds_raw_cond_wait(cond, &mtx->mtx);
390 mtx->locked_by = tds_thread_get_current_id();
394 static inline int tds_cond_timedwait(
tds_condition *cond, tds_mutex *mtx,
int timeout_sec)
397 assert(mtx && mtx->locked);
399 ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
401 mtx->locked_by = tds_thread_get_current_id();
Definition: ptw32_MCS_lock.c:97