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 void *(*tds_thread_proc)(
void *arg);
82 #define TDS_THREAD_PROC_DECLARE(name, arg) \
85 static inline int tds_thread_create(
tds_thread *ret, tds_thread_proc proc,
void *arg)
87 return pthread_create(ret, NULL, proc, arg);
90 static inline int tds_thread_join(
tds_thread th,
void **ret)
92 return pthread_join(th, ret);
95 #include <freetds/popvis.h>
104 CRITICAL_SECTION crit;
107 #define TDS_RAW_MUTEX_INITIALIZER { NULL, 0 }
122 EnterCriticalSection(&(mtx)->crit);
124 tds_win_mutex_lock(mtx);
131 LeaveCriticalSection(&(mtx)->crit);
137 DeleteCriticalSection(&(mtx)->crit);
142 #define TDS_HAVE_MUTEX 1
145 typedef void *TDS_CONDITION_VARIABLE;
148 TDS_CONDITION_VARIABLE cv;
157 return tds_raw_cond_timedwait(cond, mtx, -1);
161 typedef void *(WINAPI *tds_thread_proc)(
void *arg);
162 #define TDS_THREAD_PROC_DECLARE(name, arg) \
163 void *WINAPI name(void *arg)
165 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc,
void *arg)
167 *ret = CreateThread(NULL, 0, (DWORD (WINAPI *)(
void*)) proc, arg, 0, NULL);
168 return *ret != NULL ? 0 : 11 ;
171 static inline int tds_thread_join(tds_thread th,
void **ret)
173 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
175 if (ret && GetExitCodeThread(th, &r))
176 *ret = (
void*) (((
char*)0) + r);
191 #define TDS_RAW_MUTEX_INITIALIZER {}
226 #define tds_raw_cond_signal(cond) \
227 FreeTDS_Condition_not_compiled
229 #define tds_raw_cond_wait(cond, mtx) \
230 FreeTDS_Condition_not_compiled
232 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \
233 FreeTDS_Condition_not_compiled
238 typedef void *(*tds_thread_proc)(
void *arg);
239 #define TDS_THREAD_PROC_DECLARE(name, arg) \
240 void *name(void *arg)
242 #define tds_thread_create(ret, proc, arg) \
243 FreeTDS_Thread_not_compiled
245 #define tds_thread_join(th, ret) \
246 FreeTDS_Thread_not_compiled
250 #ifdef TDS_HAVE_MUTEX
251 # define tds_cond_init tds_raw_cond_init
252 # define tds_cond_destroy tds_raw_cond_destroy
253 # define tds_cond_signal tds_raw_cond_signal
254 # if !ENABLE_EXTRA_CHECKS
255 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER
256 # define tds_mutex tds_raw_mutex
257 # define tds_mutex_lock tds_raw_mutex_lock
258 # define tds_mutex_trylock tds_raw_mutex_trylock
259 # define tds_mutex_unlock tds_raw_mutex_unlock
260 # define tds_mutex_init tds_raw_mutex_init
261 # define tds_mutex_free tds_raw_mutex_free
262 # define tds_cond_wait tds_raw_cond_wait
263 # define tds_cond_timedwait tds_raw_cond_timedwait
267 typedef struct tds_mutex
273 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 }
275 static inline void tds_mutex_lock(tds_mutex *mtx)
278 tds_raw_mutex_lock(&mtx->mtx);
279 assert(!mtx->locked);
283 static inline int tds_mutex_trylock(tds_mutex *mtx)
287 ret = tds_raw_mutex_trylock(&mtx->mtx);
289 assert(!mtx->locked);
295 static inline void tds_mutex_unlock(tds_mutex *mtx)
297 assert(mtx && mtx->locked);
299 tds_raw_mutex_unlock(&mtx->mtx);
302 static inline int tds_mutex_init(tds_mutex *mtx)
305 return tds_raw_mutex_init(&mtx->mtx);
308 static inline void tds_mutex_free(tds_mutex *mtx)
310 assert(mtx && !mtx->locked);
311 tds_raw_mutex_free(&mtx->mtx);
314 static inline int tds_cond_wait(
tds_condition *cond, tds_mutex *mtx)
317 assert(mtx && mtx->locked);
319 ret = tds_raw_cond_wait(cond, &mtx->mtx);
324 static inline int tds_cond_timedwait(
tds_condition *cond, tds_mutex *mtx,
int timeout_sec)
327 assert(mtx && mtx->locked);
329 ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
Definition: ptw32_MCS_lock.c:97