Add missing ChangeLog entry from 2012-03-09 commit.
[official-gcc.git] / libgcc / gthr-posix.h
blobb5b161184f45c24e702dd74178faa4da5f5c8c79
1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #ifndef GCC_GTHR_POSIX_H
28 #define GCC_GTHR_POSIX_H
30 /* POSIX threads specific definitions.
31 Easy, since the interface is just one-to-one mapping. */
33 #define __GTHREADS 1
34 #define __GTHREADS_CXX0X 1
36 #include <pthread.h>
38 #if ((defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)) \
39 || !defined(_GTHREAD_USE_MUTEX_TIMEDLOCK))
40 # include <unistd.h>
41 # if defined(_POSIX_TIMEOUTS) && _POSIX_TIMEOUTS >= 0
42 # define _GTHREAD_USE_MUTEX_TIMEDLOCK 1
43 # else
44 # define _GTHREAD_USE_MUTEX_TIMEDLOCK 0
45 # endif
46 #endif
48 typedef pthread_t __gthread_t;
49 typedef pthread_key_t __gthread_key_t;
50 typedef pthread_once_t __gthread_once_t;
51 typedef pthread_mutex_t __gthread_mutex_t;
52 typedef pthread_mutex_t __gthread_recursive_mutex_t;
53 typedef pthread_cond_t __gthread_cond_t;
54 typedef struct timespec __gthread_time_t;
56 /* POSIX like conditional variables are supported. Please look at comments
57 in gthr.h for details. */
58 #define __GTHREAD_HAS_COND 1
60 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
61 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
62 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
63 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
64 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
65 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
66 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
67 #else
68 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
69 #endif
70 #define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
71 #define __GTHREAD_TIME_INIT {0,0}
73 #ifdef _GTHREAD_USE_MUTEX_INIT_FUNC
74 # undef __GTHREAD_MUTEX_INIT
75 #endif
76 #ifdef _GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC
77 # undef __GTHREAD_RECURSIVE_MUTEX_INIT
78 # undef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
79 # define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
80 #endif
81 #ifdef _GTHREAD_USE_COND_INIT_FUNC
82 # undef __GTHREAD_COND_INIT
83 # define __GTHREAD_COND_INIT_FUNCTION __gthread_cond_init_function
84 #endif
86 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
87 # ifndef __gthrw_pragma
88 # define __gthrw_pragma(pragma)
89 # endif
90 # define __gthrw2(name,name2,type) \
91 static __typeof(type) name __attribute__ ((__weakref__(#name2))); \
92 __gthrw_pragma(weak type)
93 # define __gthrw_(name) __gthrw_ ## name
94 #else
95 # define __gthrw2(name,name2,type)
96 # define __gthrw_(name) name
97 #endif
99 /* Typically, __gthrw_foo is a weak reference to symbol foo. */
100 #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
102 __gthrw(pthread_once)
103 __gthrw(pthread_getspecific)
104 __gthrw(pthread_setspecific)
106 __gthrw(pthread_create)
107 __gthrw(pthread_join)
108 __gthrw(pthread_equal)
109 __gthrw(pthread_self)
110 __gthrw(pthread_detach)
111 #ifndef __BIONIC__
112 __gthrw(pthread_cancel)
113 #endif
114 __gthrw(sched_yield)
116 __gthrw(pthread_mutex_lock)
117 __gthrw(pthread_mutex_trylock)
118 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
119 __gthrw(pthread_mutex_timedlock)
120 #endif
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)
140 /* Objective-C. */
141 __gthrw(pthread_exit)
142 #ifdef _POSIX_PRIORITY_SCHEDULING
143 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
144 __gthrw(sched_get_priority_max)
145 __gthrw(sched_get_priority_min)
146 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
147 #endif /* _POSIX_PRIORITY_SCHEDULING */
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)
154 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
155 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
157 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
159 /* On Solaris 2.6 up to 9, the libc exposes a POSIX threads interface even if
160 -pthreads is not specified. The functions are dummies and most return an
161 error value. However pthread_once returns 0 without invoking the routine
162 it is passed so we cannot pretend that the interface is active if -pthreads
163 is not specified. On Solaris 2.5.1, the interface is not exposed at all so
164 we need to play the usual game with weak symbols. On Solaris 10 and up, a
165 working interface is always exposed. On FreeBSD 6 and later, libc also
166 exposes a dummy POSIX threads interface, similar to what Solaris 2.6 up
167 to 9 does. FreeBSD >= 700014 even provides a pthread_cancel stub in libc,
168 which means the alternate __gthread_active_p below cannot be used there. */
170 #if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
172 static volatile int __gthread_active = -1;
174 static void
175 __gthread_trigger (void)
177 __gthread_active = 1;
180 static inline int
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;
186 /* Avoid reading __gthread_active twice on the main code path. */
187 int __gthread_active_latest_value = __gthread_active;
189 /* This test is not protected to avoid taking a lock on the main code
190 path so every update of __gthread_active in a threaded program must
191 be atomic with regard to the result of the test. */
192 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
194 if (__gthrw_(pthread_once))
196 /* If this really is a threaded program, then we must ensure that
197 __gthread_active has been set to 1 before exiting this block. */
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);
203 /* Make sure we'll never enter this block again. */
204 if (__gthread_active < 0)
205 __gthread_active = 0;
207 __gthread_active_latest_value = __gthread_active;
210 return __gthread_active_latest_value != 0;
213 #else /* neither FreeBSD nor Solaris */
215 static inline int
216 __gthread_active_p (void)
218 /* Android's C library does not provide pthread_cancel, check for
219 `pthread_create' instead. */
220 #ifndef __BIONIC__
221 static void *const __gthread_active_ptr
222 = __extension__ (void *) &__gthrw_(pthread_cancel);
223 #else
224 static void *const __gthread_active_ptr
225 = __extension__ (void *) &__gthrw_(pthread_create);
226 #endif
227 return __gthread_active_ptr != 0;
230 #endif /* FreeBSD or Solaris */
232 #else /* not SUPPORTS_WEAK */
234 /* Similar to Solaris, HP-UX 11 for PA-RISC provides stubs for pthread
235 calls in shared flavors of the HP-UX C library. Most of the stubs
236 have no functionality. The details are described in the "libc cumulative
237 patch" for each subversion of HP-UX 11. There are two special interfaces
238 provided for checking whether an application is linked to a shared pthread
239 library or not. However, these interfaces aren't available in early
240 libpthread libraries. We also need a test that works for archive
241 libraries. We can't use pthread_once as some libc versions call the
242 init function. We also can't use pthread_create or pthread_attr_init
243 as these create a thread and thereby prevent changing the default stack
244 size. The function pthread_default_stacksize_np is available in both
245 the archive and shared versions of libpthread. It can be used to
246 determine the default pthread stack size. There is a stub in some
247 shared libc versions which returns a zero size if pthreads are not
248 active. We provide an equivalent stub to handle cases where libc
249 doesn't provide one. */
251 #if defined(__hppa__) && defined(__hpux__)
253 static volatile int __gthread_active = -1;
255 static inline int
256 __gthread_active_p (void)
258 /* Avoid reading __gthread_active twice on the main code path. */
259 int __gthread_active_latest_value = __gthread_active;
260 size_t __s;
262 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
264 pthread_default_stacksize_np (0, &__s);
265 __gthread_active = __s ? 1 : 0;
266 __gthread_active_latest_value = __gthread_active;
269 return __gthread_active_latest_value != 0;
272 #else /* not hppa-hpux */
274 static inline int
275 __gthread_active_p (void)
277 return 1;
280 #endif /* hppa-hpux */
282 #endif /* SUPPORTS_WEAK */
284 #ifdef _LIBOBJC
286 /* This is the config.h file in libobjc/ */
287 #include <config.h>
289 #ifdef HAVE_SCHED_H
290 # include <sched.h>
291 #endif
293 /* Key structure for maintaining thread specific storage */
294 static pthread_key_t _objc_thread_storage;
295 static pthread_attr_t _objc_thread_attribs;
297 /* Thread local storage for a single thread */
298 static void *thread_local_storage = NULL;
300 /* Backend initialization functions */
302 /* Initialize the threads subsystem. */
303 static inline int
304 __gthread_objc_init_thread_system (void)
306 if (__gthread_active_p ())
308 /* Initialize the thread storage key. */
309 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
311 /* The normal default detach state for threads is
312 * PTHREAD_CREATE_JOINABLE which causes threads to not die
313 * when you think they should. */
314 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
315 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
316 PTHREAD_CREATE_DETACHED) == 0)
317 return 0;
321 return -1;
324 /* Close the threads subsystem. */
325 static inline int
326 __gthread_objc_close_thread_system (void)
328 if (__gthread_active_p ()
329 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
330 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
331 return 0;
333 return -1;
336 /* Backend thread functions */
338 /* Create a new thread of execution. */
339 static inline objc_thread_t
340 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
342 objc_thread_t thread_id;
343 pthread_t new_thread_handle;
345 if (!__gthread_active_p ())
346 return NULL;
348 if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
349 (void *) func, arg)))
350 thread_id = (objc_thread_t) new_thread_handle;
351 else
352 thread_id = NULL;
354 return thread_id;
357 /* Set the current thread's priority. */
358 static inline int
359 __gthread_objc_thread_set_priority (int priority)
361 if (!__gthread_active_p ())
362 return -1;
363 else
365 #ifdef _POSIX_PRIORITY_SCHEDULING
366 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
367 pthread_t thread_id = __gthrw_(pthread_self) ();
368 int policy;
369 struct sched_param params;
370 int priority_min, priority_max;
372 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, &params) == 0)
374 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
375 return -1;
377 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
378 return -1;
380 if (priority > priority_max)
381 priority = priority_max;
382 else if (priority < priority_min)
383 priority = priority_min;
384 params.sched_priority = priority;
387 * The solaris 7 and several other man pages incorrectly state that
388 * this should be a pointer to policy but pthread.h is universally
389 * at odds with this.
391 if (__gthrw_(pthread_setschedparam) (thread_id, policy, &params) == 0)
392 return 0;
394 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
395 #endif /* _POSIX_PRIORITY_SCHEDULING */
396 return -1;
400 /* Return the current thread's priority. */
401 static inline int
402 __gthread_objc_thread_get_priority (void)
404 #ifdef _POSIX_PRIORITY_SCHEDULING
405 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
406 if (__gthread_active_p ())
408 int policy;
409 struct sched_param params;
411 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, &params) == 0)
412 return params.sched_priority;
413 else
414 return -1;
416 else
417 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
418 #endif /* _POSIX_PRIORITY_SCHEDULING */
419 return OBJC_THREAD_INTERACTIVE_PRIORITY;
422 /* Yield our process time to another thread. */
423 static inline void
424 __gthread_objc_thread_yield (void)
426 if (__gthread_active_p ())
427 __gthrw_(sched_yield) ();
430 /* Terminate the current thread. */
431 static inline int
432 __gthread_objc_thread_exit (void)
434 if (__gthread_active_p ())
435 /* exit the thread */
436 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
438 /* Failed if we reached here */
439 return -1;
442 /* Returns an integer value which uniquely describes a thread. */
443 static inline objc_thread_t
444 __gthread_objc_thread_id (void)
446 if (__gthread_active_p ())
447 return (objc_thread_t) __gthrw_(pthread_self) ();
448 else
449 return (objc_thread_t) 1;
452 /* Sets the thread's local storage pointer. */
453 static inline int
454 __gthread_objc_thread_set_data (void *value)
456 if (__gthread_active_p ())
457 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
458 else
460 thread_local_storage = value;
461 return 0;
465 /* Returns the thread's local storage pointer. */
466 static inline void *
467 __gthread_objc_thread_get_data (void)
469 if (__gthread_active_p ())
470 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
471 else
472 return thread_local_storage;
475 /* Backend mutex functions */
477 /* Allocate a mutex. */
478 static inline int
479 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
481 if (__gthread_active_p ())
483 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
485 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
487 objc_free (mutex->backend);
488 mutex->backend = NULL;
489 return -1;
493 return 0;
496 /* Deallocate a mutex. */
497 static inline int
498 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
500 if (__gthread_active_p ())
502 int count;
505 * Posix Threads specifically require that the thread be unlocked
506 * for __gthrw_(pthread_mutex_destroy) to work.
511 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
512 if (count < 0)
513 return -1;
515 while (count);
517 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
518 return -1;
520 objc_free (mutex->backend);
521 mutex->backend = NULL;
523 return 0;
526 /* Grab a lock on a mutex. */
527 static inline int
528 __gthread_objc_mutex_lock (objc_mutex_t mutex)
530 if (__gthread_active_p ()
531 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
533 return -1;
536 return 0;
539 /* Try to grab a lock on a mutex. */
540 static inline int
541 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
543 if (__gthread_active_p ()
544 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
546 return -1;
549 return 0;
552 /* Unlock the mutex */
553 static inline int
554 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
556 if (__gthread_active_p ()
557 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
559 return -1;
562 return 0;
565 /* Backend condition mutex functions */
567 /* Allocate a condition. */
568 static inline int
569 __gthread_objc_condition_allocate (objc_condition_t condition)
571 if (__gthread_active_p ())
573 condition->backend = objc_malloc (sizeof (pthread_cond_t));
575 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
577 objc_free (condition->backend);
578 condition->backend = NULL;
579 return -1;
583 return 0;
586 /* Deallocate a condition. */
587 static inline int
588 __gthread_objc_condition_deallocate (objc_condition_t condition)
590 if (__gthread_active_p ())
592 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
593 return -1;
595 objc_free (condition->backend);
596 condition->backend = NULL;
598 return 0;
601 /* Wait on the condition */
602 static inline int
603 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
605 if (__gthread_active_p ())
606 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
607 (pthread_mutex_t *) mutex->backend);
608 else
609 return 0;
612 /* Wake up all threads waiting on this condition. */
613 static inline int
614 __gthread_objc_condition_broadcast (objc_condition_t condition)
616 if (__gthread_active_p ())
617 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
618 else
619 return 0;
622 /* Wake up one thread waiting on this condition. */
623 static inline int
624 __gthread_objc_condition_signal (objc_condition_t condition)
626 if (__gthread_active_p ())
627 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
628 else
629 return 0;
632 #else /* _LIBOBJC */
634 static inline int
635 __gthread_create (__gthread_t *__threadid, void *(*__func) (void*),
636 void *__args)
638 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
641 static inline int
642 __gthread_join (__gthread_t __threadid, void **__value_ptr)
644 return __gthrw_(pthread_join) (__threadid, __value_ptr);
647 static inline int
648 __gthread_detach (__gthread_t __threadid)
650 return __gthrw_(pthread_detach) (__threadid);
653 static inline int
654 __gthread_equal (__gthread_t __t1, __gthread_t __t2)
656 return __gthrw_(pthread_equal) (__t1, __t2);
659 static inline __gthread_t
660 __gthread_self (void)
662 return __gthrw_(pthread_self) ();
665 static inline int
666 __gthread_yield (void)
668 return __gthrw_(sched_yield) ();
671 static inline int
672 __gthread_once (__gthread_once_t *__once, void (*__func) (void))
674 if (__gthread_active_p ())
675 return __gthrw_(pthread_once) (__once, __func);
676 else
677 return -1;
680 static inline int
681 __gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
683 return __gthrw_(pthread_key_create) (__key, __dtor);
686 static inline int
687 __gthread_key_delete (__gthread_key_t __key)
689 return __gthrw_(pthread_key_delete) (__key);
692 static inline void *
693 __gthread_getspecific (__gthread_key_t __key)
695 return __gthrw_(pthread_getspecific) (__key);
698 static inline int
699 __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
701 return __gthrw_(pthread_setspecific) (__key, __ptr);
704 static inline void
705 __gthread_mutex_init_function (__gthread_mutex_t *__mutex)
707 if (__gthread_active_p ())
708 __gthrw_(pthread_mutex_init) (__mutex, NULL);
711 static inline int
712 __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
714 if (__gthread_active_p ())
715 return __gthrw_(pthread_mutex_destroy) (__mutex);
716 else
717 return 0;
720 static inline int
721 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
723 if (__gthread_active_p ())
724 return __gthrw_(pthread_mutex_lock) (__mutex);
725 else
726 return 0;
729 static inline int
730 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
732 if (__gthread_active_p ())
733 return __gthrw_(pthread_mutex_trylock) (__mutex);
734 else
735 return 0;
738 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
739 static inline int
740 __gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
741 const __gthread_time_t *__abs_timeout)
743 if (__gthread_active_p ())
744 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
745 else
746 return 0;
748 #endif
750 static inline int
751 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
753 if (__gthread_active_p ())
754 return __gthrw_(pthread_mutex_unlock) (__mutex);
755 else
756 return 0;
759 #if !defined( PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) \
760 || defined(_GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC)
761 static inline int
762 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
764 if (__gthread_active_p ())
766 pthread_mutexattr_t __attr;
767 int __r;
769 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
770 if (!__r)
771 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
772 PTHREAD_MUTEX_RECURSIVE);
773 if (!__r)
774 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
775 if (!__r)
776 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
777 return __r;
779 return 0;
781 #endif
783 static inline int
784 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
786 return __gthread_mutex_lock (__mutex);
789 static inline int
790 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
792 return __gthread_mutex_trylock (__mutex);
795 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
796 static inline int
797 __gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
798 const __gthread_time_t *__abs_timeout)
800 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
802 #endif
804 static inline int
805 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
807 return __gthread_mutex_unlock (__mutex);
810 #ifdef _GTHREAD_USE_COND_INIT_FUNC
811 static inline void
812 __gthread_cond_init_function (__gthread_cond_t *__cond)
814 if (__gthread_active_p ())
815 __gthrw_(pthread_cond_init) (__cond, NULL);
817 #endif
819 static inline int
820 __gthread_cond_broadcast (__gthread_cond_t *__cond)
822 return __gthrw_(pthread_cond_broadcast) (__cond);
825 static inline int
826 __gthread_cond_signal (__gthread_cond_t *__cond)
828 return __gthrw_(pthread_cond_signal) (__cond);
831 static inline int
832 __gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
834 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
837 static inline int
838 __gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
839 const __gthread_time_t *__abs_timeout)
841 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
844 static inline int
845 __gthread_cond_wait_recursive (__gthread_cond_t *__cond,
846 __gthread_recursive_mutex_t *__mutex)
848 return __gthread_cond_wait (__cond, __mutex);
851 static inline int
852 __gthread_cond_timedwait_recursive (__gthread_cond_t *__cond,
853 __gthread_recursive_mutex_t *__mutex,
854 const __gthread_time_t *__abs_timeout)
856 return __gthread_cond_timedwait (__cond, __mutex, __abs_timeout);
859 static inline int
860 __gthread_cond_destroy (__gthread_cond_t* __cond)
862 return __gthrw_(pthread_cond_destroy) (__cond);
865 #endif /* _LIBOBJC */
867 #endif /* ! GCC_GTHR_POSIX_H */