27 #ifndef _GLIBCXX_GCC_GTHR_POSIX_H
28 #define _GLIBCXX_GCC_GTHR_POSIX_H
34 #define __GTHREADS_CXX0X 1
37 #if !defined(_REENTRANT) && defined(__osf__)
50 typedef struct timespec __gthread_time_t;
54 #define __GTHREAD_HAS_COND 1
56 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
57 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
58 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
59 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
60 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
61 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
63 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
65 #define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
66 #define __GTHREAD_TIME_INIT {0,0}
68 #if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
69 # ifndef __gthrw_pragma
70 # define __gthrw_pragma(pragma)
72 # define __gthrw2(name,name2,type) \
73 static __typeof(type) name __attribute__ ((__weakref__(#name2))); \
74 __gthrw_pragma(weak type)
75 # define __gthrw_(name) __gthrw_ ## name
77 # define __gthrw2(name,name2,type)
78 # define __gthrw_(name) name
82 #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
87 #if defined(__osf__) && defined(_PTHREAD_USE_MANGLED_NAMES_)
88 #define __gthrw3(name) __gthrw2(__gthrw_ ## name, __ ## name, name)
89 __gthrw3(pthread_once)
90 __gthrw3(pthread_getspecific)
91 __gthrw3(pthread_setspecific)
93 __gthrw3(pthread_create)
94 __gthrw3(pthread_join)
95 __gthrw3(pthread_detach)
96 __gthrw3(pthread_equal)
97 __gthrw3(pthread_self)
98 __gthrw3(pthread_cancel)
101 __gthrw3(pthread_mutex_lock)
102 __gthrw3(pthread_mutex_trylock)
103 #ifdef _POSIX_TIMEOUTS
104 #if _POSIX_TIMEOUTS >= 0
105 __gthrw3(pthread_mutex_timedlock)
108 __gthrw3(pthread_mutex_unlock)
109 __gthrw3(pthread_mutex_init)
110 __gthrw3(pthread_mutex_destroy)
112 __gthrw3(pthread_cond_broadcast)
113 __gthrw3(pthread_cond_signal)
114 __gthrw3(pthread_cond_wait)
115 __gthrw3(pthread_cond_timedwait)
116 __gthrw3(pthread_cond_destroy)
118 __gthrw(pthread_once)
119 __gthrw(pthread_getspecific)
120 __gthrw(pthread_setspecific)
122 __gthrw(pthread_create)
123 __gthrw(pthread_join)
124 __gthrw(pthread_equal)
125 __gthrw(pthread_self)
126 __gthrw(pthread_detach)
128 __gthrw(pthread_cancel)
132 __gthrw(pthread_mutex_lock)
133 __gthrw(pthread_mutex_trylock)
134 #ifdef _POSIX_TIMEOUTS
135 #if _POSIX_TIMEOUTS >= 0
136 __gthrw(pthread_mutex_timedlock)
139 __gthrw(pthread_mutex_unlock)
140 __gthrw(pthread_mutex_init)
141 __gthrw(pthread_mutex_destroy)
143 __gthrw(pthread_cond_broadcast)
144 __gthrw(pthread_cond_signal)
145 __gthrw(pthread_cond_wait)
146 __gthrw(pthread_cond_timedwait)
147 __gthrw(pthread_cond_destroy)
150 __gthrw(pthread_key_create)
151 __gthrw(pthread_key_delete)
152 __gthrw(pthread_mutexattr_init)
153 __gthrw(pthread_mutexattr_settype)
154 __gthrw(pthread_mutexattr_destroy)
157 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
159 #if defined(__osf__) && defined(_PTHREAD_USE_MANGLED_NAMES_)
160 __gthrw3(pthread_cond_init)
161 __gthrw3(pthread_exit)
163 __gthrw(pthread_cond_init)
164 __gthrw(pthread_exit)
166 #ifdef _POSIX_PRIORITY_SCHEDULING
167 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
168 __gthrw(sched_get_priority_max)
169 __gthrw(sched_get_priority_min)
172 __gthrw(pthread_attr_destroy)
173 __gthrw(pthread_attr_init)
174 __gthrw(pthread_attr_setdetachstate)
175 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
176 __gthrw(pthread_getschedparam)
177 __gthrw(pthread_setschedparam)
181 #if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
194 #if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
196 static volatile int __gthread_active = -1;
199 __gthread_trigger (
void)
201 __gthread_active = 1;
205 __gthread_active_p (
void)
207 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
211 int __gthread_active_latest_value = __gthread_active;
216 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
218 if (__gthrw_(pthread_once))
222 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
223 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
224 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
228 if (__gthread_active < 0)
229 __gthread_active = 0;
231 __gthread_active_latest_value = __gthread_active;
234 return __gthread_active_latest_value != 0;
240 __gthread_active_p (
void)
242 static void *
const __gthread_active_ptr
243 = __extension__ (
void *) &__gthrw_(
252 return __gthread_active_ptr != 0;
276 #if defined(__hppa__) && defined(__hpux__)
278 static 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)
323 static 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)
364 static inline objc_thread_t
365 __gthread_objc_thread_detach (
void (*func)(
void *),
void *arg)
367 objc_thread_t thread_id;
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);
468 static 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 ())
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 ())
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 ())
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,
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);
675 return __gthrw_(pthread_detach) (__threadid);
681 return __gthrw_(pthread_equal) (__t1, __t2);
685 __gthread_self (
void)
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_destroy (__gthread_mutex_t *__mutex)
732 if (__gthread_active_p ())
733 return __gthrw_(pthread_mutex_destroy) (__mutex);
739 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
741 if (__gthread_active_p ())
742 return __gthrw_(pthread_mutex_lock) (__mutex);
748 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
750 if (__gthread_active_p ())
751 return __gthrw_(pthread_mutex_trylock) (__mutex);
756 #ifdef _POSIX_TIMEOUTS
757 #if _POSIX_TIMEOUTS >= 0
759 __gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
760 const __gthread_time_t *__abs_timeout)
762 if (__gthread_active_p ())
763 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
771 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
773 if (__gthread_active_p ())
774 return __gthrw_(pthread_mutex_unlock) (__mutex);
779 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
781 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
783 if (__gthread_active_p ())
788 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
790 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
791 PTHREAD_MUTEX_RECURSIVE);
793 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
795 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
803 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
805 return __gthread_mutex_lock (__mutex);
809 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
811 return __gthread_mutex_trylock (__mutex);
814 #ifdef _POSIX_TIMEOUTS
815 #if _POSIX_TIMEOUTS >= 0
817 __gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
818 const __gthread_time_t *__abs_timeout)
820 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
826 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
828 return __gthread_mutex_unlock (__mutex);
834 return __gthrw_(pthread_cond_broadcast) (__cond);
840 return __gthrw_(pthread_cond_signal) (__cond);
846 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
850 __gthread_cond_timedwait (
__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
851 const __gthread_time_t *__abs_timeout)
853 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
858 __gthread_recursive_mutex_t *__mutex)
860 return __gthread_cond_wait (__cond, __mutex);
865 __gthread_recursive_mutex_t *__mutex,
866 const __gthread_time_t *__abs_timeout)
868 return __gthread_cond_timedwait (__cond, __mutex, __abs_timeout);
874 return __gthrw_(pthread_cond_destroy) (__cond);