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;
51typedef pthread_mutex_t __gthread_recursive_mutex_t;
52typedef pthread_cond_t __gthread_cond_t;
53typedef 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 \
91 __attribute__ ((__weakref__(#name2), __copy__ (type))); \
92 __gthrw_pragma(weak type)
93# define __gthrw_(name) __gthrw_ ## name
95# define __gthrw2(name,name2,type)
96# define __gthrw_(name) name
100#define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
103__gthrw(pthread_getspecific)
104__gthrw(pthread_setspecific)
106__gthrw(pthread_create)
108__gthrw(pthread_equal)
110__gthrw(pthread_detach)
112__gthrw(pthread_cancel)
116__gthrw(pthread_mutex_lock)
117__gthrw(pthread_mutex_trylock)
118#if _GTHREAD_USE_MUTEX_TIMEDLOCK
119__gthrw(pthread_mutex_timedlock)
121__gthrw(pthread_mutex_unlock)
122__gthrw(pthread_mutex_init)
123__gthrw(pthread_mutex_destroy)
125__gthrw(pthread_cond_init)
126__gthrw(pthread_cond_broadcast)
127__gthrw(pthread_cond_signal)
128__gthrw(pthread_cond_wait)
129__gthrw(pthread_cond_timedwait)
130__gthrw(pthread_cond_destroy)
132__gthrw(pthread_key_create)
133__gthrw(pthread_key_delete)
134__gthrw(pthread_mutexattr_init)
135__gthrw(pthread_mutexattr_settype)
136__gthrw(pthread_mutexattr_destroy)
139#if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
142#ifdef _POSIX_PRIORITY_SCHEDULING
143#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
144__gthrw(sched_get_priority_max)
145__gthrw(sched_get_priority_min)
148__gthrw(pthread_attr_destroy)
149__gthrw(pthread_attr_init)
150__gthrw(pthread_attr_setdetachstate)
151#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
152__gthrw(pthread_getschedparam)
153__gthrw(pthread_setschedparam)
157#if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
170#if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
172static volatile int __gthread_active = -1;
175__gthread_trigger (
void)
177 __gthread_active = 1;
181__gthread_active_p (
void)
183 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
184 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
187 int __gthread_active_latest_value = __gthread_active;
192 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
194 if (__gthrw_(pthread_once))
198 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
199 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
200 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
204 if (__gthread_active < 0)
205 __gthread_active = 0;
207 __gthread_active_latest_value = __gthread_active;
210 return __gthread_active_latest_value != 0;
237__gthrw2(__gthrw_(__pthread_key_create),
238 __pthread_key_create,
240# define GTHR_ACTIVE_PROXY __gthrw_(__pthread_key_create)
241#elif defined (__BIONIC__)
242# define GTHR_ACTIVE_PROXY __gthrw_(pthread_create)
244# define GTHR_ACTIVE_PROXY __gthrw_(pthread_cancel)
248__gthread_active_p (
void)
250 static void *
const __gthread_active_ptr
251 = __extension__ (
void *) >HR_ACTIVE_PROXY;
252 return __gthread_active_ptr != 0;
276#if defined(__hppa__) && defined(__hpux__)
278static volatile int __gthread_active = -1;
281__gthread_active_p (
void)
284 int __gthread_active_latest_value = __gthread_active;
287 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
289 pthread_default_stacksize_np (0, &__s);
290 __gthread_active = __s ? 1 : 0;
291 __gthread_active_latest_value = __gthread_active;
294 return __gthread_active_latest_value != 0;
300__gthread_active_p (
void)
319static pthread_key_t _objc_thread_storage;
320static pthread_attr_t _objc_thread_attribs;
323static void *thread_local_storage = NULL;
329__gthread_objc_init_thread_system (
void)
331 if (__gthread_active_p ())
334 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
339 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
340 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
341 PTHREAD_CREATE_DETACHED) == 0)
351__gthread_objc_close_thread_system (
void)
353 if (__gthread_active_p ()
354 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
355 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
364static inline objc_thread_t
365__gthread_objc_thread_detach (
void (*func)(
void *),
void *arg)
367 objc_thread_t thread_id;
368 pthread_t new_thread_handle;
370 if (!__gthread_active_p ())
373 if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
374 (
void *) func, arg)))
375 thread_id = (objc_thread_t) new_thread_handle;
384__gthread_objc_thread_set_priority (
int priority)
386 if (!__gthread_active_p ())
390#ifdef _POSIX_PRIORITY_SCHEDULING
391#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
392 pthread_t thread_id = __gthrw_(pthread_self) ();
394 struct sched_param params;
395 int priority_min, priority_max;
397 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, ¶ms) == 0)
399 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
402 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
405 if (priority > priority_max)
406 priority = priority_max;
407 else if (priority < priority_min)
408 priority = priority_min;
409 params.sched_priority = priority;
416 if (__gthrw_(pthread_setschedparam) (thread_id, policy, ¶ms) == 0)
427__gthread_objc_thread_get_priority (
void)
429#ifdef _POSIX_PRIORITY_SCHEDULING
430#ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
431 if (__gthread_active_p ())
434 struct sched_param params;
436 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, ¶ms) == 0)
437 return params.sched_priority;
444 return OBJC_THREAD_INTERACTIVE_PRIORITY;
449__gthread_objc_thread_yield (
void)
451 if (__gthread_active_p ())
452 __gthrw_(sched_yield) ();
457__gthread_objc_thread_exit (
void)
459 if (__gthread_active_p ())
461 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
468static inline objc_thread_t
469__gthread_objc_thread_id (
void)
471 if (__gthread_active_p ())
472 return (objc_thread_t) __gthrw_(pthread_self) ();
474 return (objc_thread_t) 1;
479__gthread_objc_thread_set_data (
void *value)
481 if (__gthread_active_p ())
482 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
485 thread_local_storage = value;
492__gthread_objc_thread_get_data (
void)
494 if (__gthread_active_p ())
495 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
497 return thread_local_storage;
504__gthread_objc_mutex_allocate (objc_mutex_t mutex)
506 if (__gthread_active_p ())
508 mutex->backend = objc_malloc (
sizeof (pthread_mutex_t));
510 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
512 objc_free (mutex->backend);
513 mutex->backend = NULL;
523__gthread_objc_mutex_deallocate (objc_mutex_t mutex)
525 if (__gthread_active_p ())
536 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
542 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
545 objc_free (mutex->backend);
546 mutex->backend = NULL;
553__gthread_objc_mutex_lock (objc_mutex_t mutex)
555 if (__gthread_active_p ()
556 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
566__gthread_objc_mutex_trylock (objc_mutex_t mutex)
568 if (__gthread_active_p ()
569 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
579__gthread_objc_mutex_unlock (objc_mutex_t mutex)
581 if (__gthread_active_p ()
582 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
594__gthread_objc_condition_allocate (objc_condition_t condition)
596 if (__gthread_active_p ())
598 condition->backend = objc_malloc (
sizeof (pthread_cond_t));
600 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
602 objc_free (condition->backend);
603 condition->backend = NULL;
613__gthread_objc_condition_deallocate (objc_condition_t condition)
615 if (__gthread_active_p ())
617 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
620 objc_free (condition->backend);
621 condition->backend = NULL;
628__gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
630 if (__gthread_active_p ())
631 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
632 (pthread_mutex_t *) mutex->backend);
639__gthread_objc_condition_broadcast (objc_condition_t condition)
641 if (__gthread_active_p ())
642 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
649__gthread_objc_condition_signal (objc_condition_t condition)
651 if (__gthread_active_p ())
652 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
660__gthread_create (__gthread_t *__threadid,
void *(*__func) (
void*),
663 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
667__gthread_join (__gthread_t __threadid,
void **__value_ptr)
669 return __gthrw_(pthread_join) (__threadid, __value_ptr);
673__gthread_detach (__gthread_t __threadid)
675 return __gthrw_(pthread_detach) (__threadid);
679__gthread_equal (__gthread_t __t1, __gthread_t __t2)
681 return __gthrw_(pthread_equal) (__t1, __t2);
684static inline __gthread_t
687 return __gthrw_(pthread_self) ();
691__gthread_yield (
void)
693 return __gthrw_(sched_yield) ();
697__gthread_once (__gthread_once_t *__once,
void (*__func) (
void))
699 if (__gthread_active_p ())
700 return __gthrw_(pthread_once) (__once, __func);
706__gthread_key_create (__gthread_key_t *__key,
void (*__dtor) (
void *))
708 return __gthrw_(pthread_key_create) (__key, __dtor);
712__gthread_key_delete (__gthread_key_t __key)
714 return __gthrw_(pthread_key_delete) (__key);
718__gthread_getspecific (__gthread_key_t __key)
720 return __gthrw_(pthread_getspecific) (__key);
724__gthread_setspecific (__gthread_key_t __key,
const void *__ptr)
726 return __gthrw_(pthread_setspecific) (__key, __ptr);
730__gthread_mutex_init_function (__gthread_mutex_t *__mutex)
732 if (__gthread_active_p ())
733 __gthrw_(pthread_mutex_init) (__mutex, NULL);
737__gthread_mutex_destroy (__gthread_mutex_t *__mutex)
739 if (__gthread_active_p ())
740 return __gthrw_(pthread_mutex_destroy) (__mutex);
746__gthread_mutex_lock (__gthread_mutex_t *__mutex)
748 if (__gthread_active_p ())
749 return __gthrw_(pthread_mutex_lock) (__mutex);
755__gthread_mutex_trylock (__gthread_mutex_t *__mutex)
757 if (__gthread_active_p ())
758 return __gthrw_(pthread_mutex_trylock) (__mutex);
763#if _GTHREAD_USE_MUTEX_TIMEDLOCK
765__gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
766 const __gthread_time_t *__abs_timeout)
768 if (__gthread_active_p ())
769 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
776__gthread_mutex_unlock (__gthread_mutex_t *__mutex)
778 if (__gthread_active_p ())
779 return __gthrw_(pthread_mutex_unlock) (__mutex);
784#if !defined( PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) \
785 || defined(_GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC)
787__gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
789 if (__gthread_active_p ())
791 pthread_mutexattr_t __attr;
794 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
796 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
797 PTHREAD_MUTEX_RECURSIVE);
799 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
801 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
809__gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
811 return __gthread_mutex_lock (__mutex);
815__gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
817 return __gthread_mutex_trylock (__mutex);
820#if _GTHREAD_USE_MUTEX_TIMEDLOCK
822__gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
823 const __gthread_time_t *__abs_timeout)
825 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
830__gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
832 return __gthread_mutex_unlock (__mutex);
836__gthread_recursive_mutex_destroy (__gthread_recursive_mutex_t *__mutex)
838 return __gthread_mutex_destroy (__mutex);
841#ifdef _GTHREAD_USE_COND_INIT_FUNC
843__gthread_cond_init_function (__gthread_cond_t *__cond)
845 if (__gthread_active_p ())
846 __gthrw_(pthread_cond_init) (__cond, NULL);
851__gthread_cond_broadcast (__gthread_cond_t *__cond)
853 return __gthrw_(pthread_cond_broadcast) (__cond);
857__gthread_cond_signal (__gthread_cond_t *__cond)
859 return __gthrw_(pthread_cond_signal) (__cond);
863__gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
865 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
869__gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
870 const __gthread_time_t *__abs_timeout)
872 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
876__gthread_cond_wait_recursive (__gthread_cond_t *__cond,
877 __gthread_recursive_mutex_t *__mutex)
879 return __gthread_cond_wait (__cond, __mutex);
883__gthread_cond_destroy (__gthread_cond_t* __cond)
885 return __gthrw_(pthread_cond_destroy) (__cond);