FreeTDS API: include/freetds/thread.h Source File

FreeTDS API

- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List
- File Members

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 /* $Id: tdsthread.h,v 1.13 2011-09-09 08:50:47 freddy77 Exp $ */
26
27 #undef TDS_HAVE_MUTEX
28
29 #if defined(_THREAD_SAFE) && defined(TDS_HAVE_PTHREAD_MUTEX)
30
31 #include <pthread.h>
32
33 #include <freetds/pushvis.h>
34
35 typedef pthread_mutex_t tds_mutex;
36 #define TDS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
37
38 static inline void tds_mutex_lock(tds_mutex *mtx)
39 {
40 pthread_mutex_lock(mtx);
41 }
42
43 static inline int tds_mutex_trylock(tds_mutex *mtx)
44 {
45 return pthread_mutex_trylock(mtx);
46 }
47
48 static inline void tds_mutex_unlock(tds_mutex *mtx)
49 {
50 pthread_mutex_unlock(mtx);
51 }
52
53 static inline int tds_mutex_init(tds_mutex *mtx)
54 {
55 return pthread_mutex_init(mtx, NULL);
56 }
57
58 static inline void tds_mutex_free(tds_mutex *mtx)
59 {
60 pthread_mutex_destroy(mtx);
61 }
62
63 typedef pthread_cond_t tds_condition;
64
65 int tds_cond_init(tds_condition *cond);
66 static inline int tds_cond_destroy(tds_condition *cond)
67 {
68 return pthread_cond_destroy(cond);
69 }
70 static inline int tds_cond_signal(tds_condition *cond)
71 {
72 return pthread_cond_signal(cond);
73 }
74 static inline int tds_cond_wait(tds_condition *cond, pthread_mutex_t *mtx)
75 {
76 return pthread_cond_wait(cond, mtx);
77 }
78 int tds_cond_timedwait(tds_condition *cond, pthread_mutex_t *mtx, int timeout_sec);
79
80 #define TDS_HAVE_MUTEX 1
81
82 typedef pthread_t tds_thread;
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_join(tds_thread th, void **ret)
93 {
94 return pthread_join(th, ret);
95 }
96
97 #include <freetds/popvis.h>
98
99 #elif defined(_WIN32)
100
101 struct ptw32_mcs_node_t_;
102
103 typedef struct {
104 struct ptw32_mcs_node_t_ *lock;
105 LONG done;
106 CRITICAL_SECTION crit;
107 } tds_mutex;
108
109 #define TDS_MUTEX_INITIALIZER { NULL, 0 }
110
111 static inline int
112 tds_mutex_init(tds_mutex *mtx)
113 {
114 mtx->lock = NULL;
115 mtx->done = 0;
116 return 0;
117 }
118
119 void tds_win_mutex_lock(tds_mutex *mutex);
120
121 static inline void tds_mutex_lock(tds_mutex *mtx)
122 {
123 if ((mtx)->done)
124 EnterCriticalSection(&(mtx)->crit);
125 else
126 tds_win_mutex_lock(mtx);
127 }
128
129 int tds_mutex_trylock(tds_mutex *mtx);
130
131 static inline void tds_mutex_unlock(tds_mutex *mtx)
132 {
133 LeaveCriticalSection(&(mtx)->crit);
134 }
135
136 static inline void tds_mutex_free(tds_mutex *mtx)
137 {
138 if ((mtx)->done) {
139 DeleteCriticalSection(&(mtx)->crit);
140 (mtx)->done = 0;
141 }
142 }
143
144 #define TDS_HAVE_MUTEX 1
145
146 /* easy way, only single signal supported */
147 typedef void *TDS_CONDITION_VARIABLE;
148 typedef union {
149 HANDLE ev;
150 TDS_CONDITION_VARIABLE cv;
151 } tds_condition;
152
153 extern int (*tds_cond_init)(tds_condition *cond);
154 extern int (*tds_cond_destroy)(tds_condition *cond);
155 extern int (*tds_cond_signal)(tds_condition *cond);
156 extern int (*tds_cond_timedwait)(tds_condition *cond, tds_mutex *mtx, int timeout_sec);
157 static inline int tds_cond_wait(tds_condition *cond, tds_mutex *mtx)
158 {
159 return tds_cond_timedwait(cond, mtx, -1);
160 }
161
162 typedef HANDLE tds_thread;
163 typedef void *(WINAPI *tds_thread_proc)(void *arg);
164 #define TDS_THREAD_PROC_DECLARE(name, arg) \
165 void *WINAPI name(void *arg)
166
167 static inline int tds_thread_create(tds_thread *ret, tds_thread_proc proc, void *arg)
168 {
169 *ret = CreateThread(NULL, 0, (DWORD (WINAPI *)(void*)) proc, arg, 0, NULL);
170 return *ret != NULL ? 0 : 11 /* EAGAIN */;
171 }
172
173 static inline int tds_thread_join(tds_thread th, void **ret)
174 {
175 if (WaitForSingleObject(th, INFINITE) == WAIT_OBJECT_0) {
176 DWORD r;
177 if (ret && GetExitCodeThread(th, &r))
178 *ret = (void*) (((char*)0) + r);
179
180 CloseHandle(th);
181 return 0;
182 }
183 CloseHandle(th);
184 return 22 /* EINVAL */;
185 }
186
187 #else
188
189 /* define noops as "successful" */
190 typedef struct {
191 } tds_mutex;
192
193 #define TDS_MUTEX_INITIALIZER {}
194
195 static inline void tds_mutex_lock(tds_mutex *mtx)
196 {
197 }
198
199 static inline int tds_mutex_trylock(tds_mutex *mtx)
200 {
201 return 0;
202 }
203
204 static inline void tds_mutex_unlock(tds_mutex *mtx)
205 {
206 }
207
208 static inline int tds_mutex_init(tds_mutex *mtx)
209 {
210 return 0;
211 }
212
213 static inline void tds_mutex_free(tds_mutex *mtx)
214 {
215 }
216
217 typedef struct {
218 } tds_condition;
219
220 static inline int tds_cond_init(tds_condition *cond)
221 {
222 return 0;
223 }
224 static inline int tds_cond_destroy(tds_condition *cond)
225 {
226 return 0;
227 }
228 #define tds_cond_signal(cond) \
229 FreeTDS_Condition_not_compiled
230
231 #define tds_cond_wait(cond, mtx) \
232 FreeTDS_Condition_not_compiled
233
234 #define tds_cond_timedwait(cond, mtx, timeout_sec) \
235 FreeTDS_Condition_not_compiled
236
237 typedef struct {
238 } tds_thread;
239
240 typedef void *(*tds_thread_proc)(void *arg);
241 #define TDS_THREAD_PROC_DECLARE(name, arg) \
242 void *name(void *arg)
243
244 #define tds_thread_create(ret, proc, arg) \
245 FreeTDS_Thread_not_compiled
246
247 #define tds_thread_join(th, ret) \
248 FreeTDS_Thread_not_compiled
249
250 #endif
251
252 #endif
tds_mutex
Definition: thread.h:190
tds_thread
Definition: thread.h:237
ptw32_mcs_node_t_
Definition: ptw32_MCS_lock.c:97
tds_condition
Definition: thread.h:217