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