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  DWORD r;
222  if (ret && GetExitCodeThread(th, &r))
223  *ret = (void*) (((char*)0) + r);
224 
225  CloseHandle(th);
226  return 0;
227  }
228  CloseHandle(th);
229  return 22 /* EINVAL */;
230 }
231 
232 static inline tds_thread_id tds_thread_get_current_id(void)
233 {
234  return GetCurrentThreadId();
235 }
236 
237 static inline int tds_thread_is_current(tds_thread_id th)
238 {
239  return th == GetCurrentThreadId();
240 }
241 
242 #else
243 
244 #include <tds_sysdep_public.h>
245 
246 /* define noops as "successful" */
247 typedef struct {
248  char dummy[0]; /* compiler compatibility */
249 } tds_raw_mutex;
250 
251 #define TDS_RAW_MUTEX_INITIALIZER {}
252 
253 static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
254 {
255 }
256 
257 static inline int tds_raw_mutex_trylock(tds_raw_mutex *mtx)
258 {
259  return 0;
260 }
261 
262 static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
263 {
264 }
265 
266 static inline int tds_raw_mutex_init(tds_raw_mutex *mtx)
267 {
268  return 0;
269 }
270 
271 static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
272 {
273 }
274 
275 typedef struct {
276  char dummy[0]; /* compiler compatibility */
277 } tds_condition;
278 
279 static inline int tds_raw_cond_init(tds_condition *cond)
280 {
281  return 0;
282 }
283 static inline int tds_raw_cond_destroy(tds_condition *cond)
284 {
285  return 0;
286 }
287 #define tds_raw_cond_signal(cond) \
288  FreeTDS_Condition_not_compiled
289 
290 #define tds_raw_cond_wait(cond, mtx) \
291  FreeTDS_Condition_not_compiled
292 
293 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \
294  FreeTDS_Condition_not_compiled
295 
296 typedef struct {
297  char dummy[0]; /* compiler compatibility */
298 } tds_thread;
299 typedef int tds_thread_id;
300 
301 typedef void *(*tds_thread_proc)(void *arg);
302 #define TDS_THREAD_PROC_DECLARE(name, arg) \
303  void *name(void *arg)
304 #define TDS_THREAD_RESULT(n) ((void*)(intptr_t)(n))
305 
306 #define tds_thread_create(ret, proc, arg) \
307  FreeTDS_Thread_not_compiled
308 
309 #define tds_thread_create_detached(proc, arg) \
310  FreeTDS_Thread_not_compiled
311 
312 #define tds_thread_join(th, ret) \
313  FreeTDS_Thread_not_compiled
314 
315 static inline tds_thread_id tds_thread_get_current_id(void)
316 {
317  return 0;
318 }
319 
320 static inline int tds_thread_is_current(tds_thread_id th)
321 {
322  return 1;
323 }
324 
325 #endif
326 
327 # define tds_cond_init tds_raw_cond_init
328 # define tds_cond_destroy tds_raw_cond_destroy
329 # define tds_cond_signal tds_raw_cond_signal
330 # if !ENABLE_EXTRA_CHECKS
331 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER
332 # define tds_mutex tds_raw_mutex
333 # define tds_mutex_lock tds_raw_mutex_lock
334 # define tds_mutex_trylock tds_raw_mutex_trylock
335 # define tds_mutex_unlock tds_raw_mutex_unlock
336 # define tds_mutex_check_owned(mtx) do {} while(0)
337 # define tds_mutex_init tds_raw_mutex_init
338 # define tds_mutex_free tds_raw_mutex_free
339 # define tds_cond_wait tds_raw_cond_wait
340 # define tds_cond_timedwait tds_raw_cond_timedwait
341 # else
342 # include <assert.h>
343 
344 typedef struct tds_mutex
345 {
346  tds_raw_mutex mtx;
347  volatile int locked;
348  volatile tds_thread_id locked_by;
349 } tds_mutex;
350 
351 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 }
352 
353 static inline void tds_mutex_lock(tds_mutex *mtx)
354 {
355  assert(mtx);
356  tds_raw_mutex_lock(&mtx->mtx);
357  assert(!mtx->locked);
358  mtx->locked = 1;
359  mtx->locked_by = tds_thread_get_current_id();
360 }
361 
362 static inline int tds_mutex_trylock(tds_mutex *mtx)
363 {
364  int ret;
365  assert(mtx);
366  ret = tds_raw_mutex_trylock(&mtx->mtx);
367  if (!ret) {
368  assert(!mtx->locked);
369  mtx->locked = 1;
370  mtx->locked_by = tds_thread_get_current_id();
371  }
372  return ret;
373 }
374 
375 static inline void tds_mutex_unlock(tds_mutex *mtx)
376 {
377  assert(mtx && mtx->locked);
378  mtx->locked = 0;
379  tds_raw_mutex_unlock(&mtx->mtx);
380 }
381 
382 static inline void tds_mutex_check_owned(tds_mutex *mtx)
383 {
384  int ret;
385  assert(mtx);
386  ret = tds_raw_mutex_trylock(&mtx->mtx);
387  assert(ret);
388  assert(mtx->locked);
389  assert(tds_thread_is_current(mtx->locked_by));
390 }
391 
392 static inline int tds_mutex_init(tds_mutex *mtx)
393 {
394  mtx->locked = 0;
395  return tds_raw_mutex_init(&mtx->mtx);
396 }
397 
398 static inline void tds_mutex_free(tds_mutex *mtx)
399 {
400  assert(mtx && !mtx->locked);
401  tds_raw_mutex_free(&mtx->mtx);
402 }
403 
404 static inline int tds_cond_wait(tds_condition *cond, tds_mutex *mtx)
405 {
406  int ret;
407  assert(mtx && mtx->locked);
408  mtx->locked = 0;
409  ret = tds_raw_cond_wait(cond, &mtx->mtx);
410  mtx->locked = 1;
411  mtx->locked_by = tds_thread_get_current_id();
412  return ret;
413 }
414 
415 static inline int tds_cond_timedwait(tds_condition *cond, tds_mutex *mtx, int timeout_sec)
416 {
417  int ret;
418  assert(mtx && mtx->locked);
419  mtx->locked = 0;
420  ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
421  mtx->locked = 1;
422  mtx->locked_by = tds_thread_get_current_id();
423  return ret;
424 }
425 
426 # endif
427 
428 #endif
Definition: thread.h:296
Definition: thread.h:275
Definition: ptw32_MCS_lock.c:97
Definition: thread.h:247