FreeTDS API: include/freetds/thread.h Source File

FreeTDS API

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
35 typedef pthread_mutex_t tds_raw_mutex;
36 #define TDS_RAW_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
37
38 static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
39 {
40 pthread_mutex_lock(mtx);
41 }
42
43 static inline int tds_raw_mutex_trylock(tds_raw_mutex *mtx)
44 {
45 return pthread_mutex_trylock(mtx);
46 }
47
48 static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
49 {
50 pthread_mutex_unlock(mtx);
51 }
52
53 static inline int tds_raw_mutex_init(tds_raw_mutex *mtx)
54 {
55 return pthread_mutex_init(mtx, NULL);
56 }
57
58 static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
59 {
60 pthread_mutex_destroy(mtx);
61 }
62
63 typedef pthread_cond_t tds_condition;
64
65 int tds_raw_cond_init(tds_condition *cond);
66 static inline int tds_raw_cond_destroy(tds_condition *cond)
67 {
68 return pthread_cond_destroy(cond);
69 }
70 static inline int tds_raw_cond_signal(tds_condition *cond)
71 {
72 return pthread_cond_signal(cond);
73 }
74 static inline int tds_raw_cond_wait(tds_condition *cond, tds_raw_mutex *mtx)
75 {
76 return pthread_cond_wait(cond, mtx);
77 }
78 int tds_raw_cond_timedwait(tds_condition *cond, tds_raw_mutex *mtx, int timeout_sec);
79
80 #define TDS_HAVE_MUTEX 1
81
82 typedef pthread_t tds_thread;
83 typedef pthread_t tds_thread_id;
84 typedef 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
89 static 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
94 static 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
103 static inline int tds_thread_join(tds_thread th, void **ret)
104 {
105 return pthread_join(th, ret);
106 }
107
108 static inline tds_thread_id tds_thread_get_current_id(void)
109 {
110 return pthread_self();
111 }
112
113 static 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
130 struct ptw32_mcs_node_t_;
131
132 typedef struct {
133 struct ptw32_mcs_node_t_ *lock;
134 LONG done;
135 DWORD thread_id;
136 CRITICAL_SECTION crit;
137 } tds_raw_mutex;
138
139 #define TDS_RAW_MUTEX_INITIALIZER { NULL, 0, 0 }
140
141 static inline int
142 tds_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
150 void tds_win_mutex_lock(tds_raw_mutex *mutex);
151
152 static 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
162 int tds_raw_mutex_trylock(tds_raw_mutex *mtx);
163
164 static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
165 {
166 mtx->thread_id = 0;
167 LeaveCriticalSection(&mtx->crit);
168 }
169
170 static 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 */
181 typedef void *TDS_CONDITION_VARIABLE;
182 typedef union {
183 HANDLE ev;
184 TDS_CONDITION_VARIABLE cv;
185 } tds_condition;
186
187 extern int (*tds_raw_cond_init)(tds_condition *cond);
188 extern int (*tds_raw_cond_destroy)(tds_condition *cond);
189 extern int (*tds_raw_cond_signal)(tds_condition *cond);
190 extern int (*tds_raw_cond_timedwait)(tds_condition *cond, tds_raw_mutex *mtx, int timeout_sec);
191 static 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
196 typedef HANDLE tds_thread;
197 typedef DWORD tds_thread_id;
198 typedef 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
203 static 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
209 static 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
218 static 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
235 static inline tds_thread_id tds_thread_get_current_id(void)
236 {
237 return GetCurrentThreadId();
238 }
239
240 static 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" */
250 typedef struct {
251 char dummy[0]; /* compiler compatibility */
252 } tds_raw_mutex;
253
254 #define TDS_RAW_MUTEX_INITIALIZER {}
255
256 static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
257 {
258 }
259
260 static inline int tds_raw_mutex_trylock(tds_raw_mutex *mtx)
261 {
262 return 0;
263 }
264
265 static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
266 {
267 }
268
269 static inline int tds_raw_mutex_init(tds_raw_mutex *mtx)
270 {
271 return 0;
272 }
273
274 static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
275 {
276 }
277
278 typedef struct {
279 char dummy[0]; /* compiler compatibility */
280 } tds_condition;
281
282 static inline int tds_raw_cond_init(tds_condition *cond)
283 {
284 return 0;
285 }
286 static 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
299 typedef struct {
300 char dummy[0]; /* compiler compatibility */
301 } tds_thread;
302 typedef int tds_thread_id;
303
304 typedef 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
318 static inline tds_thread_id tds_thread_get_current_id(void)
319 {
320 return 0;
321 }
322
323 static 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
347 typedef 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
356 static 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
365 static 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
378 static 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
385 static 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
395 static inline int tds_mutex_init(tds_mutex *mtx)
396 {
397 mtx->locked = 0;
398 return tds_raw_mutex_init(&mtx->mtx);
399 }
400
401 static inline void tds_mutex_free(tds_mutex *mtx)
402 {
403 assert(mtx && !mtx->locked);
404 tds_raw_mutex_free(&mtx->mtx);
405 }
406
407 static 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
418 static 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
ptw32_mcs_node_t_
Definition: ptw32_MCS_lock.c:98
tds_raw_mutex
Definition: thread.h:250
tds_thread
Definition: thread.h:299
tds_condition
Definition: thread.h:278