00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef GCC_GTHR_POSIX_H
00030 #define GCC_GTHR_POSIX_H
00031
00032
00033
00034
00035 #define __GTHREADS 1
00036
00037
00038 #ifndef _REENTRANT
00039 #define _REENTRANT 1
00040 #endif
00041
00042 #include <pthread.h>
00043 #include <unistd.h>
00044
00045 typedef pthread_key_t __gthread_key_t;
00046 typedef pthread_once_t __gthread_once_t;
00047 typedef pthread_mutex_t __gthread_mutex_t;
00048
00049 typedef struct {
00050 long depth;
00051 pthread_t owner;
00052 pthread_mutex_t actual;
00053 } __gthread_recursive_mutex_t;
00054
00055 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
00056 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
00057 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
00058
00059 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
00060 # define __gthrw(name) \
00061 static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)));
00062 # define __gthrw_(name) __gthrw_ ## name
00063 #else
00064 # define __gthrw(name)
00065 # define __gthrw_(name) name
00066 #endif
00067
00068 __gthrw(pthread_once)
00069 __gthrw(pthread_key_create)
00070 __gthrw(pthread_key_delete)
00071 __gthrw(pthread_getspecific)
00072 __gthrw(pthread_setspecific)
00073 __gthrw(pthread_create)
00074 __gthrw(pthread_cancel)
00075 __gthrw(pthread_self)
00076
00077 __gthrw(pthread_mutex_lock)
00078 __gthrw(pthread_mutex_trylock)
00079 __gthrw(pthread_mutex_unlock)
00080 __gthrw(pthread_mutexattr_init)
00081 __gthrw(pthread_mutexattr_destroy)
00082
00083 __gthrw(pthread_mutex_init)
00084
00085 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
00086
00087 __gthrw(pthread_cond_broadcast)
00088 __gthrw(pthread_cond_destroy)
00089 __gthrw(pthread_cond_init)
00090 __gthrw(pthread_cond_signal)
00091 __gthrw(pthread_cond_wait)
00092 __gthrw(pthread_exit)
00093 __gthrw(pthread_mutex_destroy)
00094 #ifdef _POSIX_PRIORITY_SCHEDULING
00095 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
00096 __gthrw(sched_get_priority_max)
00097 __gthrw(sched_get_priority_min)
00098 #endif
00099 #endif
00100 __gthrw(sched_yield)
00101 __gthrw(pthread_attr_destroy)
00102 __gthrw(pthread_attr_init)
00103 __gthrw(pthread_attr_setdetachstate)
00104 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
00105 __gthrw(pthread_getschedparam)
00106 __gthrw(pthread_setschedparam)
00107 #endif
00108 #endif
00109
00110 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 #if defined(__sun) && defined(__svr4__)
00121
00122 static volatile int __gthread_active = -1;
00123
00124 static void
00125 __gthread_trigger (void)
00126 {
00127 __gthread_active = 1;
00128 }
00129
00130 static inline int
00131 __gthread_active_p (void)
00132 {
00133 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
00134 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
00135
00136
00137 int __gthread_active_latest_value = __gthread_active;
00138
00139
00140
00141
00142 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
00143 {
00144 if (__gthrw_(pthread_once))
00145 {
00146
00147
00148 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
00149 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
00150 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
00151 }
00152
00153
00154 if (__gthread_active < 0)
00155 __gthread_active = 0;
00156
00157 __gthread_active_latest_value = __gthread_active;
00158 }
00159
00160 return __gthread_active_latest_value != 0;
00161 }
00162
00163 #else
00164
00165 static inline int
00166 __gthread_active_p (void)
00167 {
00168 static void *const __gthread_active_ptr
00169 = __extension__ (void *) &__gthrw_(pthread_cancel);
00170 return __gthread_active_ptr != 0;
00171 }
00172
00173 #endif
00174
00175 #else
00176
00177 static inline int
00178 __gthread_active_p (void)
00179 {
00180 return 1;
00181 }
00182
00183 #endif
00184
00185 #ifdef _LIBOBJC
00186
00187
00188 #include <config.h>
00189
00190 #ifdef HAVE_SCHED_H
00191 # include <sched.h>
00192 #endif
00193
00194
00195 static pthread_key_t _objc_thread_storage;
00196 static pthread_attr_t _objc_thread_attribs;
00197
00198
00199 static void *thread_local_storage = NULL;
00200
00201
00202
00203
00204 static inline int
00205 __gthread_objc_init_thread_system (void)
00206 {
00207 if (__gthread_active_p ())
00208 {
00209
00210 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
00211 {
00212
00213
00214
00215 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
00216 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
00217 PTHREAD_CREATE_DETACHED) == 0)
00218 return 0;
00219 }
00220 }
00221
00222 return -1;
00223 }
00224
00225
00226 static inline int
00227 __gthread_objc_close_thread_system (void)
00228 {
00229 if (__gthread_active_p ()
00230 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
00231 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
00232 return 0;
00233
00234 return -1;
00235 }
00236
00237
00238
00239
00240 static inline objc_thread_t
00241 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
00242 {
00243 objc_thread_t thread_id;
00244 pthread_t new_thread_handle;
00245
00246 if (!__gthread_active_p ())
00247 return NULL;
00248
00249 if (!(__gthrw_(pthread_create) (&new_thread_handle, NULL, (void *) func, arg)))
00250 thread_id = (objc_thread_t) new_thread_handle;
00251 else
00252 thread_id = NULL;
00253
00254 return thread_id;
00255 }
00256
00257
00258 static inline int
00259 __gthread_objc_thread_set_priority (int priority)
00260 {
00261 if (!__gthread_active_p ())
00262 return -1;
00263 else
00264 {
00265 #ifdef _POSIX_PRIORITY_SCHEDULING
00266 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
00267 pthread_t thread_id = __gthrw_(pthread_self) ();
00268 int policy;
00269 struct sched_param params;
00270 int priority_min, priority_max;
00271
00272 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, ¶ms) == 0)
00273 {
00274 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
00275 return -1;
00276
00277 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
00278 return -1;
00279
00280 if (priority > priority_max)
00281 priority = priority_max;
00282 else if (priority < priority_min)
00283 priority = priority_min;
00284 params.sched_priority = priority;
00285
00286
00287
00288
00289
00290
00291 if (__gthrw_(pthread_setschedparam) (thread_id, policy, ¶ms) == 0)
00292 return 0;
00293 }
00294 #endif
00295 #endif
00296 return -1;
00297 }
00298 }
00299
00300
00301 static inline int
00302 __gthread_objc_thread_get_priority (void)
00303 {
00304 #ifdef _POSIX_PRIORITY_SCHEDULING
00305 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
00306 if (__gthread_active_p ())
00307 {
00308 int policy;
00309 struct sched_param params;
00310
00311 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, ¶ms) == 0)
00312 return params.sched_priority;
00313 else
00314 return -1;
00315 }
00316 else
00317 #endif
00318 #endif
00319 return OBJC_THREAD_INTERACTIVE_PRIORITY;
00320 }
00321
00322
00323 static inline void
00324 __gthread_objc_thread_yield (void)
00325 {
00326 if (__gthread_active_p ())
00327 __gthrw_(sched_yield) ();
00328 }
00329
00330
00331 static inline int
00332 __gthread_objc_thread_exit (void)
00333 {
00334 if (__gthread_active_p ())
00335
00336 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
00337
00338
00339 return -1;
00340 }
00341
00342
00343 static inline objc_thread_t
00344 __gthread_objc_thread_id (void)
00345 {
00346 if (__gthread_active_p ())
00347 return (objc_thread_t) __gthrw_(pthread_self) ();
00348 else
00349 return (objc_thread_t) 1;
00350 }
00351
00352
00353 static inline int
00354 __gthread_objc_thread_set_data (void *value)
00355 {
00356 if (__gthread_active_p ())
00357 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
00358 else
00359 {
00360 thread_local_storage = value;
00361 return 0;
00362 }
00363 }
00364
00365
00366 static inline void *
00367 __gthread_objc_thread_get_data (void)
00368 {
00369 if (__gthread_active_p ())
00370 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
00371 else
00372 return thread_local_storage;
00373 }
00374
00375
00376
00377
00378 static inline int
00379 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
00380 {
00381 if (__gthread_active_p ())
00382 {
00383 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
00384
00385 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
00386 {
00387 objc_free (mutex->backend);
00388 mutex->backend = NULL;
00389 return -1;
00390 }
00391 }
00392
00393 return 0;
00394 }
00395
00396
00397 static inline int
00398 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
00399 {
00400 if (__gthread_active_p ())
00401 {
00402 int count;
00403
00404
00405
00406
00407
00408
00409 do
00410 {
00411 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
00412 if (count < 0)
00413 return -1;
00414 }
00415 while (count);
00416
00417 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
00418 return -1;
00419
00420 objc_free (mutex->backend);
00421 mutex->backend = NULL;
00422 }
00423 return 0;
00424 }
00425
00426
00427 static inline int
00428 __gthread_objc_mutex_lock (objc_mutex_t mutex)
00429 {
00430 if (__gthread_active_p ()
00431 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
00432 {
00433 return -1;
00434 }
00435
00436 return 0;
00437 }
00438
00439
00440 static inline int
00441 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
00442 {
00443 if (__gthread_active_p ()
00444 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
00445 {
00446 return -1;
00447 }
00448
00449 return 0;
00450 }
00451
00452
00453 static inline int
00454 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
00455 {
00456 if (__gthread_active_p ()
00457 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
00458 {
00459 return -1;
00460 }
00461
00462 return 0;
00463 }
00464
00465
00466
00467
00468 static inline int
00469 __gthread_objc_condition_allocate (objc_condition_t condition)
00470 {
00471 if (__gthread_active_p ())
00472 {
00473 condition->backend = objc_malloc (sizeof (pthread_cond_t));
00474
00475 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
00476 {
00477 objc_free (condition->backend);
00478 condition->backend = NULL;
00479 return -1;
00480 }
00481 }
00482
00483 return 0;
00484 }
00485
00486
00487 static inline int
00488 __gthread_objc_condition_deallocate (objc_condition_t condition)
00489 {
00490 if (__gthread_active_p ())
00491 {
00492 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
00493 return -1;
00494
00495 objc_free (condition->backend);
00496 condition->backend = NULL;
00497 }
00498 return 0;
00499 }
00500
00501
00502 static inline int
00503 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
00504 {
00505 if (__gthread_active_p ())
00506 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
00507 (pthread_mutex_t *) mutex->backend);
00508 else
00509 return 0;
00510 }
00511
00512
00513 static inline int
00514 __gthread_objc_condition_broadcast (objc_condition_t condition)
00515 {
00516 if (__gthread_active_p ())
00517 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
00518 else
00519 return 0;
00520 }
00521
00522
00523 static inline int
00524 __gthread_objc_condition_signal (objc_condition_t condition)
00525 {
00526 if (__gthread_active_p ())
00527 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
00528 else
00529 return 0;
00530 }
00531
00532 #else
00533
00534 static inline int
00535 __gthread_once (__gthread_once_t *once, void (*func) (void))
00536 {
00537 if (__gthread_active_p ())
00538 return __gthrw_(pthread_once) (once, func);
00539 else
00540 return -1;
00541 }
00542
00543 static inline int
00544 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
00545 {
00546 return __gthrw_(pthread_key_create) (key, dtor);
00547 }
00548
00549 static inline int
00550 __gthread_key_delete (__gthread_key_t key)
00551 {
00552 return __gthrw_(pthread_key_delete) (key);
00553 }
00554
00555 static inline void *
00556 __gthread_getspecific (__gthread_key_t key)
00557 {
00558 return __gthrw_(pthread_getspecific) (key);
00559 }
00560
00561 static inline int
00562 __gthread_setspecific (__gthread_key_t key, const void *ptr)
00563 {
00564 return __gthrw_(pthread_setspecific) (key, ptr);
00565 }
00566
00567 static inline int
00568 __gthread_mutex_lock (__gthread_mutex_t *mutex)
00569 {
00570 if (__gthread_active_p ())
00571 return __gthrw_(pthread_mutex_lock) (mutex);
00572 else
00573 return 0;
00574 }
00575
00576 static inline int
00577 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
00578 {
00579 if (__gthread_active_p ())
00580 return __gthrw_(pthread_mutex_trylock) (mutex);
00581 else
00582 return 0;
00583 }
00584
00585 static inline int
00586 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
00587 {
00588 if (__gthread_active_p ())
00589 return __gthrw_(pthread_mutex_unlock) (mutex);
00590 else
00591 return 0;
00592 }
00593
00594 static inline int
00595 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
00596 {
00597 mutex->depth = 0;
00598 mutex->owner = (pthread_t) 0;
00599 return __gthrw_(pthread_mutex_init) (&mutex->actual, NULL);
00600 }
00601
00602 static inline int
00603 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
00604 {
00605 if (__gthread_active_p ())
00606 {
00607 pthread_t me = __gthrw_(pthread_self) ();
00608
00609 if (mutex->owner != me)
00610 {
00611 __gthrw_(pthread_mutex_lock) (&mutex->actual);
00612 mutex->owner = me;
00613 }
00614
00615 mutex->depth++;
00616 }
00617 return 0;
00618 }
00619
00620 static inline int
00621 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
00622 {
00623 if (__gthread_active_p ())
00624 {
00625 pthread_t me = __gthrw_(pthread_self) ();
00626
00627 if (mutex->owner != me)
00628 {
00629 if (__gthrw_(pthread_mutex_trylock) (&mutex->actual))
00630 return 1;
00631 mutex->owner = me;
00632 }
00633
00634 mutex->depth++;
00635 }
00636 return 0;
00637 }
00638
00639 static inline int
00640 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
00641 {
00642 if (__gthread_active_p ())
00643 {
00644 if (--mutex->depth == 0)
00645 {
00646 mutex->owner = (pthread_t) 0;
00647 __gthrw_(pthread_mutex_unlock) (&mutex->actual);
00648 }
00649 }
00650 return 0;
00651 }
00652
00653 #endif
00654
00655 #endif