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