29 #if defined(_THREAD_SAFE) && defined(TDS_HAVE_PTHREAD_MUTEX)
33 #include <freetds/pushvis.h>
36 #define TDS_RAW_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
40 pthread_mutex_lock(mtx);
45 return pthread_mutex_trylock(mtx);
50 pthread_mutex_unlock(mtx);
55 return pthread_mutex_init(mtx, NULL);
60 pthread_mutex_destroy(mtx);
68 return pthread_cond_destroy(cond);
72 return pthread_cond_signal(cond);
76 return pthread_cond_wait(cond, mtx);
80 #define TDS_HAVE_MUTEX 1
83 typedef void *(*tds_thread_proc)(
void *arg);
84 #define TDS_THREAD_PROC_DECLARE(name, arg) \
87 static inline int tds_thread_create(
tds_thread *ret, tds_thread_proc proc,
void *arg)
89 return pthread_create(ret, NULL, proc, arg);
92 static inline int tds_thread_join(
tds_thread th,
void **ret)
94 return pthread_join(th, ret);
97 #include <freetds/popvis.h>
106 CRITICAL_SECTION crit;
109 #define TDS_RAW_MUTEX_INITIALIZER { NULL, 0 }
124 EnterCriticalSection(&(mtx)->crit);
126 tds_win_mutex_lock(mtx);
133 LeaveCriticalSection(&(mtx)->crit);
139 DeleteCriticalSection(&(mtx)->crit);
144 #define TDS_HAVE_MUTEX 1
147 typedef void *TDS_CONDITION_VARIABLE;
150 TDS_CONDITION_VARIABLE cv;
159 return tds_raw_cond_timedwait(cond, mtx, -1);
163 typedef void *(WINAPI *tds_thread_proc)(
void *arg);
164 #define TDS_THREAD_PROC_DECLARE(name, arg) \
165 void *WINAPI name(void *arg)
167 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc,
void *arg)
169 *ret = CreateThread(NULL, 0, (DWORD (WINAPI *)(
void*)) proc, arg, 0, NULL);
170 return *ret != NULL ? 0 : 11 ;
173 static inline int tds_thread_join(tds_thread th,
void **ret)
175 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
177 if (ret && GetExitCodeThread(th, &r))
178 *ret = (
void*) (((
char*)0) + r);
193 #define TDS_RAW_MUTEX_INITIALIZER {}
228 #define tds_raw_cond_signal(cond) \
229 FreeTDS_Condition_not_compiled
231 #define tds_raw_cond_wait(cond, mtx) \
232 FreeTDS_Condition_not_compiled
234 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \
235 FreeTDS_Condition_not_compiled
240 typedef void *(*tds_thread_proc)(
void *arg);
241 #define TDS_THREAD_PROC_DECLARE(name, arg) \
242 void *name(void *arg)
244 #define tds_thread_create(ret, proc, arg) \
245 FreeTDS_Thread_not_compiled
247 #define tds_thread_join(th, ret) \
248 FreeTDS_Thread_not_compiled
252 #ifdef TDS_HAVE_MUTEX
253 # define tds_cond_init tds_raw_cond_init
254 # define tds_cond_destroy tds_raw_cond_destroy
255 # define tds_cond_signal tds_raw_cond_signal
256 # if !ENABLE_EXTRA_CHECKS
257 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER
258 # define tds_mutex tds_raw_mutex
259 # define tds_mutex_lock tds_raw_mutex_lock
260 # define tds_mutex_trylock tds_raw_mutex_trylock
261 # define tds_mutex_unlock tds_raw_mutex_unlock
262 # define tds_mutex_init tds_raw_mutex_init
263 # define tds_mutex_free tds_raw_mutex_free
264 # define tds_cond_wait tds_raw_cond_wait
265 # define tds_cond_timedwait tds_raw_cond_timedwait
269 typedef struct tds_mutex
275 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 }
277 static inline void tds_mutex_lock(tds_mutex *mtx)
280 tds_raw_mutex_lock(&mtx->mtx);
281 assert(!mtx->locked);
285 static inline int tds_mutex_trylock(tds_mutex *mtx)
289 ret = tds_raw_mutex_trylock(&mtx->mtx);
291 assert(!mtx->locked);
297 static inline void tds_mutex_unlock(tds_mutex *mtx)
299 assert(mtx && mtx->locked);
301 tds_raw_mutex_unlock(&mtx->mtx);
304 static inline int tds_mutex_init(tds_mutex *mtx)
307 return tds_raw_mutex_init(&mtx->mtx);
310 static inline void tds_mutex_free(tds_mutex *mtx)
312 assert(mtx && !mtx->locked);
313 tds_raw_mutex_free(&mtx->mtx);
316 static inline int tds_cond_wait(
tds_condition *cond, tds_mutex *mtx)
319 assert(mtx && mtx->locked);
321 ret = tds_raw_cond_wait(cond, &mtx->mtx);
326 static inline int tds_cond_timedwait(
tds_condition *cond, tds_mutex *mtx,
int timeout_sec)
329 assert(mtx && mtx->locked);
331 ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
Definition: ptw32_MCS_lock.c:97