26 #ifndef _GLIBCXX_GCC_GTHR_POSIX_H
27 #define _GLIBCXX_GCC_GTHR_POSIX_H
33 #define __GTHREADS_CXX0X 1
37 #if ((defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)) \
38 || !defined(_GTHREAD_USE_MUTEX_TIMEDLOCK))
40 # if defined(_POSIX_TIMEOUTS) && _POSIX_TIMEOUTS >= 0
41 # define _GTHREAD_USE_MUTEX_TIMEDLOCK 1
43 # define _GTHREAD_USE_MUTEX_TIMEDLOCK 0
47 typedef pthread_t __gthread_t;
48 typedef pthread_key_t __gthread_key_t;
49 typedef pthread_once_t __gthread_once_t;
50 typedef pthread_mutex_t __gthread_mutex_t;
51 typedef pthread_mutex_t __gthread_recursive_mutex_t;
52 typedef pthread_cond_t __gthread_cond_t;
53 typedef struct timespec __gthread_time_t;
57 #define __GTHREAD_HAS_COND 1
59 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
60 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
61 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
62 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
63 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
64 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
65 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
67 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
69 #define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
70 #define __GTHREAD_TIME_INIT {0,0}
72 #ifdef _GTHREAD_USE_MUTEX_INIT_FUNC
73 # undef __GTHREAD_MUTEX_INIT
75 #ifdef _GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC
76 # undef __GTHREAD_RECURSIVE_MUTEX_INIT
77 # undef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
78 # define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
80 #ifdef _GTHREAD_USE_COND_INIT_FUNC
81 # undef __GTHREAD_COND_INIT
82 # define __GTHREAD_COND_INIT_FUNCTION __gthread_cond_init_function
85 #if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
86 # ifndef __gthrw_pragma
87 # define __gthrw_pragma(pragma)
89 # define __gthrw2(name,name2,type) \
90 static __typeof(type) name __attribute__ ((__weakref__(#name2))); \
91 __gthrw_pragma(weak type)
92 # define __gthrw_(name) __gthrw_ ## name
94 # define __gthrw2(name,name2,type)
95 # define __gthrw_(name) name
99 #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
101 __gthrw(pthread_once)
102 __gthrw(pthread_getspecific)
103 __gthrw(pthread_setspecific)
105 __gthrw(pthread_create)
106 __gthrw(pthread_join)
107 __gthrw(pthread_equal)
108 __gthrw(pthread_self)
109 __gthrw(pthread_detach)
111 __gthrw(pthread_cancel)
115 __gthrw(pthread_mutex_lock)
116 __gthrw(pthread_mutex_trylock)
117 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
118 __gthrw(pthread_mutex_timedlock)
120 __gthrw(pthread_mutex_unlock)
121 __gthrw(pthread_mutex_init)
122 __gthrw(pthread_mutex_destroy)
124 __gthrw(pthread_cond_init)
125 __gthrw(pthread_cond_broadcast)
126 __gthrw(pthread_cond_signal)
127 __gthrw(pthread_cond_wait)
128 __gthrw(pthread_cond_timedwait)
129 __gthrw(pthread_cond_destroy)
131 __gthrw(pthread_key_create)
132 __gthrw(pthread_key_delete)
133 __gthrw(pthread_mutexattr_init)
134 __gthrw(pthread_mutexattr_settype)
135 __gthrw(pthread_mutexattr_destroy)
138 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
140 __gthrw(pthread_exit)
141 #ifdef _POSIX_PRIORITY_SCHEDULING
142 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
143 __gthrw(sched_get_priority_max)
144 __gthrw(sched_get_priority_min)
147 __gthrw(pthread_attr_destroy)
148 __gthrw(pthread_attr_init)
149 __gthrw(pthread_attr_setdetachstate)
150 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
151 __gthrw(pthread_getschedparam)
152 __gthrw(pthread_setschedparam)
156 #if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
169 #if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
171 static volatile int __gthread_active = -1;
174 __gthread_trigger (
void)
176 __gthread_active = 1;
180 __gthread_active_p (
void)
182 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
183 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
186 int __gthread_active_latest_value = __gthread_active;
191 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
193 if (__gthrw_(pthread_once))
197 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
198 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
199 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
203 if (__gthread_active < 0)
204 __gthread_active = 0;
206 __gthread_active_latest_value = __gthread_active;
209 return __gthread_active_latest_value != 0;
236 __gthrw2(__gthrw_(__pthread_key_create),
237 __pthread_key_create,
239 # define GTHR_ACTIVE_PROXY __gthrw_(__pthread_key_create)
240 #elif defined (__BIONIC__)
241 # define GTHR_ACTIVE_PROXY __gthrw_(pthread_create)
243 # define GTHR_ACTIVE_PROXY __gthrw_(pthread_cancel)
247 __gthread_active_p (
void)
249 static void *
const __gthread_active_ptr
250 = __extension__ (
void *) >HR_ACTIVE_PROXY;
251 return __gthread_active_ptr != 0;
275 #if defined(__hppa__) && defined(__hpux__)
277 static volatile int __gthread_active = -1;
280 __gthread_active_p (
void)
283 int __gthread_active_latest_value = __gthread_active;
286 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
288 pthread_default_stacksize_np (0, &__s);
289 __gthread_active = __s ? 1 : 0;
290 __gthread_active_latest_value = __gthread_active;
293 return __gthread_active_latest_value != 0;
299 __gthread_active_p (
void)
318 static pthread_key_t _objc_thread_storage;
319 static pthread_attr_t _objc_thread_attribs;
322 static void *thread_local_storage = NULL;
328 __gthread_objc_init_thread_system (
void)
330 if (__gthread_active_p ())
333 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
338 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
339 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
340 PTHREAD_CREATE_DETACHED) == 0)
350 __gthread_objc_close_thread_system (
void)
352 if (__gthread_active_p ()
353 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
354 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
363 static inline objc_thread_t
364 __gthread_objc_thread_detach (
void (*func)(
void *),
void *arg)
366 objc_thread_t thread_id;
367 pthread_t new_thread_handle;
369 if (!__gthread_active_p ())
372 if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
373 (
void *) func, arg)))
374 thread_id = (objc_thread_t) new_thread_handle;
383 __gthread_objc_thread_set_priority (
int priority)
385 if (!__gthread_active_p ())
389 #ifdef _POSIX_PRIORITY_SCHEDULING
390 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
391 pthread_t thread_id = __gthrw_(pthread_self) ();
393 struct sched_param params;
394 int priority_min, priority_max;
396 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, ¶ms) == 0)
398 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
401 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
404 if (priority > priority_max)
405 priority = priority_max;
406 else if (priority < priority_min)
407 priority = priority_min;
408 params.sched_priority = priority;
415 if (__gthrw_(pthread_setschedparam) (thread_id, policy, ¶ms) == 0)
426 __gthread_objc_thread_get_priority (
void)
428 #ifdef _POSIX_PRIORITY_SCHEDULING
429 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
430 if (__gthread_active_p ())
433 struct sched_param params;
435 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, ¶ms) == 0)
436 return params.sched_priority;
443 return OBJC_THREAD_INTERACTIVE_PRIORITY;
448 __gthread_objc_thread_yield (
void)
450 if (__gthread_active_p ())
451 __gthrw_(sched_yield) ();
456 __gthread_objc_thread_exit (
void)
458 if (__gthread_active_p ())
460 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
467 static inline objc_thread_t
468 __gthread_objc_thread_id (
void)
470 if (__gthread_active_p ())
471 return (objc_thread_t) __gthrw_(pthread_self) ();
473 return (objc_thread_t) 1;
478 __gthread_objc_thread_set_data (
void *value)
480 if (__gthread_active_p ())
481 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
484 thread_local_storage = value;
491 __gthread_objc_thread_get_data (
void)
493 if (__gthread_active_p ())
494 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
496 return thread_local_storage;
503 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
505 if (__gthread_active_p ())
507 mutex->backend = objc_malloc (
sizeof (pthread_mutex_t));
509 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
511 objc_free (mutex->backend);
512 mutex->backend = NULL;
522 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
524 if (__gthread_active_p ())
535 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
541 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
544 objc_free (mutex->backend);
545 mutex->backend = NULL;
552 __gthread_objc_mutex_lock (objc_mutex_t mutex)
554 if (__gthread_active_p ()
555 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
565 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
567 if (__gthread_active_p ()
568 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
578 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
580 if (__gthread_active_p ()
581 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
593 __gthread_objc_condition_allocate (objc_condition_t condition)
595 if (__gthread_active_p ())
597 condition->backend = objc_malloc (
sizeof (pthread_cond_t));
599 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
601 objc_free (condition->backend);
602 condition->backend = NULL;
612 __gthread_objc_condition_deallocate (objc_condition_t condition)
614 if (__gthread_active_p ())
616 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
619 objc_free (condition->backend);
620 condition->backend = NULL;
627 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
629 if (__gthread_active_p ())
630 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
631 (pthread_mutex_t *) mutex->backend);
638 __gthread_objc_condition_broadcast (objc_condition_t condition)
640 if (__gthread_active_p ())
641 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
648 __gthread_objc_condition_signal (objc_condition_t condition)
650 if (__gthread_active_p ())
651 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
659 __gthread_create (__gthread_t *__threadid,
void *(*__func) (
void*),
662 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
666 __gthread_join (__gthread_t __threadid,
void **__value_ptr)
668 return __gthrw_(pthread_join) (__threadid, __value_ptr);
672 __gthread_detach (__gthread_t __threadid)
674 return __gthrw_(pthread_detach) (__threadid);
678 __gthread_equal (__gthread_t __t1, __gthread_t __t2)
680 return __gthrw_(pthread_equal) (__t1, __t2);
683 static inline __gthread_t
684 __gthread_self (
void)
686 return __gthrw_(pthread_self) ();
690 __gthread_yield (
void)
692 return __gthrw_(sched_yield) ();
696 __gthread_once (__gthread_once_t *__once,
void (*__func) (
void))
698 if (__gthread_active_p ())
699 return __gthrw_(pthread_once) (__once, __func);
705 __gthread_key_create (__gthread_key_t *__key,
void (*__dtor) (
void *))
707 return __gthrw_(pthread_key_create) (__key, __dtor);
711 __gthread_key_delete (__gthread_key_t __key)
713 return __gthrw_(pthread_key_delete) (__key);
717 __gthread_getspecific (__gthread_key_t __key)
719 return __gthrw_(pthread_getspecific) (__key);
723 __gthread_setspecific (__gthread_key_t __key,
const void *__ptr)
725 return __gthrw_(pthread_setspecific) (__key, __ptr);
729 __gthread_mutex_init_function (__gthread_mutex_t *__mutex)
731 if (__gthread_active_p ())
732 __gthrw_(pthread_mutex_init) (__mutex, NULL);
736 __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
738 if (__gthread_active_p ())
739 return __gthrw_(pthread_mutex_destroy) (__mutex);
745 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
747 if (__gthread_active_p ())
748 return __gthrw_(pthread_mutex_lock) (__mutex);
754 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
756 if (__gthread_active_p ())
757 return __gthrw_(pthread_mutex_trylock) (__mutex);
762 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
764 __gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
765 const __gthread_time_t *__abs_timeout)
767 if (__gthread_active_p ())
768 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
775 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
777 if (__gthread_active_p ())
778 return __gthrw_(pthread_mutex_unlock) (__mutex);
783 #if !defined( PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) \
784 || defined(_GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC)
786 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
788 if (__gthread_active_p ())
790 pthread_mutexattr_t __attr;
793 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
795 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
796 PTHREAD_MUTEX_RECURSIVE);
798 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
800 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
808 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
810 return __gthread_mutex_lock (__mutex);
814 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
816 return __gthread_mutex_trylock (__mutex);
819 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
821 __gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
822 const __gthread_time_t *__abs_timeout)
824 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
829 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
831 return __gthread_mutex_unlock (__mutex);
835 __gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex)
837 return __gthread_mutex_destroy (__mutex);
840 #ifdef _GTHREAD_USE_COND_INIT_FUNC
842 __gthread_cond_init_function (__gthread_cond_t *__cond)
844 if (__gthread_active_p ())
845 __gthrw_(pthread_cond_init) (__cond, NULL);
850 __gthread_cond_broadcast (__gthread_cond_t *__cond)
852 return __gthrw_(pthread_cond_broadcast) (__cond);
856 __gthread_cond_signal (__gthread_cond_t *__cond)
858 return __gthrw_(pthread_cond_signal) (__cond);
862 __gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
864 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
868 __gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
869 const __gthread_time_t *__abs_timeout)
871 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
875 __gthread_cond_wait_recursive (__gthread_cond_t *__cond,
876 __gthread_recursive_mutex_t *__mutex)
878 return __gthread_cond_wait (__cond, __mutex);
882 __gthread_cond_destroy (__gthread_cond_t* __cond)
884 return __gthrw_(pthread_cond_destroy) (__cond);