globus_common  17.9
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
globus_thread.h
Go to the documentation of this file.
1 
9 #if !defined(GLOBUS_THREAD_H)
10 #define GLOBUS_THREAD_H 1
11 
12 /* Include header files */
13 #include "globus_module.h"
14 #include "globus_time.h"
15 
16 #if !defined(_WIN32) || defined(__MINGW32__)
17 #include <unistd.h>
18 #endif
19 
20 #if _POSIX_THREADS
21 #if !defined(HAVE_PTHREAD)
22 #define HAVE_PTHREAD 1
23 #endif
24 #if defined __GNUC__ && defined __EXCEPTIONS
25 #undef __EXCEPTIONS
26 #include <pthread.h>
27 #define __EXCEPTIONS 1
28 #else
29 #include <pthread.h>
30 #endif
31 #endif /* _POSIX_THREADS */
32 
33 #if defined(_WIN32)
34 #include <windows.h>
35 #define HAVE_WINDOWS_THREADS 1
36 #endif
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /*
43  * Default supported thread models (on some systems). Others can conceivably
44  * be dynamically loaded if their implementation can use the dummy block in the
45  * structures.
46  */
47 #define GLOBUS_THREAD_MODEL_NONE "none"
48 #define GLOBUS_THREAD_MODEL_PTHREADS "pthread"
49 #define GLOBUS_THREAD_MODEL_WINDOWS "windows"
50 
55 typedef union
56 {
57  int none;
58 #if HAVE_PTHREAD
59  pthread_t pthread;
60 #endif
61 #if HAVE_WINDOWS_THREADS
62  uintptr_t windows;
63 #endif
64  intptr_t dummy;
65 }
67 
72 typedef union
73 {
74  int none;
75 #if HAVE_PTHREAD
76  pthread_attr_t pthread;
77 #endif
78 #if HAVE_WINDOWS_THREADS
79  void *windows;
80 #endif
81  intptr_t dummy;
82 }
84 
85 typedef void * (*globus_thread_func_t)(void *);
86 
91 typedef union
92 {
93  int none;
94 #if HAVE_PTHREAD
95  pthread_mutex_t pthread;
96 #endif
97 #if HAVE_WINDOWS_THREADS
98  HANDLE windows;
99 #endif
100  intptr_t dummy;
101 }
103 
108 typedef union
109 {
110  int none;
111 #if HAVE_PTHREAD
112  struct globus_cond_pthread_s
113  {
114  pthread_cond_t cond;
115  globus_bool_t poll_space;
116  int space;
117  } pthread;
118 #endif
119 #if HAVE_WINDOWS_THREADS
120  struct globus_cond_windows_s
121  {
122  HANDLE events[2];
123  int numberOfWaiters;
124  }
125  windows;
126 #endif
127  intptr_t dummy;
128 }
130 
135 typedef union
136 {
137  int none;
138 #if HAVE_PTHREAD
139  pthread_mutexattr_t pthread;
140 #endif
141 #if HAVE_WINDOWS_THREADS
142  struct globus_mutexattr_windows_s
143  {
144  LPSECURITY_ATTRIBUTES securityAttributes;
145  } windows;
146 #endif
147  intptr_t dummy;
148 }
150 
155 typedef union
156 {
157  int none;
158 #if HAVE_PTHREAD
159  struct globus_condattr_pthread_s
160  {
161  pthread_condattr_t attr;
162  int space;
163  } pthread;
164 #endif
165 #if HAVE_WINDOWS_THREADS
166  struct globus_condattr_windows_s
167  {
168  LPSECURITY_ATTRIBUTES securityAttributes;
169  } windows;
170 #endif
171  intptr_t dummy;
172 }
174 
179 typedef void (* globus_thread_key_destructor_func_t)(void * value);
180 
185 typedef union
186 {
187  int none;
188 #if HAVE_PTHREAD
189  pthread_key_t pthread;
190 #endif
191 #if HAVE_WINDOWS_THREADS
192  struct globus_thread_key_windows_s
193  {
194  DWORD TLSIndex;
195  globus_thread_key_destructor_func_t destructorFunction;
196  } windows;
197 #endif
198  /*
199  * Backward-compatibility hack for fedora/debian bnaries, must not
200  * be bigger than sizeof(pthread_key_t)
201  */
202  int32_t dummy;
203 }
205 
210 typedef union
211 {
212  int32_t none;
213 #if HAVE_PTHREAD
214  pthread_once_t pthread;
215 #endif
216 #if HAVE_WINDOWS_THREADS
217  int windows;
218 #endif
219  int32_t dummy;
220 }
222 
229 extern const globus_thread_once_t GLOBUS_THREAD_ONCE_INIT_VALUE;
230 #if HAVE_PTHREAD
231 # define GLOBUS_THREAD_ONCE_INIT { .pthread = PTHREAD_ONCE_INIT }
232 #elif HAVE_WINDOWS_THREADS
233 # define GLOBUS_THREAD_ONCE_INIT { .windows = 0 }
234 #else
235 # define GLOBUS_THREAD_ONCE_INIT { .none = 0 }
236 #endif
237 
238 extern
239 int
241  const char * model);
242 
243 extern
244 int
246  globus_mutex_t * mutex,
247  globus_mutexattr_t * attr);
248 
249 extern
250 int
252  globus_mutex_t * mutex);
253 
254 extern
255 int
257  globus_mutex_t * mutex);
258 
259 extern
260 int
262  globus_mutex_t * mutex);
263 
264 extern
265 int
267  globus_mutex_t * mutex);
268 
269 extern
270 int
272  globus_cond_t * cond,
273  globus_condattr_t * attr);
274 
275 extern
276 int
278  globus_cond_t * cond);
279 
280 extern
281 int
283  globus_cond_t * cond,
284  globus_mutex_t * mutex);
285 
286 extern
287 int
289  globus_cond_t * cond,
290  globus_mutex_t * mutex,
291  globus_abstime_t * abstime);
292 
293 extern
294 int
296  globus_cond_t * cond);
297 
298 extern
299 int
301  globus_cond_t * cond);
302 
303 extern
304 int
306  globus_condattr_t * cond_attr);
307 
308 extern
309 int
311  globus_condattr_t * cond_attr);
312 
313 extern
314 int
316  globus_condattr_t * cond_attr,
317  int space);
318 
319 extern
320 int
322  globus_condattr_t * cond_attr,
323  int * space);
324 
325 extern
326 int
328  globus_thread_t * thread,
329  globus_threadattr_t * attr,
330  globus_thread_func_t func,
331  void * user_arg);
332 
333 extern
334 void *
336  globus_thread_key_t key);
337 
338 extern
339 int
342  void * value);
343 
344 extern
345 int
347  globus_thread_key_t * key,
349 
350 extern
351 int
353  globus_thread_key_t key);
354 
355 extern
356 int
358  globus_thread_once_t * once,
359  void (*init_routine)(void));
360 
361 extern
362 void
363 globus_thread_yield(void);
364 
365 extern
366 int
368  int how,
369  const sigset_t * newmask,
370  sigset_t * oldmask);
371 
372 extern
373 int
375  globus_thread_t thread,
376  int sig);
377 
378 extern
379 void
380 globus_thread_exit(void *value);
381 
382 extern
384 globus_thread_self(void);
385 
386 extern
387 int
389  globus_thread_t thread1,
390  globus_thread_t thread2);
391 
392 extern
395 
396 extern
399 
400 extern
401 void *
403  void * (*func)(void *),
404  void * arg,
405  void (*cleanup_func)(void *),
406  void * cleanup_arg,
407  globus_bool_t execute_cleanup);
408 
409 extern
410 int
412 
413 extern
414 void
416 
417 extern
418 int
420  int state,
421  int * oldstate);
422 
428 #define GLOBUS_THREAD_CANCEL_DISABLE 0
429 
434 #define GLOBUS_THREAD_CANCEL_ENABLE 1
435 
436 /* Module definition */
437 extern
438 int
439 globus_i_thread_pre_activate();
440 
441 extern
442 globus_module_descriptor_t globus_i_thread_module;
443 
449 #define GLOBUS_THREAD_MODULE (&globus_i_thread_module)
450 
451 typedef struct
452 {
453  int (*mutex_init)(globus_mutex_t *mutex, globus_mutexattr_t *attr);
454  int (*mutex_destroy)(globus_mutex_t *mutex);
455  int (*mutex_lock)(globus_mutex_t *mutex);
456  int (*mutex_unlock)(globus_mutex_t *mutex);
457  int (*mutex_trylock)(globus_mutex_t *mutex);
458  int (*cond_init)(globus_cond_t *cond, globus_condattr_t *attr);
459  int (*cond_destroy)(globus_cond_t *cond);
460  int (*cond_wait)(globus_cond_t *cond, globus_mutex_t *mutex);
461  int (*cond_timedwait)(globus_cond_t *cond, globus_mutex_t *mutex, globus_abstime_t *abstime);
462  int (*cond_signal)(globus_cond_t *cond);
463  int (*cond_broadcast)(globus_cond_t *cond);
464  int (*mutexattr_init)(globus_mutexattr_t *attr);
465  int (*mutexattr_destroy)(globus_mutexattr_t *attr);
466  int (*condattr_init)(globus_condattr_t *attr);
467  int (*condattr_destroy)(globus_condattr_t *attr);
468  int (*condattr_setspace)(globus_condattr_t *attr, int space);
469  int (*condattr_getspace)(globus_condattr_t *attr, int *space);
470  int (*thread_create)(globus_thread_t *thread, globus_threadattr_t *attr, globus_thread_func_t func, void * user_arg);
471  int (*thread_key_create)(globus_thread_key_t *key, globus_thread_key_destructor_func_t func);
472  int (*thread_key_delete)(globus_thread_key_t key);
473  int (*thread_once)(globus_thread_once_t *once, void (*init_func)(void));
474  void *(*thread_getspecific)(globus_thread_key_t key);
475  int (*thread_setspecific)(globus_thread_key_t key, void *value);
476  void (*thread_yield)(void);
477  void (*thread_exit)(void *value);
478  int (*thread_sigmask)(int how, const sigset_t *newmask, sigset_t *oldmask);
479  int (*thread_kill)(globus_thread_t thread, int sig);
480  int (*thread_setcancelstate)(int state, int *oldstate);
481  void (*thread_testcancel)(void);
482  int (*thread_cancel)(globus_thread_t thread);
483  globus_thread_t (*thread_self)(void);
484  int (*thread_equal)(globus_thread_t thread1, globus_thread_t thread2);
485  globus_bool_t (*preemptive_threads)(void);
486  globus_bool_t (*i_am_only_thread)(void);
487  void * (*thread_cancellable_func)(
488  void * (*func)(void *), void *func_arg, void (*cleanup_func)(void *), void * cleanup_arg, globus_bool_t execute_cleanup);
489  int (*thread_pre_activate)(void);
490 }
491 globus_thread_impl_t;
492 
493 #ifdef __cplusplus
494 }
495 #endif
496 
497 #endif /* GLOBUS_THREAD_H */
Thread ID.
Definition: globus_thread.h:55
int globus_cond_timedwait(globus_cond_t *cond, globus_mutex_t *mutex, globus_abstime_t *abstime)
Wait for a condition to be signalled.
Definition: globus_thread.c:614
globus_bool_t globus_i_am_only_thread(void)
Determine if threads are supported.
Definition: globus_thread.c:1615
int globus_cond_broadcast(globus_cond_t *cond)
Signal a condition to multiple threads.
Definition: globus_thread.c:689
int globus_cond_wait(globus_cond_t *cond, globus_mutex_t *mutex)
Wait for a condition to be signalled.
Definition: globus_thread.c:561
int globus_thread_key_delete(globus_thread_key_t key)
Delete a thread-local storage key.
Definition: globus_thread.c:1117
int globus_cond_destroy(globus_cond_t *cond)
Destroy a condition variable.
Definition: globus_thread.c:515
Thread attributes.
Definition: globus_thread.h:72
int globus_bool_t
Boolean type.
Definition: globus_types.h:78
int globus_mutex_destroy(globus_mutex_t *mutex)
Destroy a mutex.
Definition: globus_thread.c:291
void(* globus_thread_key_destructor_func_t)(void *value)
Thread-specific data destructor.
Definition: globus_thread.h:179
Reference Counting Module Activation and Deactivation.
int globus_thread_kill(globus_thread_t thread, int sig)
Send a signal to a thread.
Definition: globus_thread.c:1477
int globus_mutex_init(globus_mutex_t *mutex, globus_mutexattr_t *attr)
Initialize a mutex.
Definition: globus_thread.c:252
void * globus_thread_getspecific(globus_thread_key_t key)
Get a thread-specific data value.
Definition: globus_thread.c:1262
int globus_cond_init(globus_cond_t *cond, globus_condattr_t *attr)
Initialize a condition variableThe globus_cond_init() function creates a condition variable that can ...
Definition: globus_thread.c:475
int globus_thread_setspecific(globus_thread_key_t key, void *value)
Set a thread-specific data value.
Definition: globus_thread.c:1324
int globus_thread_setcancelstate(int state, int *oldstate)
Set the thread&#39;s cancellable state.
Definition: globus_thread.c:1786
void * globus_thread_cancellable_func(void *(*func)(void *), void *arg, void(*cleanup_func)(void *), void *cleanup_arg, globus_bool_t execute_cleanup)
Execute a function with thread cleanup in case of cancellation.
Definition: globus_thread.c:1666
Thread once structure.
Definition: globus_thread.h:210
Mutex.
Definition: globus_thread.h:91
globus_thread_t globus_thread_self(void)
Determine the current thread&#39;s ID.
Definition: globus_thread.c:1511
int globus_mutex_unlock(globus_mutex_t *mutex)
Unlock a mutex.
Definition: globus_thread.c:376
int globus_condattr_setspace(globus_condattr_t *cond_attr, int space)
Set callback space associated with a condition variable attributeThe globus_condattr_setspace() funct...
Definition: globus_thread.c:888
void globus_thread_yield(void)
Yield execution to another thread.
Definition: globus_thread.c:1356
Condition variable.
Definition: globus_thread.h:108
int globus_thread_sigmask(int how, const sigset_t *new_mask, sigset_t *old_mask)
Modify the current thread&#39;s signal mask.
Definition: globus_thread.c:1431
int globus_condattr_destroy(globus_condattr_t *cond_attr)
Destroy a condition attribute.
Definition: globus_thread.c:846
Condition variable attribute.
Definition: globus_thread.h:155
int globus_condattr_init(globus_condattr_t *cond_attr)
Initialize a condition variable attribute.
Definition: globus_thread.c:808
int globus_condattr_getspace(globus_condattr_t *cond_attr, int *space)
Get callback space associated with a condition variable attributeThe globus_condattr_getspace() funct...
Definition: globus_thread.c:931
int globus_thread_once(globus_thread_once_t *once, void(*init_routine)(void))
Execute a function one time.
Definition: globus_thread.c:1199
Thread-specific data key.
Definition: globus_thread.h:185
void globus_thread_testcancel(void)
Thread cancellation point.
Definition: globus_thread.c:1744
void globus_thread_exit(void *value)
Terminate the current thread.
Definition: globus_thread.c:1381
int globus_thread_key_create(globus_thread_key_t *key, globus_thread_key_destructor_func_t destructor)
Create a key for thread-specific storage.
Definition: globus_thread.c:1074
int globus_thread_cancel(globus_thread_t thr)
Cancel a thread.
Definition: globus_thread.c:1712
int globus_mutex_lock(globus_mutex_t *mutex)
Lock a mutex.
Definition: globus_thread.c:334
globus_bool_t globus_thread_equal(globus_thread_t thread1, globus_thread_t thread2)
Check whether thread identifiers match.
Definition: globus_thread.c:1551
Time Types and Macros.
int globus_mutex_trylock(globus_mutex_t *mutex)
Lock a mutex if it is not locked.
Definition: globus_thread.c:420
int globus_thread_create(globus_thread_t *thread, globus_threadattr_t *attr, globus_thread_func_t func, void *user_arg)
Create a new thread.
Definition: globus_thread.c:993
globus_bool_t globus_thread_preemptive_threads(void)
Indicate whether the active thread model supports preemption.
Definition: globus_thread.c:1583
int globus_thread_set_model(const char *model)
Select threading model for an application.
Definition: globus_thread.c:102
int globus_cond_signal(globus_cond_t *cond)
Signal a condition to a thread.
Definition: globus_thread.c:652
Module Descriptor.
Definition: globus_module.h:54
Mutex attribute.
Definition: globus_thread.h:135