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
47typedef pthread_t __gthread_t;
48typedef pthread_key_t __gthread_key_t;
49typedef pthread_once_t __gthread_once_t;
50typedef pthread_mutex_t __gthread_mutex_t;
52typedef pthread_rwlock_t __gthread_rwlock_t;
54typedef pthread_mutex_t __gthread_recursive_mutex_t;
55typedef pthread_cond_t __gthread_cond_t;
56typedef struct timespec __gthread_time_t;
60#define __GTHREAD_HAS_COND 1
62#define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
63#define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
65#define __GTHREAD_RWLOCK_INIT PTHREAD_RWLOCK_INITIALIZER
67#define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
68#if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
69#define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
70#elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
71#define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
73#define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
75#define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
76#define __GTHREAD_TIME_INIT {0,0}
78#ifdef _GTHREAD_USE_MUTEX_INIT_FUNC
79# undef __GTHREAD_MUTEX_INIT
81#ifdef _GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC
82# undef __GTHREAD_RECURSIVE_MUTEX_INIT
83# undef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
84# define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
86#ifdef _GTHREAD_USE_COND_INIT_FUNC
87# undef __GTHREAD_COND_INIT
88# define __GTHREAD_COND_INIT_FUNCTION __gthread_cond_init_function
91#if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
92# ifndef __gthrw_pragma
93# define __gthrw_pragma(pragma)
95# define __gthrw2(name,name2,type) \
96 static __typeof(type) name \
97 __attribute__ ((__weakref__(#name2), __copy__ (type))); \
98 __gthrw_pragma(weak type)
99# define __gthrw_(name) __gthrw_ ## name
101# define __gthrw2(name,name2,type)
102# define __gthrw_(name) name
106#define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
109__gthrw(pthread_getspecific)
110__gthrw(pthread_setspecific)
112__gthrw(pthread_create)
114__gthrw(pthread_equal)
116__gthrw(pthread_detach)
118__gthrw(pthread_cancel)
122__gthrw(pthread_mutex_lock)
123__gthrw(pthread_mutex_trylock)
124#if _GTHREAD_USE_MUTEX_TIMEDLOCK
125__gthrw(pthread_mutex_timedlock)
127__gthrw(pthread_mutex_unlock)
128__gthrw(pthread_mutex_init)
129__gthrw(pthread_mutex_destroy)
131__gthrw(pthread_cond_init)
132__gthrw(pthread_cond_broadcast)
133__gthrw(pthread_cond_signal)
134__gthrw(pthread_cond_wait)
135__gthrw(pthread_cond_timedwait)
136__gthrw(pthread_cond_destroy)
138__gthrw(pthread_key_create)
139__gthrw(pthread_key_delete)
140__gthrw(pthread_mutexattr_init)
141__gthrw(pthread_mutexattr_settype)
142__gthrw(pthread_mutexattr_destroy)
145__gthrw(pthread_rwlock_rdlock)
146__gthrw(pthread_rwlock_tryrdlock)
147__gthrw(pthread_rwlock_wrlock)
148__gthrw(pthread_rwlock_trywrlock)
149__gthrw(pthread_rwlock_unlock)
152#if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
155#ifdef _POSIX_PRIORITY_SCHEDULING
156#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
157__gthrw(sched_get_priority_max)
158__gthrw(sched_get_priority_min)
161__gthrw(pthread_attr_destroy)
162__gthrw(pthread_attr_init)
163__gthrw(pthread_attr_setdetachstate)
164#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
165__gthrw(pthread_getschedparam)
166__gthrw(pthread_setschedparam)
170#if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
183#if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
185static volatile int __gthread_active = -1;
188__gthread_trigger (
void)
190 __gthread_active = 1;
194__gthread_active_p (
void)
196 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
197 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
200 int __gthread_active_latest_value = __gthread_active;
205 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
207 if (__gthrw_(pthread_once))
211 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
212 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
213 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
217 if (__gthread_active < 0)
218 __gthread_active = 0;
220 __gthread_active_latest_value = __gthread_active;
223 return __gthread_active_latest_value != 0;
250__gthrw2(__gthrw_(__pthread_key_create),
251 __pthread_key_create,
253# define GTHR_ACTIVE_PROXY __gthrw_(__pthread_key_create)
254#elif defined (__BIONIC__)
255# define GTHR_ACTIVE_PROXY __gthrw_(pthread_create)
257# define GTHR_ACTIVE_PROXY __gthrw_(pthread_cancel)
261__gthread_active_p (
void)
263 static void *
const __gthread_active_ptr
264 = __extension__ (
void *) >HR_ACTIVE_PROXY;
265 return __gthread_active_ptr != 0;
289#if defined(__hppa__) && defined(__hpux__)
291static volatile int __gthread_active = -1;
294__gthread_active_p (
void)
297 int __gthread_active_latest_value = __gthread_active;
300 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
302 pthread_default_stacksize_np (0, &__s);
303 __gthread_active = __s ? 1 : 0;
304 __gthread_active_latest_value = __gthread_active;
307 return __gthread_active_latest_value != 0;
313__gthread_active_p (
void)
332static pthread_key_t _objc_thread_storage;
333static pthread_attr_t _objc_thread_attribs;
336static void *thread_local_storage = NULL;
342__gthread_objc_init_thread_system (
void)
344 if (__gthread_active_p ())
347 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
352 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
353 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
354 PTHREAD_CREATE_DETACHED) == 0)
364__gthread_objc_close_thread_system (
void)
366 if (__gthread_active_p ()
367 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
368 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
377static inline objc_thread_t
378__gthread_objc_thread_detach (
void (*func)(
void *),
void *arg)
380 objc_thread_t thread_id;
381 pthread_t new_thread_handle;
383 if (!__gthread_active_p ())
386 if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
387 (
void *) func, arg)))
388 thread_id = (objc_thread_t) new_thread_handle;
397__gthread_objc_thread_set_priority (
int priority)
399 if (!__gthread_active_p ())
403#ifdef _POSIX_PRIORITY_SCHEDULING
404#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
405 pthread_t thread_id = __gthrw_(pthread_self) ();
407 struct sched_param params;
408 int priority_min, priority_max;
410 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, ¶ms) == 0)
412 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
415 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
418 if (priority > priority_max)
419 priority = priority_max;
420 else if (priority < priority_min)
421 priority = priority_min;
422 params.sched_priority = priority;
429 if (__gthrw_(pthread_setschedparam) (thread_id, policy, ¶ms) == 0)
440__gthread_objc_thread_get_priority (
void)
442#ifdef _POSIX_PRIORITY_SCHEDULING
443#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
444 if (__gthread_active_p ())
447 struct sched_param params;
449 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, ¶ms) == 0)
450 return params.sched_priority;
457 return OBJC_THREAD_INTERACTIVE_PRIORITY;
462__gthread_objc_thread_yield (
void)
464 if (__gthread_active_p ())
465 __gthrw_(sched_yield) ();
470__gthread_objc_thread_exit (
void)
472 if (__gthread_active_p ())
474 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
481static inline objc_thread_t
482__gthread_objc_thread_id (
void)
484 if (__gthread_active_p ())
485 return (objc_thread_t) __gthrw_(pthread_self) ();
487 return (objc_thread_t) 1;
492__gthread_objc_thread_set_data (
void *value)
494 if (__gthread_active_p ())
495 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
498 thread_local_storage = value;
505__gthread_objc_thread_get_data (
void)
507 if (__gthread_active_p ())
508 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
510 return thread_local_storage;
517__gthread_objc_mutex_allocate (objc_mutex_t mutex)
519 if (__gthread_active_p ())
521 mutex->backend = objc_malloc (
sizeof (pthread_mutex_t));
523 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
525 objc_free (mutex->backend);
526 mutex->backend = NULL;
536__gthread_objc_mutex_deallocate (objc_mutex_t mutex)
538 if (__gthread_active_p ())
549 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
555 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
558 objc_free (mutex->backend);
559 mutex->backend = NULL;
566__gthread_objc_mutex_lock (objc_mutex_t mutex)
568 if (__gthread_active_p ()
569 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
579__gthread_objc_mutex_trylock (objc_mutex_t mutex)
581 if (__gthread_active_p ()
582 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
592__gthread_objc_mutex_unlock (objc_mutex_t mutex)
594 if (__gthread_active_p ()
595 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
607__gthread_objc_condition_allocate (objc_condition_t condition)
609 if (__gthread_active_p ())
611 condition->backend = objc_malloc (
sizeof (pthread_cond_t));
613 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
615 objc_free (condition->backend);
616 condition->backend = NULL;
626__gthread_objc_condition_deallocate (objc_condition_t condition)
628 if (__gthread_active_p ())
630 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
633 objc_free (condition->backend);
634 condition->backend = NULL;
641__gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
643 if (__gthread_active_p ())
644 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
645 (pthread_mutex_t *) mutex->backend);
652__gthread_objc_condition_broadcast (objc_condition_t condition)
654 if (__gthread_active_p ())
655 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
662__gthread_objc_condition_signal (objc_condition_t condition)
664 if (__gthread_active_p ())
665 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
673__gthread_create (__gthread_t *__threadid,
void *(*__func) (
void*),
676 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
680__gthread_join (__gthread_t __threadid,
void **__value_ptr)
682 return __gthrw_(pthread_join) (__threadid, __value_ptr);
686__gthread_detach (__gthread_t __threadid)
688 return __gthrw_(pthread_detach) (__threadid);
692__gthread_equal (__gthread_t __t1, __gthread_t __t2)
694 return __gthrw_(pthread_equal) (__t1, __t2);
697static inline __gthread_t
700 return __gthrw_(pthread_self) ();
704__gthread_yield (
void)
706 return __gthrw_(sched_yield) ();
710__gthread_once (__gthread_once_t *__once,
void (*__func) (
void))
712 if (__gthread_active_p ())
713 return __gthrw_(pthread_once) (__once, __func);
719__gthread_key_create (__gthread_key_t *__key,
void (*__dtor) (
void *))
721 return __gthrw_(pthread_key_create) (__key, __dtor);
725__gthread_key_delete (__gthread_key_t __key)
727 return __gthrw_(pthread_key_delete) (__key);
731__gthread_getspecific (__gthread_key_t __key)
733 return __gthrw_(pthread_getspecific) (__key);
737__gthread_setspecific (__gthread_key_t __key,
const void *__ptr)
739 return __gthrw_(pthread_setspecific) (__key, __ptr);
743__gthread_mutex_init_function (__gthread_mutex_t *__mutex)
745 if (__gthread_active_p ())
746 __gthrw_(pthread_mutex_init) (__mutex, NULL);
750__gthread_mutex_destroy (__gthread_mutex_t *__mutex)
752 if (__gthread_active_p ())
753 return __gthrw_(pthread_mutex_destroy) (__mutex);
759__gthread_mutex_lock (__gthread_mutex_t *__mutex)
761 if (__gthread_active_p ())
762 return __gthrw_(pthread_mutex_lock) (__mutex);
768__gthread_mutex_trylock (__gthread_mutex_t *__mutex)
770 if (__gthread_active_p ())
771 return __gthrw_(pthread_mutex_trylock) (__mutex);
776#if _GTHREAD_USE_MUTEX_TIMEDLOCK
778__gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
779 const __gthread_time_t *__abs_timeout)
781 if (__gthread_active_p ())
782 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
789__gthread_mutex_unlock (__gthread_mutex_t *__mutex)
791 if (__gthread_active_p ())
792 return __gthrw_(pthread_mutex_unlock) (__mutex);
797#if !defined( PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) \
798 || defined(_GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC)
800__gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
802 if (__gthread_active_p ())
804 pthread_mutexattr_t __attr;
807 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
809 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
810 PTHREAD_MUTEX_RECURSIVE);
812 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
814 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
822__gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
824 return __gthread_mutex_lock (__mutex);
828__gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
830 return __gthread_mutex_trylock (__mutex);
833#if _GTHREAD_USE_MUTEX_TIMEDLOCK
835__gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
836 const __gthread_time_t *__abs_timeout)
838 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
843__gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
845 return __gthread_mutex_unlock (__mutex);
849__gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex)
851 return __gthread_mutex_destroy (__mutex);
854#ifdef _GTHREAD_USE_COND_INIT_FUNC
856__gthread_cond_init_function (__gthread_cond_t *__cond)
858 if (__gthread_active_p ())
859 __gthrw_(pthread_cond_init) (__cond, NULL);
864__gthread_cond_broadcast (__gthread_cond_t *__cond)
866 return __gthrw_(pthread_cond_broadcast) (__cond);
870__gthread_cond_signal (__gthread_cond_t *__cond)
872 return __gthrw_(pthread_cond_signal) (__cond);
876__gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
878 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
882__gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
883 const __gthread_time_t *__abs_timeout)
885 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
889__gthread_cond_wait_recursive (__gthread_cond_t *__cond,
890 __gthread_recursive_mutex_t *__mutex)
892 return __gthread_cond_wait (__cond, __mutex);
896__gthread_cond_destroy (__gthread_cond_t* __cond)
898 return __gthrw_(pthread_cond_destroy) (__cond);
903__gthread_rwlock_rdlock (__gthread_rwlock_t *__rwlock)
905 if (__gthread_active_p ())
906 return __gthrw_(pthread_rwlock_rdlock) (__rwlock);
912__gthread_rwlock_tryrdlock (__gthread_rwlock_t *__rwlock)
914 if (__gthread_active_p ())
915 return __gthrw_(pthread_rwlock_tryrdlock) (__rwlock);
921__gthread_rwlock_wrlock (__gthread_rwlock_t *__rwlock)
923 if (__gthread_active_p ())
924 return __gthrw_(pthread_rwlock_wrlock) (__rwlock);
930__gthread_rwlock_trywrlock (__gthread_rwlock_t *__rwlock)
932 if (__gthread_active_p ())
933 return __gthrw_(pthread_rwlock_trywrlock) (__rwlock);
939__gthread_rwlock_unlock (__gthread_rwlock_t *__rwlock)
941 if (__gthread_active_p ())
942 return __gthrw_(pthread_rwlock_unlock) (__rwlock);