FreeTDS API
Loading...
Searching...
No Matches
thread.h
1/* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 *
3 * Copyright (C) 2005 Liam Widdowson
4 * Copyright (C) 2010-2012 Frediano Ziglio
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22#ifndef TDSTHREAD_H
23#define TDSTHREAD_H 1
24
25#undef TDS_HAVE_MUTEX
26
27#if defined(_THREAD_SAFE) && defined(TDS_HAVE_PTHREAD_MUTEX)
28
29#include <tds_sysdep_public.h>
30#include <pthread.h>
31#include <errno.h>
32
33#include <freetds/pushvis.h>
34
35typedef pthread_mutex_t tds_raw_mutex;
36#define TDS_RAW_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
37
38static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
39{
40 pthread_mutex_lock(mtx);
41}
42
43static inline int tds_raw_mutex_trylock(tds_raw_mutex *mtx)
44{
45 return pthread_mutex_trylock(mtx);
46}
47
48static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
49{
50 pthread_mutex_unlock(mtx);
51}
52
53static inline int tds_raw_mutex_init(tds_raw_mutex *mtx)
54{
55 return pthread_mutex_init(mtx, NULL);
56}
57
58static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
59{
60 pthread_mutex_destroy(mtx);
61}
62
63typedef pthread_cond_t tds_condition;
64
65int tds_raw_cond_init(tds_condition *cond);
66static inline int tds_raw_cond_destroy(tds_condition *cond)
67{
68 return pthread_cond_destroy(cond);
69}
70static inline int tds_raw_cond_signal(tds_condition *cond)
71{
72 return pthread_cond_signal(cond);
73}
74static inline int tds_raw_cond_wait(tds_condition *cond, tds_raw_mutex *mtx)
75{
76 return pthread_cond_wait(cond, mtx);
77}
78int tds_raw_cond_timedwait(tds_condition *cond, tds_raw_mutex *mtx, int timeout_sec);
79
80#define TDS_HAVE_MUTEX 1
81
82typedef pthread_t tds_thread;
83typedef pthread_t tds_thread_id;
84typedef void *(*tds_thread_proc)(void *arg);
85#define TDS_THREAD_PROC_DECLARE(name, arg) \
86 void *name(void *arg)
87#define TDS_THREAD_RESULT(n) ((void*)(intptr_t)(n))
88
89static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc, void *arg)
90{
91 return pthread_create(ret, NULL, proc, arg);
92}
93
94static inline int tds_thread_create_detached(tds_thread_proc proc, void *arg)
95{
96 tds_thread th;
97 int ret = pthread_create(&th, NULL, proc, arg);
98 if (!ret)
99 pthread_detach(th);
100 return ret;
101}
102
103static inline int tds_thread_join(tds_thread th, void **ret)
104{
105 return pthread_join(th, ret);
106}
107
108static inline tds_thread_id tds_thread_get_current_id(void)
109{
110 return pthread_self();
111}
112
113static inline int tds_thread_is_current(tds_thread_id th)
114{
115 return pthread_equal(th, pthread_self());
116}
117
118#include <freetds/popvis.h>
119
120#elif defined(_WIN32)
121
122#include <freetds/windows.h>
123#include <errno.h>
124
125/* old version of Windows do not define this constant */
126#ifndef ETIMEDOUT
127#define ETIMEDOUT 138
128#endif
129
130struct ptw32_mcs_node_t_;
131
132typedef struct {
133 struct ptw32_mcs_node_t_ *lock;
134 LONG done;
135 DWORD thread_id;
136 CRITICAL_SECTION crit;
138
139#define TDS_RAW_MUTEX_INITIALIZER { NULL, 0, 0 }
140
141static inline int
142tds_raw_mutex_init(tds_raw_mutex *mtx)
143{
144 mtx->lock = NULL;
145 mtx->done = 0;
146 mtx->thread_id = 0;
147 return 0;
148}
149
150void tds_win_mutex_lock(tds_raw_mutex *mutex);
151
152static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
153{
154 if (mtx->done) {
155 EnterCriticalSection(&mtx->crit);
156 mtx->thread_id = GetCurrentThreadId();
157 } else {
158 tds_win_mutex_lock(mtx);
159 }
160}
161
162int tds_raw_mutex_trylock(tds_raw_mutex *mtx);
163
164static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
165{
166 mtx->thread_id = 0;
167 LeaveCriticalSection(&mtx->crit);
168}
169
170static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
171{
172 if (mtx->done) {
173 DeleteCriticalSection(&mtx->crit);
174 mtx->done = 0;
175 }
176}
177
178#define TDS_HAVE_MUTEX 1
179
180/* easy way, only single signal supported */
181typedef void *TDS_CONDITION_VARIABLE;
182typedef union {
183 HANDLE ev;
184 TDS_CONDITION_VARIABLE cv;
186
187extern int (*tds_raw_cond_init)(tds_condition *cond);
188extern int (*tds_raw_cond_destroy)(tds_condition *cond);
189extern int (*tds_raw_cond_signal)(tds_condition *cond);
190extern int (*tds_raw_cond_timedwait)(tds_condition *cond, tds_raw_mutex *mtx, int timeout_sec);
191static inline int tds_raw_cond_wait(tds_condition *cond, tds_raw_mutex *mtx)
192{
193 return tds_raw_cond_timedwait(cond, mtx, -1);
194}
195
196typedef HANDLE tds_thread;
197typedef DWORD tds_thread_id;
198typedef DWORD (WINAPI *tds_thread_proc)(void *arg);
199#define TDS_THREAD_PROC_DECLARE(name, arg) \
200 DWORD WINAPI name(void *arg)
201#define TDS_THREAD_RESULT(n) ((DWORD)(int)(n))
202
203static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc, void *arg)
204{
205 *ret = CreateThread(NULL, 0, proc, arg, 0, NULL);
206 return *ret != NULL ? 0 : 11 /* EAGAIN */;
207}
208
209static inline int tds_thread_create_detached(tds_thread_proc proc, void *arg)
210{
211 HANDLE h = CreateThread(NULL, 0, proc, arg, 0, NULL);
212 if (h)
213 return 0;
214 CloseHandle(h);
215 return 11 /* EAGAIN */;
216}
217
218static inline int tds_thread_join(tds_thread th, void **ret)
219{
220 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
221 if (ret) {
222 DWORD r;
223 if (!GetExitCodeThread(th, &r))
224 r = 0xffffffffu;
225 *ret = (void*) (((char*)0) + r);
226 }
227
228 CloseHandle(th);
229 return 0;
230 }
231 CloseHandle(th);
232 return 22 /* EINVAL */;
233}
234
235static inline tds_thread_id tds_thread_get_current_id(void)
236{
237 return GetCurrentThreadId();
238}
239
240static inline int tds_thread_is_current(tds_thread_id th)
241{
242 return th == GetCurrentThreadId();
243}
244
245#else
246
247#include <tds_sysdep_public.h>
248
249/* define noops as "successful" */
250typedef struct {
251 char dummy[0]; /* compiler compatibility */
253
254#define TDS_RAW_MUTEX_INITIALIZER {}
255
256static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
257{
258}
259
260static inline int tds_raw_mutex_trylock(tds_raw_mutex *mtx)
261{
262 return 0;
263}
264
265static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
266{
267}
268
269static inline int tds_raw_mutex_init(tds_raw_mutex *mtx)
270{
271 return 0;
272}
273
274static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
275{
276}
277
278typedef struct {
279 char dummy[0]; /* compiler compatibility */
281
282static inline int tds_raw_cond_init(tds_condition *cond)
283{
284 return 0;
285}
286static inline int tds_raw_cond_destroy(tds_condition *cond)
287{
288 return 0;
289}
290#define tds_raw_cond_signal(cond) \
291 FreeTDS_Condition_not_compiled
292
293#define tds_raw_cond_wait(cond, mtx) \
294 FreeTDS_Condition_not_compiled
295
296#define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \
297 FreeTDS_Condition_not_compiled
298
299typedef struct {
300 char dummy[0]; /* compiler compatibility */
301} tds_thread;
302typedef int tds_thread_id;
303
304typedef void *(*tds_thread_proc)(void *arg);
305#define TDS_THREAD_PROC_DECLARE(name, arg) \
306 void *name(void *arg)
307#define TDS_THREAD_RESULT(n) ((void*)(intptr_t)(n))
308
309#define tds_thread_create(ret, proc, arg) \
310 FreeTDS_Thread_not_compiled
311
312#define tds_thread_create_detached(proc, arg) \
313 FreeTDS_Thread_not_compiled
314
315#define tds_thread_join(th, ret) \
316 FreeTDS_Thread_not_compiled
317
318static inline tds_thread_id tds_thread_get_current_id(void)
319{
320 return 0;
321}
322
323static inline int tds_thread_is_current(tds_thread_id th)
324{
325 return 1;
326}
327
328#endif
329
330# define tds_cond_init tds_raw_cond_init
331# define tds_cond_destroy tds_raw_cond_destroy
332# define tds_cond_signal tds_raw_cond_signal
333# if !ENABLE_EXTRA_CHECKS
334# define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER
335# define tds_mutex tds_raw_mutex
336# define tds_mutex_lock tds_raw_mutex_lock
337# define tds_mutex_trylock tds_raw_mutex_trylock
338# define tds_mutex_unlock tds_raw_mutex_unlock
339# define tds_mutex_check_owned(mtx) do {} while(0)
340# define tds_mutex_init tds_raw_mutex_init
341# define tds_mutex_free tds_raw_mutex_free
342# define tds_cond_wait tds_raw_cond_wait
343# define tds_cond_timedwait tds_raw_cond_timedwait
344# else
345# include <assert.h>
346
347typedef struct tds_mutex
348{
349 tds_raw_mutex mtx;
350 volatile int locked;
351 volatile tds_thread_id locked_by;
352} tds_mutex;
353
354# define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 }
355
356static inline void tds_mutex_lock(tds_mutex *mtx)
357{
358 assert(mtx);
359 tds_raw_mutex_lock(&mtx->mtx);
360 assert(!mtx->locked);
361 mtx->locked = 1;
362 mtx->locked_by = tds_thread_get_current_id();
363}
364
365static inline int tds_mutex_trylock(tds_mutex *mtx)
366{
367 int ret;
368 assert(mtx);
369 ret = tds_raw_mutex_trylock(&mtx->mtx);
370 if (!ret) {
371 assert(!mtx->locked);
372 mtx->locked = 1;
373 mtx->locked_by = tds_thread_get_current_id();
374 }
375 return ret;
376}
377
378static inline void tds_mutex_unlock(tds_mutex *mtx)
379{
380 assert(mtx && mtx->locked);
381 mtx->locked = 0;
382 tds_raw_mutex_unlock(&mtx->mtx);
383}
384
385static inline void tds_mutex_check_owned(tds_mutex *mtx)
386{
387 int ret;
388 assert(mtx);
389 ret = tds_raw_mutex_trylock(&mtx->mtx);
390 assert(ret);
391 assert(mtx->locked);
392 assert(tds_thread_is_current(mtx->locked_by));
393}
394
395static inline int tds_mutex_init(tds_mutex *mtx)
396{
397 mtx->locked = 0;
398 return tds_raw_mutex_init(&mtx->mtx);
399}
400
401static inline void tds_mutex_free(tds_mutex *mtx)
402{
403 assert(mtx && !mtx->locked);
404 tds_raw_mutex_free(&mtx->mtx);
405}
406
407static inline int tds_cond_wait(tds_condition *cond, tds_mutex *mtx)
408{
409 int ret;
410 assert(mtx && mtx->locked);
411 mtx->locked = 0;
412 ret = tds_raw_cond_wait(cond, &mtx->mtx);
413 mtx->locked = 1;
414 mtx->locked_by = tds_thread_get_current_id();
415 return ret;
416}
417
418static inline int tds_cond_timedwait(tds_condition *cond, tds_mutex *mtx, int timeout_sec)
419{
420 int ret;
421 assert(mtx && mtx->locked);
422 mtx->locked = 0;
423 ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
424 mtx->locked = 1;
425 mtx->locked_by = tds_thread_get_current_id();
426 return ret;
427}
428
429# endif
430
431#endif
Definition: ptw32_MCS_lock.c:98
Definition: thread.h:250
Definition: thread.h:278
Definition: thread.h:299