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)
126 CRITICAL_SECTION crit;
129 #define TDS_RAW_MUTEX_INITIALIZER { NULL, 0 }
144 EnterCriticalSection(&(mtx)->crit);
146 tds_win_mutex_lock(mtx);
153 LeaveCriticalSection(&(mtx)->crit);
159 DeleteCriticalSection(&(mtx)->crit);
164 #define TDS_HAVE_MUTEX 1
167 typedef void *TDS_CONDITION_VARIABLE;
170 TDS_CONDITION_VARIABLE cv;
179 return tds_raw_cond_timedwait(cond, mtx, -1);
183 typedef DWORD tds_thread_id;
184 typedef void *(WINAPI *tds_thread_proc)(
void *arg);
185 #define TDS_THREAD_PROC_DECLARE(name, arg) \
186 void *WINAPI name(void *arg)
188 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc,
void *arg)
190 *ret = CreateThread(NULL, 0, (DWORD (WINAPI *)(
void*)) proc, arg, 0, NULL);
191 return *ret != NULL ? 0 : 11 ;
194 static inline int tds_thread_create_detached(tds_thread_proc proc,
void *arg)
196 HANDLE h = CreateThread(NULL, 0, (DWORD (WINAPI *)(
void*)) proc, arg, 0, NULL);
203 static inline int tds_thread_join(tds_thread th,
void **ret)
205 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
207 if (ret && GetExitCodeThread(th, &r))
208 *ret = (
void*) (((
char*)0) + r);
217 static inline tds_thread_id tds_thread_get_current_id(
void)
219 return GetCurrentThreadId();
222 static inline int tds_thread_is_current(tds_thread_id th)
224 return th == GetCurrentThreadId();
233 #define TDS_RAW_MUTEX_INITIALIZER {}
268 #define tds_raw_cond_signal(cond) \
269 FreeTDS_Condition_not_compiled
271 #define tds_raw_cond_wait(cond, mtx) \
272 FreeTDS_Condition_not_compiled
274 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \
275 FreeTDS_Condition_not_compiled
279 typedef int tds_thread_id;
281 typedef void *(*tds_thread_proc)(
void *arg);
282 #define TDS_THREAD_PROC_DECLARE(name, arg) \
283 void *name(void *arg)
285 #define tds_thread_create(ret, proc, arg) \
286 FreeTDS_Thread_not_compiled
288 #define tds_thread_create_detached(proc, arg) \
289 FreeTDS_Thread_not_compiled
291 #define tds_thread_join(th, ret) \
292 FreeTDS_Thread_not_compiled
294 static inline tds_thread_id tds_thread_get_current_id(
void)
299 static inline int tds_thread_is_current(tds_thread_id th)
307 #ifdef TDS_HAVE_MUTEX
308 # define tds_cond_init tds_raw_cond_init
309 # define tds_cond_destroy tds_raw_cond_destroy
310 # define tds_cond_signal tds_raw_cond_signal
311 # if !ENABLE_EXTRA_CHECKS
312 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER
313 # define tds_mutex tds_raw_mutex
314 # define tds_mutex_lock tds_raw_mutex_lock
315 # define tds_mutex_trylock tds_raw_mutex_trylock
316 # define tds_mutex_unlock tds_raw_mutex_unlock
317 # define tds_mutex_check_owned(mtx) do {} while(0)
318 # define tds_mutex_init tds_raw_mutex_init
319 # define tds_mutex_free tds_raw_mutex_free
320 # define tds_cond_wait tds_raw_cond_wait
321 # define tds_cond_timedwait tds_raw_cond_timedwait
325 typedef struct tds_mutex
329 volatile tds_thread_id locked_by;
332 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 }
334 static inline void tds_mutex_lock(tds_mutex *mtx)
337 tds_raw_mutex_lock(&mtx->mtx);
338 assert(!mtx->locked);
340 mtx->locked_by = tds_thread_get_current_id();
343 static inline int tds_mutex_trylock(tds_mutex *mtx)
347 ret = tds_raw_mutex_trylock(&mtx->mtx);
349 assert(!mtx->locked);
351 mtx->locked_by = tds_thread_get_current_id();
356 static inline void tds_mutex_unlock(tds_mutex *mtx)
358 assert(mtx && mtx->locked);
360 tds_raw_mutex_unlock(&mtx->mtx);
363 static inline void tds_mutex_check_owned(tds_mutex *mtx)
367 ret = tds_raw_mutex_trylock(&mtx->mtx);
370 assert(tds_thread_is_current(mtx->locked_by));
373 static inline int tds_mutex_init(tds_mutex *mtx)
376 return tds_raw_mutex_init(&mtx->mtx);
379 static inline void tds_mutex_free(tds_mutex *mtx)
381 assert(mtx && !mtx->locked);
382 tds_raw_mutex_free(&mtx->mtx);
385 static inline int tds_cond_wait(
tds_condition *cond, tds_mutex *mtx)
388 assert(mtx && mtx->locked);
390 ret = tds_raw_cond_wait(cond, &mtx->mtx);
392 mtx->locked_by = tds_thread_get_current_id();
396 static inline int tds_cond_timedwait(
tds_condition *cond, tds_mutex *mtx,
int timeout_sec)
399 assert(mtx && mtx->locked);
401 ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
403 mtx->locked_by = tds_thread_get_current_id();
Definition: ptw32_MCS_lock.c:97