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