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__)
43 #if ((defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)) \
44 || !defined(_GTHREAD_USE_MUTEX_TIMEDLOCK))
46 # if defined(_POSIX_TIMEOUTS) && _POSIX_TIMEOUTS >= 0
47 # define _GTHREAD_USE_MUTEX_TIMEDLOCK 1
49 # define _GTHREAD_USE_MUTEX_TIMEDLOCK 0
53 typedef pthread_t __gthread_t;
54 typedef pthread_key_t __gthread_key_t;
55 typedef pthread_once_t __gthread_once_t;
56 typedef pthread_mutex_t __gthread_mutex_t;
57 typedef pthread_mutex_t __gthread_recursive_mutex_t;
58 typedef pthread_cond_t __gthread_cond_t;
59 typedef struct timespec __gthread_time_t;
63 #define __GTHREAD_HAS_COND 1
65 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
66 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
67 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
68 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
69 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
70 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
72 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
74 #define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
75 #define __GTHREAD_TIME_INIT {0,0}
77 #ifdef _GTHREAD_USE_MUTEX_INIT_FUNC
78 # undef __GTHREAD_MUTEX_INIT
79 # define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
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 __attribute__ ((__weakref__(#name2))); \
97 __gthrw_pragma(weak type)
98 # define __gthrw_(name) __gthrw_ ## name
100 # define __gthrw2(name,name2,type)
101 # define __gthrw_(name) name
105 #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
110 #if defined(__osf__) && defined(_PTHREAD_USE_MANGLED_NAMES_)
111 #define __gthrw3(name) __gthrw2(__gthrw_ ## name, __ ## name, name)
112 __gthrw3(pthread_once)
113 __gthrw3(pthread_getspecific)
114 __gthrw3(pthread_setspecific)
116 __gthrw3(pthread_create)
117 __gthrw3(pthread_join)
118 __gthrw3(pthread_detach)
119 __gthrw3(pthread_equal)
120 __gthrw3(pthread_self)
121 __gthrw3(pthread_cancel)
122 __gthrw3(sched_yield)
124 __gthrw3(pthread_mutex_lock)
125 __gthrw3(pthread_mutex_trylock)
126 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
127 __gthrw3(pthread_mutex_timedlock)
129 __gthrw3(pthread_mutex_unlock)
130 __gthrw3(pthread_mutex_init)
131 __gthrw3(pthread_mutex_destroy)
133 __gthrw3(pthread_cond_init)
134 __gthrw3(pthread_cond_broadcast)
135 __gthrw3(pthread_cond_signal)
136 __gthrw3(pthread_cond_wait)
137 __gthrw3(pthread_cond_timedwait)
138 __gthrw3(pthread_cond_destroy)
140 __gthrw(pthread_once)
141 __gthrw(pthread_getspecific)
142 __gthrw(pthread_setspecific)
144 __gthrw(pthread_create)
145 __gthrw(pthread_join)
146 __gthrw(pthread_equal)
147 __gthrw(pthread_self)
148 __gthrw(pthread_detach)
150 __gthrw(pthread_cancel)
154 __gthrw(pthread_mutex_lock)
155 __gthrw(pthread_mutex_trylock)
156 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
157 __gthrw(pthread_mutex_timedlock)
159 __gthrw(pthread_mutex_unlock)
160 __gthrw(pthread_mutex_init)
161 __gthrw(pthread_mutex_destroy)
163 __gthrw(pthread_cond_init)
164 __gthrw(pthread_cond_broadcast)
165 __gthrw(pthread_cond_signal)
166 __gthrw(pthread_cond_wait)
167 __gthrw(pthread_cond_timedwait)
168 __gthrw(pthread_cond_destroy)
171 __gthrw(pthread_key_create)
172 __gthrw(pthread_key_delete)
173 __gthrw(pthread_mutexattr_init)
174 __gthrw(pthread_mutexattr_settype)
175 __gthrw(pthread_mutexattr_destroy)
178 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
180 #if defined(__osf__) && defined(_PTHREAD_USE_MANGLED_NAMES_)
181 __gthrw3(pthread_exit)
183 __gthrw(pthread_exit)
185 #ifdef _POSIX_PRIORITY_SCHEDULING
186 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
187 __gthrw(sched_get_priority_max)
188 __gthrw(sched_get_priority_min)
191 __gthrw(pthread_attr_destroy)
192 __gthrw(pthread_attr_init)
193 __gthrw(pthread_attr_setdetachstate)
194 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
195 __gthrw(pthread_getschedparam)
196 __gthrw(pthread_setschedparam)
200 #if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
213 #if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
215 static volatile int __gthread_active = -1;
218 __gthread_trigger (
void)
220 __gthread_active = 1;
224 __gthread_active_p (
void)
226 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
227 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
230 int __gthread_active_latest_value = __gthread_active;
235 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
237 if (__gthrw_(pthread_once))
241 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
242 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
243 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
247 if (__gthread_active < 0)
248 __gthread_active = 0;
250 __gthread_active_latest_value = __gthread_active;
253 return __gthread_active_latest_value != 0;
259 __gthread_active_p (
void)
264 static void *
const __gthread_active_ptr
265 = __extension__ (
void *) &__gthrw_(pthread_cancel);
267 static void *
const __gthread_active_ptr
268 = __extension__ (
void *) &__gthrw_(pthread_create);
270 return __gthread_active_ptr != 0;
294 #if defined(__hppa__) && defined(__hpux__)
296 static volatile int __gthread_active = -1;
299 __gthread_active_p (
void)
302 int __gthread_active_latest_value = __gthread_active;
305 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
307 pthread_default_stacksize_np (0, &__s);
308 __gthread_active = __s ? 1 : 0;
309 __gthread_active_latest_value = __gthread_active;
312 return __gthread_active_latest_value != 0;
318 __gthread_active_p (
void)
337 static pthread_key_t _objc_thread_storage;
338 static pthread_attr_t _objc_thread_attribs;
341 static void *thread_local_storage = NULL;
347 __gthread_objc_init_thread_system (
void)
349 if (__gthread_active_p ())
352 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
357 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
358 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
359 PTHREAD_CREATE_DETACHED) == 0)
369 __gthread_objc_close_thread_system (
void)
371 if (__gthread_active_p ()
372 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
373 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
382 static inline objc_thread_t
383 __gthread_objc_thread_detach (
void (*func)(
void *),
void *
arg)
385 objc_thread_t thread_id;
386 pthread_t new_thread_handle;
388 if (!__gthread_active_p ())
391 if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
392 (
void *) func, arg)))
393 thread_id = (objc_thread_t) new_thread_handle;
402 __gthread_objc_thread_set_priority (
int priority)
404 if (!__gthread_active_p ())
408 #ifdef _POSIX_PRIORITY_SCHEDULING
409 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
410 pthread_t thread_id = __gthrw_(pthread_self) ();
412 struct sched_param params;
413 int priority_min, priority_max;
415 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, ¶ms) == 0)
417 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
420 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
423 if (priority > priority_max)
424 priority = priority_max;
425 else if (priority < priority_min)
426 priority = priority_min;
427 params.sched_priority = priority;
434 if (__gthrw_(pthread_setschedparam) (thread_id, policy, ¶ms) == 0)
445 __gthread_objc_thread_get_priority (
void)
447 #ifdef _POSIX_PRIORITY_SCHEDULING
448 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
449 if (__gthread_active_p ())
452 struct sched_param params;
454 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, ¶ms) == 0)
455 return params.sched_priority;
462 return OBJC_THREAD_INTERACTIVE_PRIORITY;
467 __gthread_objc_thread_yield (
void)
469 if (__gthread_active_p ())
470 __gthrw_(sched_yield) ();
475 __gthread_objc_thread_exit (
void)
477 if (__gthread_active_p ())
479 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
486 static inline objc_thread_t
487 __gthread_objc_thread_id (
void)
489 if (__gthread_active_p ())
490 return (objc_thread_t) __gthrw_(pthread_self) ();
492 return (objc_thread_t) 1;
497 __gthread_objc_thread_set_data (
void *value)
499 if (__gthread_active_p ())
500 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
503 thread_local_storage = value;
510 __gthread_objc_thread_get_data (
void)
512 if (__gthread_active_p ())
513 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
515 return thread_local_storage;
522 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
524 if (__gthread_active_p ())
526 mutex->backend = objc_malloc (
sizeof (pthread_mutex_t));
528 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
530 objc_free (mutex->backend);
531 mutex->backend = NULL;
541 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
543 if (__gthread_active_p ())
554 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
560 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
563 objc_free (mutex->backend);
564 mutex->backend = NULL;
571 __gthread_objc_mutex_lock (objc_mutex_t mutex)
573 if (__gthread_active_p ()
574 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
584 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
586 if (__gthread_active_p ()
587 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
597 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
599 if (__gthread_active_p ()
600 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
612 __gthread_objc_condition_allocate (objc_condition_t condition)
614 if (__gthread_active_p ())
616 condition->backend = objc_malloc (
sizeof (pthread_cond_t));
618 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
620 objc_free (condition->backend);
621 condition->backend = NULL;
631 __gthread_objc_condition_deallocate (objc_condition_t condition)
633 if (__gthread_active_p ())
635 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
638 objc_free (condition->backend);
639 condition->backend = NULL;
646 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
648 if (__gthread_active_p ())
649 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
650 (pthread_mutex_t *) mutex->backend);
657 __gthread_objc_condition_broadcast (objc_condition_t condition)
659 if (__gthread_active_p ())
660 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
667 __gthread_objc_condition_signal (objc_condition_t condition)
669 if (__gthread_active_p ())
670 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
678 __gthread_create (__gthread_t *__threadid,
void *(*__func) (
void*),
681 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
685 __gthread_join (__gthread_t __threadid,
void **__value_ptr)
687 return __gthrw_(pthread_join) (__threadid, __value_ptr);
691 __gthread_detach (__gthread_t __threadid)
693 return __gthrw_(pthread_detach) (__threadid);
697 __gthread_equal (__gthread_t __t1, __gthread_t __t2)
699 return __gthrw_(pthread_equal) (__t1, __t2);
702 static inline __gthread_t
703 __gthread_self (
void)
705 return __gthrw_(pthread_self) ();
709 __gthread_yield (
void)
711 return __gthrw_(sched_yield) ();
715 __gthread_once (__gthread_once_t *__once,
void (*__func) (
void))
717 if (__gthread_active_p ())
718 return __gthrw_(pthread_once) (__once, __func);
724 __gthread_key_create (__gthread_key_t *__key,
void (*__dtor) (
void *))
726 return __gthrw_(pthread_key_create) (__key, __dtor);
730 __gthread_key_delete (__gthread_key_t __key)
732 return __gthrw_(pthread_key_delete) (__key);
736 __gthread_getspecific (__gthread_key_t __key)
738 return __gthrw_(pthread_getspecific) (__key);
742 __gthread_setspecific (__gthread_key_t __key,
const void *__ptr)
744 return __gthrw_(pthread_setspecific) (__key, __ptr);
747 #ifdef _GTHREAD_USE_MUTEX_INIT_FUNC
749 __gthread_mutex_init_function (__gthread_mutex_t *__mutex)
751 if (__gthread_active_p ())
752 __gthrw_(pthread_mutex_init) (__mutex, NULL);
757 __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
759 if (__gthread_active_p ())
760 return __gthrw_(pthread_mutex_destroy) (__mutex);
766 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
768 if (__gthread_active_p ())
769 return __gthrw_(pthread_mutex_lock) (__mutex);
775 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
777 if (__gthread_active_p ())
778 return __gthrw_(pthread_mutex_trylock) (__mutex);
783 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
785 __gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
786 const __gthread_time_t *__abs_timeout)
788 if (__gthread_active_p ())
789 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
796 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
798 if (__gthread_active_p ())
799 return __gthrw_(pthread_mutex_unlock) (__mutex);
804 #if !defined( PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) \
805 || defined(_GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC)
807 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
809 if (__gthread_active_p ())
811 pthread_mutexattr_t __attr;
814 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
816 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
817 PTHREAD_MUTEX_RECURSIVE);
819 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
821 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
829 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
831 return __gthread_mutex_lock (__mutex);
835 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
837 return __gthread_mutex_trylock (__mutex);
840 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
842 __gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
843 const __gthread_time_t *__abs_timeout)
845 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
850 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
852 return __gthread_mutex_unlock (__mutex);
855 #ifdef _GTHREAD_USE_COND_INIT_FUNC
857 __gthread_cond_init_function (__gthread_cond_t *__cond)
859 if (__gthread_active_p ())
860 __gthrw_(pthread_cond_init) (__cond, NULL);
865 __gthread_cond_broadcast (__gthread_cond_t *__cond)
867 return __gthrw_(pthread_cond_broadcast) (__cond);
871 __gthread_cond_signal (__gthread_cond_t *__cond)
873 return __gthrw_(pthread_cond_signal) (__cond);
877 __gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
879 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
883 __gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
884 const __gthread_time_t *__abs_timeout)
886 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
890 __gthread_cond_wait_recursive (__gthread_cond_t *__cond,
891 __gthread_recursive_mutex_t *__mutex)
893 return __gthread_cond_wait (__cond, __mutex);
897 __gthread_cond_timedwait_recursive (__gthread_cond_t *__cond,
898 __gthread_recursive_mutex_t *__mutex,
899 const __gthread_time_t *__abs_timeout)
901 return __gthread_cond_timedwait (__cond, __mutex, __abs_timeout);
905 __gthread_cond_destroy (__gthread_cond_t* __cond)
907 return __gthrw_(pthread_cond_destroy) (__cond);
_Tp arg(const complex< _Tp > &)
Return phase angle of z.
size_t count() const _GLIBCXX_NOEXCEPT
Returns the number of bits which are set.