globus_thread.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 1999-2010 University of Chicago
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00025 #if !defined(GLOBUS_THREAD_H)
00026 #define GLOBUS_THREAD_H 1
00027 
00028 /* Include header files */
00029 #include "globus_module.h"
00030 #include "globus_time.h"
00031 
00032 #if !defined(_WIN32) || defined(__MINGW32__)
00033 #include <unistd.h>
00034 #endif
00035 
00036 #if _POSIX_THREADS
00037 #if !defined(HAVE_PTHREAD)
00038 #define HAVE_PTHREAD 1
00039 #endif
00040 #if defined __GNUC__ && defined __EXCEPTIONS
00041 #undef __EXCEPTIONS
00042 #include <pthread.h>
00043 #define __EXCEPTIONS 1
00044 #else
00045 #include <pthread.h>
00046 #endif
00047 #endif /* _POSIX_THREADS */
00048 
00049 #if defined(_WIN32)
00050 #include <windows.h>
00051 #define HAVE_WINDOWS_THREADS 1
00052 #endif
00053 
00054 #ifdef __cplusplus
00055 extern "C" {
00056 #endif
00057 
00058 /* 
00059  * Default supported thread models (on some systems). Others can conceivably
00060  * be dynamically loaded if their implementation can use the dummy block in the
00061  * structures.
00062  */
00063 #define GLOBUS_THREAD_MODEL_NONE "none"
00064 #define GLOBUS_THREAD_MODEL_PTHREADS "pthread"
00065 #define GLOBUS_THREAD_MODEL_WINDOWS "windows"
00066 
00071 typedef union
00072 {
00073     int none;
00074 #if HAVE_PTHREAD
00075     pthread_t pthread;
00076 #endif
00077 #if HAVE_WINDOWS_THREADS
00078     uintptr_t windows;
00079 #endif
00080     intptr_t dummy;
00081 }
00082 globus_thread_t;
00083 
00088 typedef union
00089 {
00090     int none;
00091 #if HAVE_PTHREAD
00092     pthread_attr_t pthread;
00093 #endif
00094 #if HAVE_WINDOWS_THREADS
00095     void *windows;
00096 #endif
00097     intptr_t dummy;
00098 }
00099 globus_threadattr_t;
00100 
00101 typedef void * (*globus_thread_func_t)(void *);
00102 
00107 typedef union
00108 {
00109     int none;
00110 #if HAVE_PTHREAD
00111     pthread_mutex_t pthread;
00112 #endif
00113 #if HAVE_WINDOWS_THREADS
00114     HANDLE windows;
00115 #endif
00116     intptr_t dummy;
00117 }
00118 globus_mutex_t;
00119 
00124 typedef union
00125 {
00126     int none;
00127 #if HAVE_PTHREAD
00128     struct globus_cond_pthread_s
00129     {
00130         pthread_cond_t cond;
00131         globus_bool_t poll_space;
00132         int space;
00133     } pthread;
00134 #endif
00135 #if HAVE_WINDOWS_THREADS
00136     struct globus_cond_windows_s
00137     {
00138         HANDLE events[2];
00139         int numberOfWaiters;
00140     }
00141     windows;
00142 #endif
00143     intptr_t dummy;
00144 }
00145 globus_cond_t;
00146 
00151 typedef union
00152 {
00153     int none;
00154 #if HAVE_PTHREAD
00155     pthread_mutexattr_t pthread;
00156 #endif
00157 #if HAVE_WINDOWS_THREADS
00158     struct globus_mutexattr_windows_s
00159     {
00160         LPSECURITY_ATTRIBUTES securityAttributes;
00161     } windows;
00162 #endif
00163     intptr_t dummy;
00164 }
00165 globus_mutexattr_t;
00166 
00171 typedef union
00172 {
00173     int none;
00174 #if HAVE_PTHREAD
00175     struct globus_condattr_pthread_s
00176     {
00177         pthread_condattr_t attr;
00178         int space;
00179     } pthread;
00180 #endif
00181 #if HAVE_WINDOWS_THREADS
00182     struct globus_condattr_windows_s
00183     {
00184         LPSECURITY_ATTRIBUTES securityAttributes;
00185     } windows;
00186 #endif
00187     intptr_t dummy;
00188 }
00189 globus_condattr_t;
00190 
00195 typedef void (* globus_thread_key_destructor_func_t)(void * value);
00196 
00201 typedef union
00202 {
00203     int none;
00204 #if HAVE_PTHREAD
00205     pthread_key_t pthread;
00206 #endif
00207 #if HAVE_WINDOWS_THREADS
00208     struct globus_thread_key_windows_s
00209     {
00210         DWORD TLSIndex;
00211         globus_thread_key_destructor_func_t destructorFunction;
00212     } windows;
00213 #endif
00214     /*
00215      * Backward-compatibility hack for fedora/debian bnaries, must not
00216      * be bigger than sizeof(pthread_key_t)
00217      */
00218     int32_t dummy;
00219 }
00220 globus_thread_key_t;
00221 
00226 typedef union
00227 {
00228     int32_t none;
00229 #if HAVE_PTHREAD
00230     pthread_once_t pthread;
00231 #endif
00232 #if HAVE_WINDOWS_THREADS
00233     int windows;
00234 #endif
00235     int32_t dummy;
00236 }
00237 globus_thread_once_t;
00238 
00245 extern const globus_thread_once_t GLOBUS_THREAD_ONCE_INIT_VALUE;
00246 #if HAVE_PTHREAD
00247 #   define GLOBUS_THREAD_ONCE_INIT { .pthread = PTHREAD_ONCE_INIT }
00248 #elif HAVE_WINDOWS_THREADS
00249 #   define GLOBUS_THREAD_ONCE_INIT { .windows = 0 }
00250 #else
00251 #   define GLOBUS_THREAD_ONCE_INIT { .none = 0 }
00252 #endif
00253 
00254 extern
00255 int
00256 globus_thread_set_model(
00257     const char *                        model);
00258 
00259 extern
00260 int
00261 globus_mutex_init(
00262     globus_mutex_t *                    mutex,
00263     globus_mutexattr_t *                attr);
00264 
00265 extern
00266 int
00267 globus_mutex_destroy(
00268     globus_mutex_t *                    mutex);
00269 
00270 extern
00271 int
00272 globus_mutex_lock(
00273     globus_mutex_t *                    mutex);
00274 
00275 extern
00276 int
00277 globus_mutex_unlock(
00278     globus_mutex_t *                    mutex);
00279 
00280 extern
00281 int
00282 globus_mutex_trylock(
00283     globus_mutex_t *                    mutex);
00284 
00285 extern
00286 int
00287 globus_cond_init(
00288     globus_cond_t *                     cond,
00289     globus_condattr_t *                 attr);
00290 
00291 extern
00292 int
00293 globus_cond_destroy(
00294     globus_cond_t *                     cond);
00295 
00296 extern
00297 int
00298 globus_cond_wait(
00299     globus_cond_t *                     cond,
00300     globus_mutex_t *                    mutex);
00301 
00302 extern
00303 int
00304 globus_cond_timedwait(
00305     globus_cond_t *                     cond,
00306     globus_mutex_t *                    mutex,
00307     globus_abstime_t *                  abstime);
00308 
00309 extern
00310 int
00311 globus_cond_signal(
00312     globus_cond_t *                     cond);
00313 
00314 extern
00315 int
00316 globus_cond_broadcast(
00317     globus_cond_t *                     cond);
00318 
00319 extern
00320 int
00321 globus_condattr_init(
00322     globus_condattr_t *                 cond_attr);
00323 
00324 extern
00325 int
00326 globus_condattr_destroy(
00327     globus_condattr_t *                 cond_attr);
00328 
00329 extern
00330 int
00331 globus_condattr_setspace(
00332     globus_condattr_t *                 cond_attr,
00333     int                                 space);
00334 
00335 extern
00336 int
00337 globus_condattr_getspace(
00338     globus_condattr_t *                 cond_attr,
00339     int *                               space);
00340 
00341 extern
00342 int
00343 globus_thread_create(
00344     globus_thread_t *                   thread,
00345     globus_threadattr_t *               attr,
00346     globus_thread_func_t                func,
00347     void *                              user_arg);
00348 
00349 extern
00350 void *
00351 globus_thread_getspecific(
00352     globus_thread_key_t                 key);
00353 
00354 extern
00355 int
00356 globus_thread_setspecific(
00357     globus_thread_key_t                 key,
00358     void *                              value);
00359 
00360 extern
00361 int
00362 globus_thread_key_create(
00363     globus_thread_key_t *               key,
00364     globus_thread_key_destructor_func_t func);
00365     
00366 extern
00367 int
00368 globus_thread_key_delete(
00369     globus_thread_key_t                 key);
00370 
00371 extern
00372 int
00373 globus_thread_once(
00374     globus_thread_once_t *              once,
00375     void (*init_routine)(void));
00376 
00377 extern
00378 void
00379 globus_thread_yield(void);
00380 
00381 extern
00382 int
00383 globus_thread_sigmask(
00384     int                                 how,
00385     const sigset_t *                    newmask,
00386     sigset_t *                          oldmask);
00387 
00388 extern
00389 int
00390 globus_thread_kill(
00391     globus_thread_t                     thread,
00392     int                                 sig);
00393 
00394 extern
00395 void
00396 globus_thread_exit(void *value);
00397 
00398 extern
00399 globus_thread_t
00400 globus_thread_self(void);
00401 
00402 extern
00403 int
00404 globus_thread_equal(
00405     globus_thread_t                     thread1,
00406     globus_thread_t                     thread2);
00407 
00408 extern
00409 globus_bool_t
00410 globus_i_am_only_thread(void);
00411 
00412 extern
00413 globus_bool_t
00414 globus_thread_preemptive_threads(void);
00415 
00416 extern
00417 void *
00418 globus_thread_cancellable_func(
00419     void *                              (*func)(void *),
00420     void *                              arg,
00421     void                                (*cleanup_func)(void *),
00422     void *                              cleanup_arg,
00423     globus_bool_t                       execute_cleanup);
00424 
00425 extern
00426 int
00427 globus_thread_cancel(globus_thread_t thr);
00428 
00429 extern
00430 void
00431 globus_thread_testcancel(void);
00432 
00433 extern
00434 int
00435 globus_thread_setcancelstate(
00436     int                                 state,
00437     int *                               oldstate);
00438 
00444 #define GLOBUS_THREAD_CANCEL_DISABLE 0
00445 
00450 #define GLOBUS_THREAD_CANCEL_ENABLE 1
00451 
00452 /* Module definition */
00453 extern
00454 int
00455 globus_i_thread_pre_activate();
00456 
00457 extern
00458 globus_module_descriptor_t       globus_i_thread_module;
00459 
00465 #define GLOBUS_THREAD_MODULE (&globus_i_thread_module)
00466 
00467 typedef struct
00468 {
00469     int (*mutex_init)(globus_mutex_t *mutex, globus_mutexattr_t *attr);
00470     int (*mutex_destroy)(globus_mutex_t *mutex);
00471     int (*mutex_lock)(globus_mutex_t *mutex);
00472     int (*mutex_unlock)(globus_mutex_t *mutex);
00473     int (*mutex_trylock)(globus_mutex_t *mutex);
00474     int (*cond_init)(globus_cond_t *cond, globus_condattr_t *attr);
00475     int (*cond_destroy)(globus_cond_t *cond);
00476     int (*cond_wait)(globus_cond_t *cond, globus_mutex_t *mutex);
00477     int (*cond_timedwait)(globus_cond_t *cond, globus_mutex_t *mutex, globus_abstime_t *abstime);
00478     int (*cond_signal)(globus_cond_t *cond);
00479     int (*cond_broadcast)(globus_cond_t *cond);
00480     int (*mutexattr_init)(globus_mutexattr_t *attr);
00481     int (*mutexattr_destroy)(globus_mutexattr_t *attr);
00482     int (*condattr_init)(globus_condattr_t *attr);
00483     int (*condattr_destroy)(globus_condattr_t *attr);
00484     int (*condattr_setspace)(globus_condattr_t *attr, int space);
00485     int (*condattr_getspace)(globus_condattr_t *attr, int *space);
00486     int (*thread_create)(globus_thread_t *thread, globus_threadattr_t *attr, globus_thread_func_t func, void * user_arg);
00487     int (*thread_key_create)(globus_thread_key_t *key, globus_thread_key_destructor_func_t func);
00488     int (*thread_key_delete)(globus_thread_key_t key);
00489     int (*thread_once)(globus_thread_once_t *once, void (*init_func)(void));
00490     void *(*thread_getspecific)(globus_thread_key_t key);
00491     int (*thread_setspecific)(globus_thread_key_t key, void *value); 
00492     void (*thread_yield)(void);
00493     void (*thread_exit)(void *value);
00494     int (*thread_sigmask)(int how, const sigset_t *newmask, sigset_t *oldmask);
00495     int (*thread_kill)(globus_thread_t thread, int sig);
00496     int (*thread_setcancelstate)(int state, int *oldstate);
00497     void (*thread_testcancel)(void);
00498     int (*thread_cancel)(globus_thread_t thread);
00499     globus_thread_t (*thread_self)(void);
00500     int (*thread_equal)(globus_thread_t thread1, globus_thread_t thread2);
00501     globus_bool_t (*preemptive_threads)(void);
00502     globus_bool_t (*i_am_only_thread)(void);
00503     void * (*thread_cancellable_func)(
00504         void * (*func)(void *), void *func_arg, void (*cleanup_func)(void *), void * cleanup_arg, globus_bool_t execute_cleanup);
00505     int (*thread_pre_activate)(void);
00506 }
00507 globus_thread_impl_t;
00508 
00509 #ifdef __cplusplus
00510 }
00511 #endif
00512 
00513 #endif /* GLOBUS_THREAD_H  */

Generated on 17 Mar 2017 for globus_common by  doxygen 1.4.7