Grid Community Toolkit  6.2.1550507116
globus_thread.h
Go to the documentation of this file.
1 /*
2  * Copyright 1999-2010 University of Chicago
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
25 #if !defined(GLOBUS_THREAD_H)
26 #define GLOBUS_THREAD_H 1
27 
28 /* Include header files */
29 #include "globus_module.h"
30 #include "globus_time.h"
31 
32 #if !defined(_WIN32) || defined(__MINGW32__)
33 #include <unistd.h>
34 #endif
35 
36 #if _POSIX_THREADS
37 #if !defined(HAVE_PTHREAD)
38 #define HAVE_PTHREAD 1
39 #endif
40 #if defined __GNUC__ && defined __EXCEPTIONS
41 #undef __EXCEPTIONS
42 #include <pthread.h>
43 #define __EXCEPTIONS 1
44 #else
45 #include <pthread.h>
46 #endif
47 #endif /* _POSIX_THREADS */
48 
49 #if defined(_WIN32)
50 #include <windows.h>
51 #define HAVE_WINDOWS_THREADS 1
52 #endif
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 /*
59  * Default supported thread models (on some systems). Others can conceivably
60  * be dynamically loaded if their implementation can use the dummy block in the
61  * structures.
62  */
63 #define GLOBUS_THREAD_MODEL_NONE "none"
64 #define GLOBUS_THREAD_MODEL_PTHREADS "pthread"
65 #define GLOBUS_THREAD_MODEL_WINDOWS "windows"
66 
71 typedef union
72 {
73  int none;
74 #if HAVE_PTHREAD
75  pthread_t pthread;
76 #endif
77 #if HAVE_WINDOWS_THREADS
78  uintptr_t windows;
79 #endif
80  intptr_t dummy;
81 }
83 
88 typedef union
89 {
90  int none;
91 #if HAVE_PTHREAD
92  pthread_attr_t pthread;
93 #endif
94 #if HAVE_WINDOWS_THREADS
95  void *windows;
96 #endif
97  intptr_t dummy;
98 }
100 
101 typedef void * (*globus_thread_func_t)(void *);
102 
107 typedef union
108 {
109  int none;
110 #if HAVE_PTHREAD
111  pthread_mutex_t pthread;
112 #endif
113 #if HAVE_WINDOWS_THREADS
114  HANDLE windows;
115 #endif
116  intptr_t dummy;
117 }
119 
124 typedef union
125 {
126  int none;
127 #if HAVE_PTHREAD
128  struct globus_cond_pthread_s
129  {
130  pthread_cond_t cond;
131  globus_bool_t poll_space;
132  int space;
133  } pthread;
134 #endif
135 #if HAVE_WINDOWS_THREADS
136  struct globus_cond_windows_s
137  {
138  HANDLE events[2];
139  int numberOfWaiters;
140  }
141  windows;
142 #endif
143  intptr_t dummy;
144 }
146 
151 typedef union
152 {
153  int none;
154 #if HAVE_PTHREAD
155  pthread_mutexattr_t pthread;
156 #endif
157 #if HAVE_WINDOWS_THREADS
158  struct globus_mutexattr_windows_s
159  {
160  LPSECURITY_ATTRIBUTES securityAttributes;
161  } windows;
162 #endif
163  intptr_t dummy;
164 }
166 
171 typedef union
172 {
173  int none;
174 #if HAVE_PTHREAD
175  struct globus_condattr_pthread_s
176  {
177  pthread_condattr_t attr;
178  int space;
179  } pthread;
180 #endif
181 #if HAVE_WINDOWS_THREADS
182  struct globus_condattr_windows_s
183  {
184  LPSECURITY_ATTRIBUTES securityAttributes;
185  } windows;
186 #endif
187  intptr_t dummy;
188 }
190 
195 typedef void (* globus_thread_key_destructor_func_t)(void * value);
196 
201 typedef union
202 {
203  int none;
204 #if HAVE_PTHREAD
205  pthread_key_t pthread;
206 #endif
207 #if HAVE_WINDOWS_THREADS
208  struct globus_thread_key_windows_s
209  {
210  DWORD TLSIndex;
211  globus_thread_key_destructor_func_t destructorFunction;
212  } windows;
213 #endif
214  /*
215  * Backward-compatibility hack for fedora/debian bnaries, must not
216  * be bigger than sizeof(pthread_key_t)
217  */
218  int32_t dummy;
219 }
221 
226 typedef union
227 {
228  int32_t none;
229 #if HAVE_PTHREAD
230  pthread_once_t pthread;
231 #endif
232 #if HAVE_WINDOWS_THREADS
233  int windows;
234 #endif
235  int32_t dummy;
236 }
238 
245 extern const globus_thread_once_t GLOBUS_THREAD_ONCE_INIT_VALUE;
246 #if HAVE_PTHREAD
247 # define GLOBUS_THREAD_ONCE_INIT { .pthread = PTHREAD_ONCE_INIT }
248 #elif HAVE_WINDOWS_THREADS
249 # define GLOBUS_THREAD_ONCE_INIT { .windows = 0 }
250 #else
251 # define GLOBUS_THREAD_ONCE_INIT { .none = 0 }
252 #endif
253 
254 extern
255 int
257  const char * model);
258 
259 extern
260 int
262  globus_mutex_t * mutex,
263  globus_mutexattr_t * attr);
264 
265 extern
266 int
268  globus_mutex_t * mutex);
269 
270 extern
271 int
273  globus_mutex_t * mutex);
274 
275 extern
276 int
278  globus_mutex_t * mutex);
279 
280 extern
281 int
283  globus_mutex_t * mutex);
284 
285 extern
286 int
288  globus_cond_t * cond,
289  globus_condattr_t * attr);
290 
291 extern
292 int
294  globus_cond_t * cond);
295 
296 extern
297 int
299  globus_cond_t * cond,
300  globus_mutex_t * mutex);
301 
302 extern
303 int
305  globus_cond_t * cond,
306  globus_mutex_t * mutex,
307  globus_abstime_t * abstime);
308 
309 extern
310 int
312  globus_cond_t * cond);
313 
314 extern
315 int
317  globus_cond_t * cond);
318 
319 extern
320 int
322  globus_condattr_t * cond_attr);
323 
324 extern
325 int
327  globus_condattr_t * cond_attr);
328 
329 extern
330 int
332  globus_condattr_t * cond_attr,
333  int space);
334 
335 extern
336 int
338  globus_condattr_t * cond_attr,
339  int * space);
340 
341 extern
342 int
344  globus_thread_t * thread,
345  globus_threadattr_t * attr,
346  globus_thread_func_t func,
347  void * user_arg);
348 
349 extern
350 void *
352  globus_thread_key_t key);
353 
354 extern
355 int
358  void * value);
359 
360 extern
361 int
363  globus_thread_key_t * key,
365 
366 extern
367 int
369  globus_thread_key_t key);
370 
371 extern
372 int
374  globus_thread_once_t * once,
375  void (*init_routine)(void));
376 
377 extern
378 void
379 globus_thread_yield(void);
380 
381 extern
382 int
384  int how,
385  const sigset_t * newmask,
386  sigset_t * oldmask);
387 
388 extern
389 int
391  globus_thread_t thread,
392  int sig);
393 
394 extern
395 void
396 globus_thread_exit(void *value);
397 
398 extern
400 globus_thread_self(void);
401 
402 extern
403 int
405  globus_thread_t thread1,
406  globus_thread_t thread2);
407 
408 extern
411 
412 extern
415 
416 extern
417 void *
419  void * (*func)(void *),
420  void * arg,
421  void (*cleanup_func)(void *),
422  void * cleanup_arg,
423  globus_bool_t execute_cleanup);
424 
425 extern
426 int
428 
429 extern
430 void
432 
433 extern
434 int
436  int state,
437  int * oldstate);
438 
444 #define GLOBUS_THREAD_CANCEL_DISABLE 0
445 
450 #define GLOBUS_THREAD_CANCEL_ENABLE 1
451 
452 /* Module definition */
453 extern
454 int
455 globus_i_thread_pre_activate();
456 
457 extern
458 globus_module_descriptor_t globus_i_thread_module;
459 
465 #define GLOBUS_THREAD_MODULE (&globus_i_thread_module)
466 
467 typedef struct
468 {
469  int (*mutex_init)(globus_mutex_t *mutex, globus_mutexattr_t *attr);
470  int (*mutex_destroy)(globus_mutex_t *mutex);
471  int (*mutex_lock)(globus_mutex_t *mutex);
472  int (*mutex_unlock)(globus_mutex_t *mutex);
473  int (*mutex_trylock)(globus_mutex_t *mutex);
474  int (*cond_init)(globus_cond_t *cond, globus_condattr_t *attr);
475  int (*cond_destroy)(globus_cond_t *cond);
476  int (*cond_wait)(globus_cond_t *cond, globus_mutex_t *mutex);
477  int (*cond_timedwait)(globus_cond_t *cond, globus_mutex_t *mutex, globus_abstime_t *abstime);
478  int (*cond_signal)(globus_cond_t *cond);
479  int (*cond_broadcast)(globus_cond_t *cond);
480  int (*mutexattr_init)(globus_mutexattr_t *attr);
481  int (*mutexattr_destroy)(globus_mutexattr_t *attr);
482  int (*condattr_init)(globus_condattr_t *attr);
483  int (*condattr_destroy)(globus_condattr_t *attr);
484  int (*condattr_setspace)(globus_condattr_t *attr, int space);
485  int (*condattr_getspace)(globus_condattr_t *attr, int *space);
486  int (*thread_create)(globus_thread_t *thread, globus_threadattr_t *attr, globus_thread_func_t func, void * user_arg);
487  int (*thread_key_create)(globus_thread_key_t *key, globus_thread_key_destructor_func_t func);
488  int (*thread_key_delete)(globus_thread_key_t key);
489  int (*thread_once)(globus_thread_once_t *once, void (*init_func)(void));
490  void *(*thread_getspecific)(globus_thread_key_t key);
491  int (*thread_setspecific)(globus_thread_key_t key, void *value);
492  void (*thread_yield)(void);
493  void (*thread_exit)(void *value);
494  int (*thread_sigmask)(int how, const sigset_t *newmask, sigset_t *oldmask);
495  int (*thread_kill)(globus_thread_t thread, int sig);
496  int (*thread_setcancelstate)(int state, int *oldstate);
497  void (*thread_testcancel)(void);
498  int (*thread_cancel)(globus_thread_t thread);
499  globus_thread_t (*thread_self)(void);
500  int (*thread_equal)(globus_thread_t thread1, globus_thread_t thread2);
501  globus_bool_t (*preemptive_threads)(void);
502  globus_bool_t (*i_am_only_thread)(void);
503  void * (*thread_cancellable_func)(
504  void * (*func)(void *), void *func_arg, void (*cleanup_func)(void *), void * cleanup_arg, globus_bool_t execute_cleanup);
505  int (*thread_pre_activate)(void);
506 }
507 globus_thread_impl_t;
508 
509 #ifdef __cplusplus
510 }
511 #endif
512 
513 #endif /* GLOBUS_THREAD_H */
Thread ID.
Definition: globus_thread.h:71
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:634
globus_bool_t globus_i_am_only_thread(void)
Determine if threads are supported.
Definition: globus_thread.c:1619
int globus_cond_broadcast(globus_cond_t *cond)
Signal a condition to multiple threads.
Definition: globus_thread.c:709
int globus_cond_wait(globus_cond_t *cond, globus_mutex_t *mutex)
Wait for a condition to be signalled.
Definition: globus_thread.c:581
int globus_thread_key_delete(globus_thread_key_t key)
Delete a thread-local storage key.
Definition: globus_thread.c:1134
int globus_cond_destroy(globus_cond_t *cond)
Destroy a condition variable.
Definition: globus_thread.c:535
Thread attributes.
Definition: globus_thread.h:88
int globus_bool_t
Boolean type.
Definition: globus_types.h:93
int globus_mutex_destroy(globus_mutex_t *mutex)
Destroy a mutex.
Definition: globus_thread.c:309
Reference Counting Module Activation and Deactivation.
void(* globus_thread_key_destructor_func_t)(void *value)
Thread-specific data destructor.
Definition: globus_thread.h:195
int globus_thread_kill(globus_thread_t thread, int sig)
Send a signal to a thread.
Definition: globus_thread.c:1481
int globus_mutex_init(globus_mutex_t *mutex, globus_mutexattr_t *attr)
Initialize a mutex.
Definition: globus_thread.c:270
void * globus_thread_getspecific(globus_thread_key_t key)
Get a thread-specific data value.
Definition: globus_thread.c:1271
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:495
int globus_thread_setspecific(globus_thread_key_t key, void *value)
Set a thread-specific data value.
Definition: globus_thread.c:1328
int globus_thread_setcancelstate(int state, int *oldstate)
Set the thread&#39;s cancellable state.
Definition: globus_thread.c:1790
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:1670
Thread once structure.
Definition: globus_thread.h:226
Mutex.
Definition: globus_thread.h:107
globus_thread_t globus_thread_self(void)
Determine the current thread&#39;s ID.
Definition: globus_thread.c:1515
int globus_mutex_unlock(globus_mutex_t *mutex)
Unlock a mutex.
Definition: globus_thread.c:394
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:908
void globus_thread_yield(void)
Yield execution to another thread.
Definition: globus_thread.c:1360
Condition variable.
Definition: globus_thread.h:124
int globus_thread_sigmask(int how, const sigset_t *newmask, sigset_t *oldmask)
Modify the current thread&#39;s signal mask.
Definition: globus_thread.c:1435
int globus_condattr_destroy(globus_condattr_t *cond_attr)
Destroy a condition attribute.
Definition: globus_thread.c:866
Condition variable attribute.
Definition: globus_thread.h:171
int globus_condattr_init(globus_condattr_t *cond_attr)
Initialize a condition variable attribute.
Definition: globus_thread.c:828
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:951
int globus_thread_once(globus_thread_once_t *once, void(*init_routine)(void))
Execute a function one time.
Definition: globus_thread.c:1213
Thread-specific data key.
Definition: globus_thread.h:201
void globus_thread_testcancel(void)
Thread cancellation point.
Definition: globus_thread.c:1748
void globus_thread_exit(void *value)
Terminate the current thread.
Definition: globus_thread.c:1385
int globus_thread_key_create(globus_thread_key_t *key, globus_thread_key_destructor_func_t func)
Create a key for thread-specific storage.
Definition: globus_thread.c:1091
int globus_thread_cancel(globus_thread_t thr)
Cancel a thread.
Definition: globus_thread.c:1716
int globus_mutex_lock(globus_mutex_t *mutex)
Lock a mutex.
Definition: globus_thread.c:352
int globus_thread_equal(globus_thread_t thread1, globus_thread_t thread2)
Check whether thread identifiers match.
Definition: globus_thread.c:1555
Time Types and Macros.
int globus_mutex_trylock(globus_mutex_t *mutex)
Lock a mutex if it is not locked.
Definition: globus_thread.c:438
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:1013
globus_bool_t globus_thread_preemptive_threads(void)
Indicate whether the active thread model supports preemption.
Definition: globus_thread.c:1587
int globus_thread_set_model(const char *model)
Select threading model for an application.
Definition: globus_thread.c:118
int globus_cond_signal(globus_cond_t *cond)
Signal a condition to a thread.
Definition: globus_thread.c:672
Module Descriptor.
Definition: globus_module.h:71
Mutex attribute.
Definition: globus_thread.h:151