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 <pthread.h>
30 #include <errno.h>
31 
32 #include <freetds/pushvis.h>
33 
34 typedef pthread_mutex_t tds_raw_mutex;
35 #define TDS_RAW_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
36 
37 static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
38 {
39  pthread_mutex_lock(mtx);
40 }
41 
42 static inline int tds_raw_mutex_trylock(tds_raw_mutex *mtx)
43 {
44  return pthread_mutex_trylock(mtx);
45 }
46 
47 static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
48 {
49  pthread_mutex_unlock(mtx);
50 }
51 
52 static inline int tds_raw_mutex_init(tds_raw_mutex *mtx)
53 {
54  return pthread_mutex_init(mtx, NULL);
55 }
56 
57 static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
58 {
59  pthread_mutex_destroy(mtx);
60 }
61 
62 typedef pthread_cond_t tds_condition;
63 
64 int tds_raw_cond_init(tds_condition *cond);
65 static inline int tds_raw_cond_destroy(tds_condition *cond)
66 {
67  return pthread_cond_destroy(cond);
68 }
69 static inline int tds_raw_cond_signal(tds_condition *cond)
70 {
71  return pthread_cond_signal(cond);
72 }
73 static inline int tds_raw_cond_wait(tds_condition *cond, tds_raw_mutex *mtx)
74 {
75  return pthread_cond_wait(cond, mtx);
76 }
77 int tds_raw_cond_timedwait(tds_condition *cond, tds_raw_mutex *mtx, int timeout_sec);
78 
79 #define TDS_HAVE_MUTEX 1
80 
81 typedef pthread_t tds_thread;
82 typedef pthread_t tds_thread_id;
83 typedef void *(*tds_thread_proc)(void *arg);
84 #define TDS_THREAD_PROC_DECLARE(name, arg) \
85  void *name(void *arg)
86 
87 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc, void *arg)
88 {
89  return pthread_create(ret, NULL, proc, arg);
90 }
91 
92 static inline int tds_thread_create_detached(tds_thread_proc proc, void *arg)
93 {
94  tds_thread th;
95  int ret = pthread_create(&th, NULL, proc, arg);
96  if (!ret)
97  pthread_detach(th);
98  return ret;
99 }
100 
101 static inline int tds_thread_join(tds_thread th, void **ret)
102 {
103  return pthread_join(th, ret);
104 }
105 
106 static inline tds_thread_id tds_thread_get_current_id(void)
107 {
108  return pthread_self();
109 }
110 
111 static inline int tds_thread_is_current(tds_thread_id th)
112 {
113  return pthread_equal(th, pthread_self());
114 }
115 
116 #include <freetds/popvis.h>
117 
118 #elif defined(_WIN32)
119 
120 #include <windows.h>
121 #include <errno.h>
122 
123 /* old version of Windows do not define this constant */
124 #ifndef ETIMEDOUT
125 #define ETIMEDOUT 138
126 #endif
127 
128 struct ptw32_mcs_node_t_;
129 
130 typedef struct {
131  struct ptw32_mcs_node_t_ *lock;
132  LONG done;
133  CRITICAL_SECTION crit;
134 } tds_raw_mutex;
135 
136 #define TDS_RAW_MUTEX_INITIALIZER { NULL, 0 }
137 
138 static inline int
139 tds_raw_mutex_init(tds_raw_mutex *mtx)
140 {
141  mtx->lock = NULL;
142  mtx->done = 0;
143  return 0;
144 }
145 
146 void tds_win_mutex_lock(tds_raw_mutex *mutex);
147 
148 static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
149 {
150  if ((mtx)->done)
151  EnterCriticalSection(&(mtx)->crit);
152  else
153  tds_win_mutex_lock(mtx);
154 }
155 
156 int tds_raw_mutex_trylock(tds_raw_mutex *mtx);
157 
158 static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
159 {
160  LeaveCriticalSection(&(mtx)->crit);
161 }
162 
163 static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
164 {
165  if ((mtx)->done) {
166  DeleteCriticalSection(&(mtx)->crit);
167  (mtx)->done = 0;
168  }
169 }
170 
171 #define TDS_HAVE_MUTEX 1
172 
173 /* easy way, only single signal supported */
174 typedef void *TDS_CONDITION_VARIABLE;
175 typedef union {
176  HANDLE ev;
177  TDS_CONDITION_VARIABLE cv;
178 } tds_condition;
179 
180 extern int (*tds_raw_cond_init)(tds_condition *cond);
181 extern int (*tds_raw_cond_destroy)(tds_condition *cond);
182 extern int (*tds_raw_cond_signal)(tds_condition *cond);
183 extern int (*tds_raw_cond_timedwait)(tds_condition *cond, tds_raw_mutex *mtx, int timeout_sec);
184 static inline int tds_raw_cond_wait(tds_condition *cond, tds_raw_mutex *mtx)
185 {
186  return tds_raw_cond_timedwait(cond, mtx, -1);
187 }
188 
189 typedef HANDLE tds_thread;
190 typedef DWORD tds_thread_id;
191 typedef void *(WINAPI *tds_thread_proc)(void *arg);
192 #define TDS_THREAD_PROC_DECLARE(name, arg) \
193  void *WINAPI name(void *arg)
194 
195 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc, void *arg)
196 {
197  *ret = CreateThread(NULL, 0, (DWORD (WINAPI *)(void*)) proc, arg, 0, NULL);
198  return *ret != NULL ? 0 : 11 /* EAGAIN */;
199 }
200 
201 static inline int tds_thread_create_detached(tds_thread_proc proc, void *arg)
202 {
203  HANDLE h = CreateThread(NULL, 0, (DWORD (WINAPI *)(void*)) proc, arg, 0, NULL);
204  if (h)
205  return 0;
206  CloseHandle(h);
207  return 11 /* EAGAIN */;
208 }
209 
210 static inline int tds_thread_join(tds_thread th, void **ret)
211 {
212  if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
213  DWORD r;
214  if (ret && GetExitCodeThread(th, &r))
215  *ret = (void*) (((char*)0) + r);
216 
217  CloseHandle(th);
218  return 0;
219  }
220  CloseHandle(th);
221  return 22 /* EINVAL */;
222 }
223 
224 static inline tds_thread_id tds_thread_get_current_id(void)
225 {
226  return GetCurrentThreadId();
227 }
228 
229 static inline int tds_thread_is_current(tds_thread_id th)
230 {
231  return th == GetCurrentThreadId();
232 }
233 
234 #else
235 
236 /* define noops as "successful" */
237 typedef struct {
238 } tds_raw_mutex;
239 
240 #define TDS_RAW_MUTEX_INITIALIZER {}
241 
242 static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
243 {
244 }
245 
246 static inline int tds_raw_mutex_trylock(tds_raw_mutex *mtx)
247 {
248  return 0;
249 }
250 
251 static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
252 {
253 }
254 
255 static inline int tds_raw_mutex_init(tds_raw_mutex *mtx)
256 {
257  return 0;
258 }
259 
260 static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
261 {
262 }
263 
264 typedef struct {
265 } tds_condition;
266 
267 static inline int tds_raw_cond_init(tds_condition *cond)
268 {
269  return 0;
270 }
271 static inline int tds_raw_cond_destroy(tds_condition *cond)
272 {
273  return 0;
274 }
275 #define tds_raw_cond_signal(cond) \
276  FreeTDS_Condition_not_compiled
277 
278 #define tds_raw_cond_wait(cond, mtx) \
279  FreeTDS_Condition_not_compiled
280 
281 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \
282  FreeTDS_Condition_not_compiled
283 
284 typedef struct {
285 } tds_thread;
286 typedef int tds_thread_id;
287 
288 typedef void *(*tds_thread_proc)(void *arg);
289 #define TDS_THREAD_PROC_DECLARE(name, arg) \
290  void *name(void *arg)
291 
292 #define tds_thread_create(ret, proc, arg) \
293  FreeTDS_Thread_not_compiled
294 
295 #define tds_thread_create_detached(proc, arg) \
296  FreeTDS_Thread_not_compiled
297 
298 #define tds_thread_join(th, ret) \
299  FreeTDS_Thread_not_compiled
300 
301 static inline tds_thread_id tds_thread_get_current_id(void)
302 {
303  return 0;
304 }
305 
306 static inline int tds_thread_is_current(tds_thread_id th)
307 {
308  return 1;
309 }
310 
311 
312 #endif
313 
314 #ifdef TDS_HAVE_MUTEX
315 # define tds_cond_init tds_raw_cond_init
316 # define tds_cond_destroy tds_raw_cond_destroy
317 # define tds_cond_signal tds_raw_cond_signal
318 # if !ENABLE_EXTRA_CHECKS
319 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER
320 # define tds_mutex tds_raw_mutex
321 # define tds_mutex_lock tds_raw_mutex_lock
322 # define tds_mutex_trylock tds_raw_mutex_trylock
323 # define tds_mutex_unlock tds_raw_mutex_unlock
324 # define tds_mutex_check_owned(mtx) do {} while(0)
325 # define tds_mutex_init tds_raw_mutex_init
326 # define tds_mutex_free tds_raw_mutex_free
327 # define tds_cond_wait tds_raw_cond_wait
328 # define tds_cond_timedwait tds_raw_cond_timedwait
329 # else
330 # include <assert.h>
331 
332 typedef struct tds_mutex
333 {
334  tds_raw_mutex mtx;
335  volatile int locked;
336  volatile tds_thread_id locked_by;
337 } tds_mutex;
338 
339 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 }
340 
341 static inline void tds_mutex_lock(tds_mutex *mtx)
342 {
343  assert(mtx);
344  tds_raw_mutex_lock(&mtx->mtx);
345  assert(!mtx->locked);
346  mtx->locked = 1;
347  mtx->locked_by = tds_thread_get_current_id();
348 }
349 
350 static inline int tds_mutex_trylock(tds_mutex *mtx)
351 {
352  int ret;
353  assert(mtx);
354  ret = tds_raw_mutex_trylock(&mtx->mtx);
355  if (!ret) {
356  assert(!mtx->locked);
357  mtx->locked = 1;
358  mtx->locked_by = tds_thread_get_current_id();
359  }
360  return ret;
361 }
362 
363 static inline void tds_mutex_unlock(tds_mutex *mtx)
364 {
365  assert(mtx && mtx->locked);
366  mtx->locked = 0;
367  tds_raw_mutex_unlock(&mtx->mtx);
368 }
369 
370 static inline void tds_mutex_check_owned(tds_mutex *mtx)
371 {
372  int ret;
373  assert(mtx);
374  ret = tds_raw_mutex_trylock(&mtx->mtx);
375  assert(ret);
376  assert(mtx->locked);
377  assert(tds_thread_is_current(mtx->locked_by));
378 }
379 
380 static inline int tds_mutex_init(tds_mutex *mtx)
381 {
382  mtx->locked = 0;
383  return tds_raw_mutex_init(&mtx->mtx);
384 }
385 
386 static inline void tds_mutex_free(tds_mutex *mtx)
387 {
388  assert(mtx && !mtx->locked);
389  tds_raw_mutex_free(&mtx->mtx);
390 }
391 
392 static inline int tds_cond_wait(tds_condition *cond, tds_mutex *mtx)
393 {
394  int ret;
395  assert(mtx && mtx->locked);
396  mtx->locked = 0;
397  ret = tds_raw_cond_wait(cond, &mtx->mtx);
398  mtx->locked = 1;
399  mtx->locked_by = tds_thread_get_current_id();
400  return ret;
401 }
402 
403 static inline int tds_cond_timedwait(tds_condition *cond, tds_mutex *mtx, int timeout_sec)
404 {
405  int ret;
406  assert(mtx && mtx->locked);
407  mtx->locked = 0;
408  ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
409  mtx->locked = 1;
410  mtx->locked_by = tds_thread_get_current_id();
411  return ret;
412 }
413 
414 # endif
415 #endif
416 
417 #endif
Definition: thread.h:264
Definition: thread.h:237
Definition: thread.h:284
Definition: ptw32_MCS_lock.c:97