2011-08-19 Andrew Stubbs <ams@codesourcery.com>
[official-gcc.git] / gcc / gthr-posix.h
blobb1d499dcc3db555fd3c9ad7683fdd84d4c4d23a2
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 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 /* Some implementations of <pthread.h> require this to be defined. */
37 #if !defined(_REENTRANT) && defined(__osf__)
38 #define _REENTRANT 1
39 #endif
41 #include <pthread.h>
43 #if ((defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)) \
44 || !defined(_GTHREAD_USE_MUTEX_TIMEDLOCK))
45 # include <unistd.h>
46 # if defined(_POSIX_TIMEOUTS) && _POSIX_TIMEOUTS >= 0
47 # define _GTHREAD_USE_MUTEX_TIMEDLOCK 1
48 # else
49 # define _GTHREAD_USE_MUTEX_TIMEDLOCK 0
50 # endif
51 #endif
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;
61 /* POSIX like conditional variables are supported. Please look at comments
62 in gthr.h for details. */
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
71 #else
72 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
73 #endif
74 #define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
75 #define __GTHREAD_TIME_INIT {0,0}
77 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
78 # ifndef __gthrw_pragma
79 # define __gthrw_pragma(pragma)
80 # endif
81 # define __gthrw2(name,name2,type) \
82 static __typeof(type) name __attribute__ ((__weakref__(#name2))); \
83 __gthrw_pragma(weak type)
84 # define __gthrw_(name) __gthrw_ ## name
85 #else
86 # define __gthrw2(name,name2,type)
87 # define __gthrw_(name) name
88 #endif
90 /* Typically, __gthrw_foo is a weak reference to symbol foo. */
91 #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
93 /* On Tru64, /usr/include/pthread.h uses #pragma extern_prefix "__" to
94 map a subset of the POSIX pthread API to mangled versions of their
95 names. */
96 #if defined(__osf__) && defined(_PTHREAD_USE_MANGLED_NAMES_)
97 #define __gthrw3(name) __gthrw2(__gthrw_ ## name, __ ## name, name)
98 __gthrw3(pthread_once)
99 __gthrw3(pthread_getspecific)
100 __gthrw3(pthread_setspecific)
102 __gthrw3(pthread_create)
103 __gthrw3(pthread_join)
104 __gthrw3(pthread_detach)
105 __gthrw3(pthread_equal)
106 __gthrw3(pthread_self)
107 __gthrw3(pthread_cancel)
108 __gthrw3(sched_yield)
110 __gthrw3(pthread_mutex_lock)
111 __gthrw3(pthread_mutex_trylock)
112 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
113 __gthrw3(pthread_mutex_timedlock)
114 #endif
115 __gthrw3(pthread_mutex_unlock)
116 __gthrw3(pthread_mutex_init)
117 __gthrw3(pthread_mutex_destroy)
119 __gthrw3(pthread_cond_broadcast)
120 __gthrw3(pthread_cond_signal)
121 __gthrw3(pthread_cond_wait)
122 __gthrw3(pthread_cond_timedwait)
123 __gthrw3(pthread_cond_destroy)
124 #else
125 __gthrw(pthread_once)
126 __gthrw(pthread_getspecific)
127 __gthrw(pthread_setspecific)
129 __gthrw(pthread_create)
130 __gthrw(pthread_join)
131 __gthrw(pthread_equal)
132 __gthrw(pthread_self)
133 __gthrw(pthread_detach)
134 #ifndef __BIONIC__
135 __gthrw(pthread_cancel)
136 #endif
137 __gthrw(sched_yield)
139 __gthrw(pthread_mutex_lock)
140 __gthrw(pthread_mutex_trylock)
141 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
142 __gthrw(pthread_mutex_timedlock)
143 #endif
144 __gthrw(pthread_mutex_unlock)
145 __gthrw(pthread_mutex_init)
146 __gthrw(pthread_mutex_destroy)
148 __gthrw(pthread_cond_broadcast)
149 __gthrw(pthread_cond_signal)
150 __gthrw(pthread_cond_wait)
151 __gthrw(pthread_cond_timedwait)
152 __gthrw(pthread_cond_destroy)
153 #endif
155 __gthrw(pthread_key_create)
156 __gthrw(pthread_key_delete)
157 __gthrw(pthread_mutexattr_init)
158 __gthrw(pthread_mutexattr_settype)
159 __gthrw(pthread_mutexattr_destroy)
162 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
163 /* Objective-C. */
164 #if defined(__osf__) && defined(_PTHREAD_USE_MANGLED_NAMES_)
165 __gthrw3(pthread_cond_init)
166 __gthrw3(pthread_exit)
167 #else
168 __gthrw(pthread_cond_init)
169 __gthrw(pthread_exit)
170 #endif /* __osf__ && _PTHREAD_USE_MANGLED_NAMES_ */
171 #ifdef _POSIX_PRIORITY_SCHEDULING
172 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
173 __gthrw(sched_get_priority_max)
174 __gthrw(sched_get_priority_min)
175 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
176 #endif /* _POSIX_PRIORITY_SCHEDULING */
177 __gthrw(pthread_attr_destroy)
178 __gthrw(pthread_attr_init)
179 __gthrw(pthread_attr_setdetachstate)
180 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
181 __gthrw(pthread_getschedparam)
182 __gthrw(pthread_setschedparam)
183 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
184 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
186 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
188 /* On Solaris 2.6 up to 9, the libc exposes a POSIX threads interface even if
189 -pthreads is not specified. The functions are dummies and most return an
190 error value. However pthread_once returns 0 without invoking the routine
191 it is passed so we cannot pretend that the interface is active if -pthreads
192 is not specified. On Solaris 2.5.1, the interface is not exposed at all so
193 we need to play the usual game with weak symbols. On Solaris 10 and up, a
194 working interface is always exposed. On FreeBSD 6 and later, libc also
195 exposes a dummy POSIX threads interface, similar to what Solaris 2.6 up
196 to 9 does. FreeBSD >= 700014 even provides a pthread_cancel stub in libc,
197 which means the alternate __gthread_active_p below cannot be used there. */
199 #if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
201 static volatile int __gthread_active = -1;
203 static void
204 __gthread_trigger (void)
206 __gthread_active = 1;
209 static inline int
210 __gthread_active_p (void)
212 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
213 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
215 /* Avoid reading __gthread_active twice on the main code path. */
216 int __gthread_active_latest_value = __gthread_active;
218 /* This test is not protected to avoid taking a lock on the main code
219 path so every update of __gthread_active in a threaded program must
220 be atomic with regard to the result of the test. */
221 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
223 if (__gthrw_(pthread_once))
225 /* If this really is a threaded program, then we must ensure that
226 __gthread_active has been set to 1 before exiting this block. */
227 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
228 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
229 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
232 /* Make sure we'll never enter this block again. */
233 if (__gthread_active < 0)
234 __gthread_active = 0;
236 __gthread_active_latest_value = __gthread_active;
239 return __gthread_active_latest_value != 0;
242 #else /* neither FreeBSD nor Solaris */
244 static inline int
245 __gthread_active_p (void)
247 static void *const __gthread_active_ptr
248 = __extension__ (void *) &__gthrw_(
249 /* Android's C library does not provide pthread_cancel, check for
250 `pthread_create' instead. */
251 #ifndef __BIONIC__
252 pthread_cancel
253 #else
254 pthread_create
255 #endif
257 return __gthread_active_ptr != 0;
260 #endif /* FreeBSD or Solaris */
262 #else /* not SUPPORTS_WEAK */
264 /* Similar to Solaris, HP-UX 11 for PA-RISC provides stubs for pthread
265 calls in shared flavors of the HP-UX C library. Most of the stubs
266 have no functionality. The details are described in the "libc cumulative
267 patch" for each subversion of HP-UX 11. There are two special interfaces
268 provided for checking whether an application is linked to a shared pthread
269 library or not. However, these interfaces aren't available in early
270 libpthread libraries. We also need a test that works for archive
271 libraries. We can't use pthread_once as some libc versions call the
272 init function. We also can't use pthread_create or pthread_attr_init
273 as these create a thread and thereby prevent changing the default stack
274 size. The function pthread_default_stacksize_np is available in both
275 the archive and shared versions of libpthread. It can be used to
276 determine the default pthread stack size. There is a stub in some
277 shared libc versions which returns a zero size if pthreads are not
278 active. We provide an equivalent stub to handle cases where libc
279 doesn't provide one. */
281 #if defined(__hppa__) && defined(__hpux__)
283 static volatile int __gthread_active = -1;
285 static inline int
286 __gthread_active_p (void)
288 /* Avoid reading __gthread_active twice on the main code path. */
289 int __gthread_active_latest_value = __gthread_active;
290 size_t __s;
292 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
294 pthread_default_stacksize_np (0, &__s);
295 __gthread_active = __s ? 1 : 0;
296 __gthread_active_latest_value = __gthread_active;
299 return __gthread_active_latest_value != 0;
302 #else /* not hppa-hpux */
304 static inline int
305 __gthread_active_p (void)
307 return 1;
310 #endif /* hppa-hpux */
312 #endif /* SUPPORTS_WEAK */
314 #ifdef _LIBOBJC
316 /* This is the config.h file in libobjc/ */
317 #include <config.h>
319 #ifdef HAVE_SCHED_H
320 # include <sched.h>
321 #endif
323 /* Key structure for maintaining thread specific storage */
324 static pthread_key_t _objc_thread_storage;
325 static pthread_attr_t _objc_thread_attribs;
327 /* Thread local storage for a single thread */
328 static void *thread_local_storage = NULL;
330 /* Backend initialization functions */
332 /* Initialize the threads subsystem. */
333 static inline int
334 __gthread_objc_init_thread_system (void)
336 if (__gthread_active_p ())
338 /* Initialize the thread storage key. */
339 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
341 /* The normal default detach state for threads is
342 * PTHREAD_CREATE_JOINABLE which causes threads to not die
343 * when you think they should. */
344 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
345 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
346 PTHREAD_CREATE_DETACHED) == 0)
347 return 0;
351 return -1;
354 /* Close the threads subsystem. */
355 static inline int
356 __gthread_objc_close_thread_system (void)
358 if (__gthread_active_p ()
359 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
360 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
361 return 0;
363 return -1;
366 /* Backend thread functions */
368 /* Create a new thread of execution. */
369 static inline objc_thread_t
370 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
372 objc_thread_t thread_id;
373 pthread_t new_thread_handle;
375 if (!__gthread_active_p ())
376 return NULL;
378 if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
379 (void *) func, arg)))
380 thread_id = (objc_thread_t) new_thread_handle;
381 else
382 thread_id = NULL;
384 return thread_id;
387 /* Set the current thread's priority. */
388 static inline int
389 __gthread_objc_thread_set_priority (int priority)
391 if (!__gthread_active_p ())
392 return -1;
393 else
395 #ifdef _POSIX_PRIORITY_SCHEDULING
396 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
397 pthread_t thread_id = __gthrw_(pthread_self) ();
398 int policy;
399 struct sched_param params;
400 int priority_min, priority_max;
402 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, &params) == 0)
404 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
405 return -1;
407 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
408 return -1;
410 if (priority > priority_max)
411 priority = priority_max;
412 else if (priority < priority_min)
413 priority = priority_min;
414 params.sched_priority = priority;
417 * The solaris 7 and several other man pages incorrectly state that
418 * this should be a pointer to policy but pthread.h is universally
419 * at odds with this.
421 if (__gthrw_(pthread_setschedparam) (thread_id, policy, &params) == 0)
422 return 0;
424 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
425 #endif /* _POSIX_PRIORITY_SCHEDULING */
426 return -1;
430 /* Return the current thread's priority. */
431 static inline int
432 __gthread_objc_thread_get_priority (void)
434 #ifdef _POSIX_PRIORITY_SCHEDULING
435 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
436 if (__gthread_active_p ())
438 int policy;
439 struct sched_param params;
441 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, &params) == 0)
442 return params.sched_priority;
443 else
444 return -1;
446 else
447 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
448 #endif /* _POSIX_PRIORITY_SCHEDULING */
449 return OBJC_THREAD_INTERACTIVE_PRIORITY;
452 /* Yield our process time to another thread. */
453 static inline void
454 __gthread_objc_thread_yield (void)
456 if (__gthread_active_p ())
457 __gthrw_(sched_yield) ();
460 /* Terminate the current thread. */
461 static inline int
462 __gthread_objc_thread_exit (void)
464 if (__gthread_active_p ())
465 /* exit the thread */
466 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
468 /* Failed if we reached here */
469 return -1;
472 /* Returns an integer value which uniquely describes a thread. */
473 static inline objc_thread_t
474 __gthread_objc_thread_id (void)
476 if (__gthread_active_p ())
477 return (objc_thread_t) __gthrw_(pthread_self) ();
478 else
479 return (objc_thread_t) 1;
482 /* Sets the thread's local storage pointer. */
483 static inline int
484 __gthread_objc_thread_set_data (void *value)
486 if (__gthread_active_p ())
487 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
488 else
490 thread_local_storage = value;
491 return 0;
495 /* Returns the thread's local storage pointer. */
496 static inline void *
497 __gthread_objc_thread_get_data (void)
499 if (__gthread_active_p ())
500 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
501 else
502 return thread_local_storage;
505 /* Backend mutex functions */
507 /* Allocate a mutex. */
508 static inline int
509 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
511 if (__gthread_active_p ())
513 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
515 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
517 objc_free (mutex->backend);
518 mutex->backend = NULL;
519 return -1;
523 return 0;
526 /* Deallocate a mutex. */
527 static inline int
528 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
530 if (__gthread_active_p ())
532 int count;
535 * Posix Threads specifically require that the thread be unlocked
536 * for __gthrw_(pthread_mutex_destroy) to work.
541 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
542 if (count < 0)
543 return -1;
545 while (count);
547 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
548 return -1;
550 objc_free (mutex->backend);
551 mutex->backend = NULL;
553 return 0;
556 /* Grab a lock on a mutex. */
557 static inline int
558 __gthread_objc_mutex_lock (objc_mutex_t mutex)
560 if (__gthread_active_p ()
561 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
563 return -1;
566 return 0;
569 /* Try to grab a lock on a mutex. */
570 static inline int
571 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
573 if (__gthread_active_p ()
574 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
576 return -1;
579 return 0;
582 /* Unlock the mutex */
583 static inline int
584 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
586 if (__gthread_active_p ()
587 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
589 return -1;
592 return 0;
595 /* Backend condition mutex functions */
597 /* Allocate a condition. */
598 static inline int
599 __gthread_objc_condition_allocate (objc_condition_t condition)
601 if (__gthread_active_p ())
603 condition->backend = objc_malloc (sizeof (pthread_cond_t));
605 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
607 objc_free (condition->backend);
608 condition->backend = NULL;
609 return -1;
613 return 0;
616 /* Deallocate a condition. */
617 static inline int
618 __gthread_objc_condition_deallocate (objc_condition_t condition)
620 if (__gthread_active_p ())
622 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
623 return -1;
625 objc_free (condition->backend);
626 condition->backend = NULL;
628 return 0;
631 /* Wait on the condition */
632 static inline int
633 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
635 if (__gthread_active_p ())
636 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
637 (pthread_mutex_t *) mutex->backend);
638 else
639 return 0;
642 /* Wake up all threads waiting on this condition. */
643 static inline int
644 __gthread_objc_condition_broadcast (objc_condition_t condition)
646 if (__gthread_active_p ())
647 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
648 else
649 return 0;
652 /* Wake up one thread waiting on this condition. */
653 static inline int
654 __gthread_objc_condition_signal (objc_condition_t condition)
656 if (__gthread_active_p ())
657 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
658 else
659 return 0;
662 #else /* _LIBOBJC */
664 static inline int
665 __gthread_create (__gthread_t *__threadid, void *(*__func) (void*),
666 void *__args)
668 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
671 static inline int
672 __gthread_join (__gthread_t __threadid, void **__value_ptr)
674 return __gthrw_(pthread_join) (__threadid, __value_ptr);
677 static inline int
678 __gthread_detach (__gthread_t __threadid)
680 return __gthrw_(pthread_detach) (__threadid);
683 static inline int
684 __gthread_equal (__gthread_t __t1, __gthread_t __t2)
686 return __gthrw_(pthread_equal) (__t1, __t2);
689 static inline __gthread_t
690 __gthread_self (void)
692 return __gthrw_(pthread_self) ();
695 static inline int
696 __gthread_yield (void)
698 return __gthrw_(sched_yield) ();
701 static inline int
702 __gthread_once (__gthread_once_t *__once, void (*__func) (void))
704 if (__gthread_active_p ())
705 return __gthrw_(pthread_once) (__once, __func);
706 else
707 return -1;
710 static inline int
711 __gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
713 return __gthrw_(pthread_key_create) (__key, __dtor);
716 static inline int
717 __gthread_key_delete (__gthread_key_t __key)
719 return __gthrw_(pthread_key_delete) (__key);
722 static inline void *
723 __gthread_getspecific (__gthread_key_t __key)
725 return __gthrw_(pthread_getspecific) (__key);
728 static inline int
729 __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
731 return __gthrw_(pthread_setspecific) (__key, __ptr);
734 static inline int
735 __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
737 if (__gthread_active_p ())
738 return __gthrw_(pthread_mutex_destroy) (__mutex);
739 else
740 return 0;
743 static inline int
744 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
746 if (__gthread_active_p ())
747 return __gthrw_(pthread_mutex_lock) (__mutex);
748 else
749 return 0;
752 static inline int
753 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
755 if (__gthread_active_p ())
756 return __gthrw_(pthread_mutex_trylock) (__mutex);
757 else
758 return 0;
761 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
762 static inline int
763 __gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
764 const __gthread_time_t *__abs_timeout)
766 if (__gthread_active_p ())
767 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
768 else
769 return 0;
771 #endif
773 static inline int
774 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
776 if (__gthread_active_p ())
777 return __gthrw_(pthread_mutex_unlock) (__mutex);
778 else
779 return 0;
782 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
783 static inline int
784 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
786 if (__gthread_active_p ())
788 pthread_mutexattr_t __attr;
789 int __r;
791 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
792 if (!__r)
793 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
794 PTHREAD_MUTEX_RECURSIVE);
795 if (!__r)
796 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
797 if (!__r)
798 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
799 return __r;
801 return 0;
803 #endif
805 static inline int
806 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
808 return __gthread_mutex_lock (__mutex);
811 static inline int
812 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
814 return __gthread_mutex_trylock (__mutex);
817 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
818 static inline int
819 __gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
820 const __gthread_time_t *__abs_timeout)
822 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
824 #endif
826 static inline int
827 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
829 return __gthread_mutex_unlock (__mutex);
832 static inline int
833 __gthread_cond_broadcast (__gthread_cond_t *__cond)
835 return __gthrw_(pthread_cond_broadcast) (__cond);
838 static inline int
839 __gthread_cond_signal (__gthread_cond_t *__cond)
841 return __gthrw_(pthread_cond_signal) (__cond);
844 static inline int
845 __gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
847 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
850 static inline int
851 __gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
852 const __gthread_time_t *__abs_timeout)
854 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
857 static inline int
858 __gthread_cond_wait_recursive (__gthread_cond_t *__cond,
859 __gthread_recursive_mutex_t *__mutex)
861 return __gthread_cond_wait (__cond, __mutex);
864 static inline int
865 __gthread_cond_timedwait_recursive (__gthread_cond_t *__cond,
866 __gthread_recursive_mutex_t *__mutex,
867 const __gthread_time_t *__abs_timeout)
869 return __gthread_cond_timedwait (__cond, __mutex, __abs_timeout);
872 static inline int
873 __gthread_cond_destroy (__gthread_cond_t* __cond)
875 return __gthrw_(pthread_cond_destroy) (__cond);
878 #endif /* _LIBOBJC */
880 #endif /* ! GCC_GTHR_POSIX_H */