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  DWORD thread_id;
134  CRITICAL_SECTION crit;
135 } tds_raw_mutex;
136 
137 #define TDS_RAW_MUTEX_INITIALIZER { NULL, 0, 0 }
138 
139 static inline int
140 tds_raw_mutex_init(tds_raw_mutex *mtx)
141 {
142  mtx->lock = NULL;
143  mtx->done = 0;
144  mtx->thread_id = 0;
145  return 0;
146 }
147 
148 void tds_win_mutex_lock(tds_raw_mutex *mutex);
149 
150 static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
151 {
152  if (mtx->done) {
153  EnterCriticalSection(&mtx->crit);
154  mtx->thread_id = GetCurrentThreadId();
155  } else {
156  tds_win_mutex_lock(mtx);
157  }
158 }
159 
160 int tds_raw_mutex_trylock(tds_raw_mutex *mtx);
161 
162 static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
163 {
164  mtx->thread_id = 0;
165  LeaveCriticalSection(&mtx->crit);
166 }
167 
168 static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
169 {
170  if (mtx->done) {
171  DeleteCriticalSection(&mtx->crit);
172  mtx->done = 0;
173  }
174 }
175 
176 #define TDS_HAVE_MUTEX 1
177 
178 /* easy way, only single signal supported */
179 typedef void *TDS_CONDITION_VARIABLE;
180 typedef union {
181  HANDLE ev;
182  TDS_CONDITION_VARIABLE cv;
183 } tds_condition;
184 
185 extern int (*tds_raw_cond_init)(tds_condition *cond);
186 extern int (*tds_raw_cond_destroy)(tds_condition *cond);
187 extern int (*tds_raw_cond_signal)(tds_condition *cond);
188 extern int (*tds_raw_cond_timedwait)(tds_condition *cond, tds_raw_mutex *mtx, int timeout_sec);
189 static inline int tds_raw_cond_wait(tds_condition *cond, tds_raw_mutex *mtx)
190 {
191  return tds_raw_cond_timedwait(cond, mtx, -1);
192 }
193 
194 typedef HANDLE tds_thread;
195 typedef DWORD tds_thread_id;
196 typedef void *(WINAPI *tds_thread_proc)(void *arg);
197 #define TDS_THREAD_PROC_DECLARE(name, arg) \
198  void *WINAPI name(void *arg)
199 
200 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc, void *arg)
201 {
202  *ret = CreateThread(NULL, 0, (DWORD (WINAPI *)(void*)) proc, arg, 0, NULL);
203  return *ret != NULL ? 0 : 11 /* EAGAIN */;
204 }
205 
206 static inline int tds_thread_create_detached(tds_thread_proc proc, void *arg)
207 {
208  HANDLE h = CreateThread(NULL, 0, (DWORD (WINAPI *)(void*)) proc, arg, 0, NULL);
209  if (h)
210  return 0;
211  CloseHandle(h);
212  return 11 /* EAGAIN */;
213 }
214 
215 static inline int tds_thread_join(tds_thread th, void **ret)
216 {
217  if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
218  DWORD r;
219  if (ret && GetExitCodeThread(th, &r))
220  *ret = (void*) (((char*)0) + r);
221 
222  CloseHandle(th);
223  return 0;
224  }
225  CloseHandle(th);
226  return 22 /* EINVAL */;
227 }
228 
229 static inline tds_thread_id tds_thread_get_current_id(void)
230 {
231  return GetCurrentThreadId();
232 }
233 
234 static inline int tds_thread_is_current(tds_thread_id th)
235 {
236  return th == GetCurrentThreadId();
237 }
238 
239 #else
240 
241 /* define noops as "successful" */
242 typedef struct {
243 } tds_raw_mutex;
244 
245 #define TDS_RAW_MUTEX_INITIALIZER {}
246 
247 static inline void tds_raw_mutex_lock(tds_raw_mutex *mtx)
248 {
249 }
250 
251 static inline int tds_raw_mutex_trylock(tds_raw_mutex *mtx)
252 {
253  return 0;
254 }
255 
256 static inline void tds_raw_mutex_unlock(tds_raw_mutex *mtx)
257 {
258 }
259 
260 static inline int tds_raw_mutex_init(tds_raw_mutex *mtx)
261 {
262  return 0;
263 }
264 
265 static inline void tds_raw_mutex_free(tds_raw_mutex *mtx)
266 {
267 }
268 
269 typedef struct {
270 } tds_condition;
271 
272 static inline int tds_raw_cond_init(tds_condition *cond)
273 {
274  return 0;
275 }
276 static inline int tds_raw_cond_destroy(tds_condition *cond)
277 {
278  return 0;
279 }
280 #define tds_raw_cond_signal(cond) \
281  FreeTDS_Condition_not_compiled
282 
283 #define tds_raw_cond_wait(cond, mtx) \
284  FreeTDS_Condition_not_compiled
285 
286 #define tds_raw_cond_timedwait(cond, mtx, timeout_sec) \
287  FreeTDS_Condition_not_compiled
288 
289 typedef struct {
290 } tds_thread;
291 typedef int tds_thread_id;
292 
293 typedef void *(*tds_thread_proc)(void *arg);
294 #define TDS_THREAD_PROC_DECLARE(name, arg) \
295  void *name(void *arg)
296 
297 #define tds_thread_create(ret, proc, arg) \
298  FreeTDS_Thread_not_compiled
299 
300 #define tds_thread_create_detached(proc, arg) \
301  FreeTDS_Thread_not_compiled
302 
303 #define tds_thread_join(th, ret) \
304  FreeTDS_Thread_not_compiled
305 
306 static inline tds_thread_id tds_thread_get_current_id(void)
307 {
308  return 0;
309 }
310 
311 static inline int tds_thread_is_current(tds_thread_id th)
312 {
313  return 1;
314 }
315 
316 
317 #endif
318 
319 #ifdef TDS_HAVE_MUTEX
320 # define tds_cond_init tds_raw_cond_init
321 # define tds_cond_destroy tds_raw_cond_destroy
322 # define tds_cond_signal tds_raw_cond_signal
323 # if !ENABLE_EXTRA_CHECKS
324 # define TDS_MUTEX_INITIALIZER TDS_RAW_MUTEX_INITIALIZER
325 # define tds_mutex tds_raw_mutex
326 # define tds_mutex_lock tds_raw_mutex_lock
327 # define tds_mutex_trylock tds_raw_mutex_trylock
328 # define tds_mutex_unlock tds_raw_mutex_unlock
329 # define tds_mutex_check_owned(mtx) do {} while(0)
330 # define tds_mutex_init tds_raw_mutex_init
331 # define tds_mutex_free tds_raw_mutex_free
332 # define tds_cond_wait tds_raw_cond_wait
333 # define tds_cond_timedwait tds_raw_cond_timedwait
334 # else
335 # include <assert.h>
336 
337 typedef struct tds_mutex
338 {
339  tds_raw_mutex mtx;
340  volatile int locked;
341  volatile tds_thread_id locked_by;
342 } tds_mutex;
343 
344 # define TDS_MUTEX_INITIALIZER { TDS_RAW_MUTEX_INITIALIZER, 0 }
345 
346 static inline void tds_mutex_lock(tds_mutex *mtx)
347 {
348  assert(mtx);
349  tds_raw_mutex_lock(&mtx->mtx);
350  assert(!mtx->locked);
351  mtx->locked = 1;
352  mtx->locked_by = tds_thread_get_current_id();
353 }
354 
355 static inline int tds_mutex_trylock(tds_mutex *mtx)
356 {
357  int ret;
358  assert(mtx);
359  ret = tds_raw_mutex_trylock(&mtx->mtx);
360  if (!ret) {
361  assert(!mtx->locked);
362  mtx->locked = 1;
363  mtx->locked_by = tds_thread_get_current_id();
364  }
365  return ret;
366 }
367 
368 static inline void tds_mutex_unlock(tds_mutex *mtx)
369 {
370  assert(mtx && mtx->locked);
371  mtx->locked = 0;
372  tds_raw_mutex_unlock(&mtx->mtx);
373 }
374 
375 static inline void tds_mutex_check_owned(tds_mutex *mtx)
376 {
377  int ret;
378  assert(mtx);
379  ret = tds_raw_mutex_trylock(&mtx->mtx);
380  assert(ret);
381  assert(mtx->locked);
382  assert(tds_thread_is_current(mtx->locked_by));
383 }
384 
385 static inline int tds_mutex_init(tds_mutex *mtx)
386 {
387  mtx->locked = 0;
388  return tds_raw_mutex_init(&mtx->mtx);
389 }
390 
391 static inline void tds_mutex_free(tds_mutex *mtx)
392 {
393  assert(mtx && !mtx->locked);
394  tds_raw_mutex_free(&mtx->mtx);
395 }
396 
397 static inline int tds_cond_wait(tds_condition *cond, tds_mutex *mtx)
398 {
399  int ret;
400  assert(mtx && mtx->locked);
401  mtx->locked = 0;
402  ret = tds_raw_cond_wait(cond, &mtx->mtx);
403  mtx->locked = 1;
404  mtx->locked_by = tds_thread_get_current_id();
405  return ret;
406 }
407 
408 static inline int tds_cond_timedwait(tds_condition *cond, tds_mutex *mtx, int timeout_sec)
409 {
410  int ret;
411  assert(mtx && mtx->locked);
412  mtx->locked = 0;
413  ret = tds_raw_cond_timedwait(cond, &mtx->mtx, timeout_sec);
414  mtx->locked = 1;
415  mtx->locked_by = tds_thread_get_current_id();
416  return ret;
417 }
418 
419 # endif
420 #endif
421 
422 #endif
Definition: thread.h:289
Definition: thread.h:269
Definition: thread.h:242
Definition: ptw32_MCS_lock.c:97