Grid Community Toolkit  6.2.1705709074 (tag: v6.2.20240202)
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 int
256  const char * model);
257 
258 int
260  globus_mutex_t * mutex,
261  globus_mutexattr_t * attr);
262 
263 int
265  globus_mutex_t * mutex);
266 
267 int
269  globus_mutex_t * mutex);
270 
271 int
273  globus_mutex_t * mutex);
274 
275 int
277  globus_mutex_t * mutex);
278 
279 int
281  globus_cond_t * cond,
282  globus_condattr_t * attr);
283 
284 int
286  globus_cond_t * cond);
287 
288 int
290  globus_cond_t * cond,
291  globus_mutex_t * mutex);
292 
293 int
295  globus_cond_t * cond,
296  globus_mutex_t * mutex,
297  globus_abstime_t * abstime);
298 
299 int
301  globus_cond_t * cond);
302 
303 int
305  globus_cond_t * cond);
306 
307 int
309  globus_condattr_t * cond_attr);
310 
311 int
313  globus_condattr_t * cond_attr);
314 
315 int
317  globus_condattr_t * cond_attr,
318  int space);
319 
320 int
322  globus_condattr_t * cond_attr,
323  int * space);
324 
325 int
327  globus_thread_t * thread,
328  globus_threadattr_t * attr,
329  globus_thread_func_t func,
330  void * user_arg);
331 
332 void *
334  globus_thread_key_t key);
335 
336 int
339  void * value);
340 
341 int
343  globus_thread_key_t * key,
345 
346 int
348  globus_thread_key_t key);
349 
350 int
352  globus_thread_once_t * once,
353  void (*init_routine)(void));
354 
355 void
356 globus_thread_yield(void);
357 
358 int
360  int how,
361  const sigset_t * newmask,
362  sigset_t * oldmask);
363 
364 int
366  globus_thread_t thread,
367  int sig);
368 
369 void
370 globus_thread_exit(void *value);
371 
373 globus_thread_self(void);
374 
375 int
377  globus_thread_t thread1,
378  globus_thread_t thread2);
379 
382 
385 
386 void *
388  void * (*func)(void *),
389  void * arg,
390  void (*cleanup_func)(void *),
391  void * cleanup_arg,
392  globus_bool_t execute_cleanup);
393 
394 int
396 
397 void
399 
400 int
402  int state,
403  int * oldstate);
404 
410 #define GLOBUS_THREAD_CANCEL_DISABLE 0
411 
416 #define GLOBUS_THREAD_CANCEL_ENABLE 1
417 
418 /* Module definition */
419 int
420 globus_i_thread_pre_activate();
421 
422 extern
423 globus_module_descriptor_t globus_i_thread_module;
424 
430 #define GLOBUS_THREAD_MODULE (&globus_i_thread_module)
431 
432 typedef struct
433 {
434  int (*mutex_init)(globus_mutex_t *mutex, globus_mutexattr_t *attr);
435  int (*mutex_destroy)(globus_mutex_t *mutex);
436  int (*mutex_lock)(globus_mutex_t *mutex);
437  int (*mutex_unlock)(globus_mutex_t *mutex);
438  int (*mutex_trylock)(globus_mutex_t *mutex);
439  int (*cond_init)(globus_cond_t *cond, globus_condattr_t *attr);
440  int (*cond_destroy)(globus_cond_t *cond);
441  int (*cond_wait)(globus_cond_t *cond, globus_mutex_t *mutex);
442  int (*cond_timedwait)(globus_cond_t *cond, globus_mutex_t *mutex, globus_abstime_t *abstime);
443  int (*cond_signal)(globus_cond_t *cond);
444  int (*cond_broadcast)(globus_cond_t *cond);
445  int (*mutexattr_init)(globus_mutexattr_t *attr);
446  int (*mutexattr_destroy)(globus_mutexattr_t *attr);
447  int (*condattr_init)(globus_condattr_t *attr);
448  int (*condattr_destroy)(globus_condattr_t *attr);
449  int (*condattr_setspace)(globus_condattr_t *attr, int space);
450  int (*condattr_getspace)(globus_condattr_t *attr, int *space);
451  int (*thread_create)(globus_thread_t *thread, globus_threadattr_t *attr, globus_thread_func_t func, void * user_arg);
452  int (*thread_key_create)(globus_thread_key_t *key, globus_thread_key_destructor_func_t func);
453  int (*thread_key_delete)(globus_thread_key_t key);
454  int (*thread_once)(globus_thread_once_t *once, void (*init_func)(void));
455  void *(*thread_getspecific)(globus_thread_key_t key);
456  int (*thread_setspecific)(globus_thread_key_t key, void *value);
457  void (*thread_yield)(void);
458  void (*thread_exit)(void *value);
459  int (*thread_sigmask)(int how, const sigset_t *newmask, sigset_t *oldmask);
460  int (*thread_kill)(globus_thread_t thread, int sig);
461  int (*thread_setcancelstate)(int state, int *oldstate);
462  void (*thread_testcancel)(void);
463  int (*thread_cancel)(globus_thread_t thread);
464  globus_thread_t (*thread_self)(void);
465  int (*thread_equal)(globus_thread_t thread1, globus_thread_t thread2);
466  globus_bool_t (*preemptive_threads)(void);
467  globus_bool_t (*i_am_only_thread)(void);
468  void * (*thread_cancellable_func)(
469  void * (*func)(void *), void *func_arg, void (*cleanup_func)(void *), void * cleanup_arg, globus_bool_t execute_cleanup);
470  int (*thread_pre_activate)(void);
471 }
472 globus_thread_impl_t;
473 
474 #ifdef __cplusplus
475 }
476 #endif
477 
478 #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:623
globus_bool_t globus_i_am_only_thread(void)
Determine if threads are supported.
Definition: globus_thread.c:1614
int globus_cond_broadcast(globus_cond_t *cond)
Signal a condition to multiple threads.
Definition: globus_thread.c:697
int globus_cond_wait(globus_cond_t *cond, globus_mutex_t *mutex)
Wait for a condition to be signalled.
Definition: globus_thread.c:571
int globus_thread_key_delete(globus_thread_key_t key)
Delete a thread-local storage key.
Definition: globus_thread.c:1123
int globus_cond_destroy(globus_cond_t *cond)
Destroy a condition variable.
Definition: globus_thread.c:526
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:305
void(* globus_thread_key_destructor_func_t)(void *value)
Thread-specific data destructor.
Definition: globus_thread.h:195
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:1480
int globus_mutex_init(globus_mutex_t *mutex, globus_mutexattr_t *attr)
Initialize a mutex.
Definition: globus_thread.c:267
void * globus_thread_getspecific(globus_thread_key_t key)
Get a thread-specific data value.
Definition: globus_thread.c:1269
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:487
int globus_thread_setspecific(globus_thread_key_t key, void *value)
Set a thread-specific data value.
Definition: globus_thread.c:1331
int globus_thread_setcancelstate(int state, int *oldstate)
Set the thread&#39;s cancellable state.
Definition: globus_thread.c:1781
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:1664
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:1513
int globus_mutex_unlock(globus_mutex_t *mutex)
Unlock a mutex.
Definition: globus_thread.c:388
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:894
void globus_thread_yield(void)
Yield execution to another thread.
Definition: globus_thread.c:1362
Condition variable.
Definition: globus_thread.h:124
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:1435
int globus_condattr_destroy(globus_condattr_t *cond_attr)
Destroy a condition attribute.
Definition: globus_thread.c:852
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:814
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:937
int globus_thread_once(globus_thread_once_t *once, void(*init_routine)(void))
Execute a function one time.
Definition: globus_thread.c:1206
Thread-specific data key.
Definition: globus_thread.h:201
void globus_thread_testcancel(void)
Thread cancellation point.
Definition: globus_thread.c:1740
void globus_thread_exit(void *value)
Terminate the current thread.
Definition: globus_thread.c:1386
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:1081
int globus_thread_cancel(globus_thread_t thr)
Cancel a thread.
Definition: globus_thread.c:1709
int globus_mutex_lock(globus_mutex_t *mutex)
Lock a mutex.
Definition: globus_thread.c:347
globus_bool_t globus_thread_equal(globus_thread_t thread1, globus_thread_t thread2)
Check whether thread identifiers match.
Definition: globus_thread.c:1552
Time Types and Macros.
int globus_mutex_trylock(globus_mutex_t *mutex)
Lock a mutex if it is not locked.
Definition: globus_thread.c:431
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:998
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:117
int globus_cond_signal(globus_cond_t *cond)
Signal a condition to a thread.
Definition: globus_thread.c:661
Module Descriptor.
Definition: globus_module.h:71
Mutex attribute.
Definition: globus_thread.h:151