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
48# if __has_attribute(__always_inline__)
49# define __GTHREAD_ALWAYS_INLINE __attribute__((__always_inline__))
52#ifndef __GTHREAD_ALWAYS_INLINE
53# define __GTHREAD_ALWAYS_INLINE
57# define __GTHREAD_INLINE inline __GTHREAD_ALWAYS_INLINE
59# define __GTHREAD_INLINE static inline
62typedef pthread_t __gthread_t;
63typedef pthread_key_t __gthread_key_t;
64typedef pthread_once_t __gthread_once_t;
65typedef pthread_mutex_t __gthread_mutex_t;
67typedef pthread_rwlock_t __gthread_rwlock_t;
69typedef pthread_mutex_t __gthread_recursive_mutex_t;
70typedef pthread_cond_t __gthread_cond_t;
71typedef struct timespec __gthread_time_t;
75#define __GTHREAD_HAS_COND 1
77#define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
78#define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
80#define __GTHREAD_RWLOCK_INIT PTHREAD_RWLOCK_INITIALIZER
82#define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
83#if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
84#define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
85#elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
86#define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
88#define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
90#define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
91#define __GTHREAD_TIME_INIT {0,0}
93#ifdef _GTHREAD_USE_MUTEX_INIT_FUNC
94# undef __GTHREAD_MUTEX_INIT
96#ifdef _GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC
97# undef __GTHREAD_RECURSIVE_MUTEX_INIT
98# undef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
99# define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
101#ifdef _GTHREAD_USE_COND_INIT_FUNC
102# undef __GTHREAD_COND_INIT
103# define __GTHREAD_COND_INIT_FUNCTION __gthread_cond_init_function
106#if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
107# ifndef __gthrw_pragma
108# define __gthrw_pragma(pragma)
110# define __gthrw2(name,name2,type) \
111 static __typeof(type) name \
112 __attribute__ ((__weakref__(#name2), __copy__ (type))); \
113 __gthrw_pragma(weak type)
114# define __gthrw_(name) __gthrw_ ## name
116# define __gthrw2(name,name2,type)
117# define __gthrw_(name) name
121#define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
124__gthrw(pthread_getspecific)
125__gthrw(pthread_setspecific)
127__gthrw(pthread_create)
129__gthrw(pthread_equal)
131__gthrw(pthread_detach)
133__gthrw(pthread_cancel)
137__gthrw(pthread_mutex_lock)
138__gthrw(pthread_mutex_trylock)
139#if _GTHREAD_USE_MUTEX_TIMEDLOCK
140__gthrw(pthread_mutex_timedlock)
142__gthrw(pthread_mutex_unlock)
143__gthrw(pthread_mutex_init)
144__gthrw(pthread_mutex_destroy)
146__gthrw(pthread_cond_init)
147__gthrw(pthread_cond_broadcast)
148__gthrw(pthread_cond_signal)
149__gthrw(pthread_cond_wait)
150__gthrw(pthread_cond_timedwait)
151__gthrw(pthread_cond_destroy)
153__gthrw(pthread_key_create)
154__gthrw(pthread_key_delete)
155__gthrw(pthread_mutexattr_init)
156__gthrw(pthread_mutexattr_settype)
157__gthrw(pthread_mutexattr_destroy)
160__gthrw(pthread_rwlock_rdlock)
161__gthrw(pthread_rwlock_tryrdlock)
162__gthrw(pthread_rwlock_wrlock)
163__gthrw(pthread_rwlock_trywrlock)
164__gthrw(pthread_rwlock_unlock)
167#if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
170#ifdef _POSIX_PRIORITY_SCHEDULING
171#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
172__gthrw(sched_get_priority_max)
173__gthrw(sched_get_priority_min)
176__gthrw(pthread_attr_destroy)
177__gthrw(pthread_attr_init)
178__gthrw(pthread_attr_setdetachstate)
179#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
180__gthrw(pthread_getschedparam)
181__gthrw(pthread_setschedparam)
185#if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
198#if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
200#pragma GCC visibility push(hidden)
201__GTHREAD_INLINE
volatile int *
202__gthread_active (
void)
204 static volatile int __gthread_active_var = -1;
205 return &__gthread_active_var;
207#pragma GCC visibility pop
210__gthread_trigger (
void)
212 *__gthread_active () = 1;
215#pragma GCC visibility push(hidden)
217__gthread_active_p (
void)
219 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
220 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
223 int __gthread_active_latest_value = *__gthread_active ();
228 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
230 if (__gthrw_(pthread_once))
234 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
235 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
236 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
240 if (*__gthread_active () < 0)
241 *__gthread_active () = 0;
243 __gthread_active_latest_value = *__gthread_active ();
246 return __gthread_active_latest_value != 0;
248#pragma GCC visibility pop
274__gthrw2(__gthrw_(__pthread_key_create),
275 __pthread_key_create,
277# define GTHR_ACTIVE_PROXY __gthrw_(__pthread_key_create)
278#elif defined (__BIONIC__)
279# define GTHR_ACTIVE_PROXY __gthrw_(pthread_create)
281# define GTHR_ACTIVE_PROXY __gthrw_(pthread_cancel)
284#pragma GCC visibility push(hidden)
286__gthread_active_p (
void)
288 static void *
const __gthread_active_ptr
289 = __extension__ (
void *) >HR_ACTIVE_PROXY;
290 return __gthread_active_ptr != 0;
292#pragma GCC visibility pop
315#if defined(__hppa__) && defined(__hpux__)
317#pragma GCC visibility push(hidden)
318__GTHREAD_INLINE
volatile int *
319__gthread_active (
void)
321 static volatile int __gthread_active_var = -1;
322 return &__gthread_active_var;
324#pragma GCC visibility pop
327__gthread_active_p (
void)
330 int __gthread_active_latest_value = *__gthread_active ();
333 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
335 pthread_default_stacksize_np (0, &__s);
336 *__gthread_active () = __s ? 1 : 0;
337 __gthread_active_latest_value = *__gthread_active ();
340 return __gthread_active_latest_value != 0;
346__gthread_active_p (
void)
365static pthread_key_t _objc_thread_storage;
366static pthread_attr_t _objc_thread_attribs;
369static void *thread_local_storage = NULL;
375__gthread_objc_init_thread_system (
void)
377 if (__gthread_active_p ())
380 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
385 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
386 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
387 PTHREAD_CREATE_DETACHED) == 0)
397__gthread_objc_close_thread_system (
void)
399 if (__gthread_active_p ()
400 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
401 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
410static inline objc_thread_t
411__gthread_objc_thread_detach (
void (*func)(
void *),
void *arg)
413 objc_thread_t thread_id;
414 pthread_t new_thread_handle;
416 if (!__gthread_active_p ())
419 if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
420 (
void *) func, arg)))
421 thread_id = (objc_thread_t) new_thread_handle;
430__gthread_objc_thread_set_priority (
int priority)
432 if (!__gthread_active_p ())
436#ifdef _POSIX_PRIORITY_SCHEDULING
437#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
438 pthread_t thread_id = __gthrw_(pthread_self) ();
440 struct sched_param params;
441 int priority_min, priority_max;
443 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, ¶ms) == 0)
445 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
448 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
451 if (priority > priority_max)
452 priority = priority_max;
453 else if (priority < priority_min)
454 priority = priority_min;
455 params.sched_priority = priority;
462 if (__gthrw_(pthread_setschedparam) (thread_id, policy, ¶ms) == 0)
473__gthread_objc_thread_get_priority (
void)
475#ifdef _POSIX_PRIORITY_SCHEDULING
476#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
477 if (__gthread_active_p ())
480 struct sched_param params;
482 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, ¶ms) == 0)
483 return params.sched_priority;
490 return OBJC_THREAD_INTERACTIVE_PRIORITY;
495__gthread_objc_thread_yield (
void)
497 if (__gthread_active_p ())
498 __gthrw_(sched_yield) ();
503__gthread_objc_thread_exit (
void)
505 if (__gthread_active_p ())
507 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
514static inline objc_thread_t
515__gthread_objc_thread_id (
void)
517 if (__gthread_active_p ())
518 return (objc_thread_t) __gthrw_(pthread_self) ();
520 return (objc_thread_t) 1;
525__gthread_objc_thread_set_data (
void *value)
527 if (__gthread_active_p ())
528 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
531 thread_local_storage = value;
538__gthread_objc_thread_get_data (
void)
540 if (__gthread_active_p ())
541 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
543 return thread_local_storage;
550__gthread_objc_mutex_allocate (objc_mutex_t mutex)
552 if (__gthread_active_p ())
554 mutex->backend = objc_malloc (
sizeof (pthread_mutex_t));
556 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
558 objc_free (mutex->backend);
559 mutex->backend = NULL;
569__gthread_objc_mutex_deallocate (objc_mutex_t mutex)
571 if (__gthread_active_p ())
582 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
588 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
591 objc_free (mutex->backend);
592 mutex->backend = NULL;
599__gthread_objc_mutex_lock (objc_mutex_t mutex)
601 if (__gthread_active_p ()
602 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
612__gthread_objc_mutex_trylock (objc_mutex_t mutex)
614 if (__gthread_active_p ()
615 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
625__gthread_objc_mutex_unlock (objc_mutex_t mutex)
627 if (__gthread_active_p ()
628 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
640__gthread_objc_condition_allocate (objc_condition_t condition)
642 if (__gthread_active_p ())
644 condition->backend = objc_malloc (
sizeof (pthread_cond_t));
646 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
648 objc_free (condition->backend);
649 condition->backend = NULL;
659__gthread_objc_condition_deallocate (objc_condition_t condition)
661 if (__gthread_active_p ())
663 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
666 objc_free (condition->backend);
667 condition->backend = NULL;
674__gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
676 if (__gthread_active_p ())
677 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
678 (pthread_mutex_t *) mutex->backend);
685__gthread_objc_condition_broadcast (objc_condition_t condition)
687 if (__gthread_active_p ())
688 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
695__gthread_objc_condition_signal (objc_condition_t condition)
697 if (__gthread_active_p ())
698 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
706__gthread_create (__gthread_t *__threadid,
void *(*__func) (
void*),
709 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
713__gthread_join (__gthread_t __threadid,
void **__value_ptr)
715 return __gthrw_(pthread_join) (__threadid, __value_ptr);
719__gthread_detach (__gthread_t __threadid)
721 return __gthrw_(pthread_detach) (__threadid);
725__gthread_equal (__gthread_t __t1, __gthread_t __t2)
727 return __gthrw_(pthread_equal) (__t1, __t2);
730__GTHREAD_INLINE __gthread_t
733 return __gthrw_(pthread_self) ();
737__gthread_yield (
void)
739 return __gthrw_(sched_yield) ();
743__gthread_once (__gthread_once_t *__once,
void (*__func) (
void))
745 if (__gthread_active_p ())
746 return __gthrw_(pthread_once) (__once, __func);
752__gthread_key_create (__gthread_key_t *__key,
void (*__dtor) (
void *))
754 return __gthrw_(pthread_key_create) (__key, __dtor);
758__gthread_key_delete (__gthread_key_t __key)
760 return __gthrw_(pthread_key_delete) (__key);
763__GTHREAD_INLINE
void *
764__gthread_getspecific (__gthread_key_t __key)
766 return __gthrw_(pthread_getspecific) (__key);
770__gthread_setspecific (__gthread_key_t __key,
const void *__ptr)
772 return __gthrw_(pthread_setspecific) (__key, __ptr);
776__gthread_mutex_init_function (__gthread_mutex_t *__mutex)
778 if (__gthread_active_p ())
779 __gthrw_(pthread_mutex_init) (__mutex, NULL);
783__gthread_mutex_destroy (__gthread_mutex_t *__mutex)
785 if (__gthread_active_p ())
786 return __gthrw_(pthread_mutex_destroy) (__mutex);
792__gthread_mutex_lock (__gthread_mutex_t *__mutex)
794 if (__gthread_active_p ())
795 return __gthrw_(pthread_mutex_lock) (__mutex);
801__gthread_mutex_trylock (__gthread_mutex_t *__mutex)
803 if (__gthread_active_p ())
804 return __gthrw_(pthread_mutex_trylock) (__mutex);
809#if _GTHREAD_USE_MUTEX_TIMEDLOCK
811__gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
812 const __gthread_time_t *__abs_timeout)
814 if (__gthread_active_p ())
815 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
822__gthread_mutex_unlock (__gthread_mutex_t *__mutex)
824 if (__gthread_active_p ())
825 return __gthrw_(pthread_mutex_unlock) (__mutex);
830#if !defined( PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) \
831 || defined(_GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC)
833__gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
835 if (__gthread_active_p ())
837 pthread_mutexattr_t __attr;
840 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
842 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
843 PTHREAD_MUTEX_RECURSIVE);
845 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
847 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
855__gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
857 return __gthread_mutex_lock (__mutex);
861__gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
863 return __gthread_mutex_trylock (__mutex);
866#if _GTHREAD_USE_MUTEX_TIMEDLOCK
868__gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
869 const __gthread_time_t *__abs_timeout)
871 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
876__gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
878 return __gthread_mutex_unlock (__mutex);
882__gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex)
884 return __gthread_mutex_destroy (__mutex);
887#ifdef _GTHREAD_USE_COND_INIT_FUNC
889__gthread_cond_init_function (__gthread_cond_t *__cond)
891 if (__gthread_active_p ())
892 __gthrw_(pthread_cond_init) (__cond, NULL);
897__gthread_cond_broadcast (__gthread_cond_t *__cond)
899 return __gthrw_(pthread_cond_broadcast) (__cond);
903__gthread_cond_signal (__gthread_cond_t *__cond)
905 return __gthrw_(pthread_cond_signal) (__cond);
909__gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
911 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
915__gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
916 const __gthread_time_t *__abs_timeout)
918 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
922__gthread_cond_wait_recursive (__gthread_cond_t *__cond,
923 __gthread_recursive_mutex_t *__mutex)
925 return __gthread_cond_wait (__cond, __mutex);
929__gthread_cond_destroy (__gthread_cond_t* __cond)
931 return __gthrw_(pthread_cond_destroy) (__cond);
936__gthread_rwlock_rdlock (__gthread_rwlock_t *__rwlock)
938 if (__gthread_active_p ())
939 return __gthrw_(pthread_rwlock_rdlock) (__rwlock);
945__gthread_rwlock_tryrdlock (__gthread_rwlock_t *__rwlock)
947 if (__gthread_active_p ())
948 return __gthrw_(pthread_rwlock_tryrdlock) (__rwlock);
954__gthread_rwlock_wrlock (__gthread_rwlock_t *__rwlock)
956 if (__gthread_active_p ())
957 return __gthrw_(pthread_rwlock_wrlock) (__rwlock);
963__gthread_rwlock_trywrlock (__gthread_rwlock_t *__rwlock)
965 if (__gthread_active_p ())
966 return __gthrw_(pthread_rwlock_trywrlock) (__rwlock);
972__gthread_rwlock_unlock (__gthread_rwlock_t *__rwlock)
974 if (__gthread_active_p ())
975 return __gthrw_(pthread_rwlock_unlock) (__rwlock);
983#undef __GTHREAD_INLINE
984#undef __GTHREAD_ALWAYS_INLINE