Merged revisions 143552,143554,143557,143560,143562,143564-143567,143570-143573,14357...
[official-gcc.git] / gcc / gthr-posix.h
blobe0d2bbff61171219b9c461dfdfa31a13c79aef7c
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 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 2, 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 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* As a special exception, if you link this library with other files,
24 some of which are compiled with GCC, to produce an executable,
25 this library does not by itself cause the resulting executable
26 to be covered by the GNU General Public License.
27 This exception does not however invalidate any other reasons why
28 the executable file might be covered by the GNU General Public License. */
30 #ifndef GCC_GTHR_POSIX_H
31 #define GCC_GTHR_POSIX_H
33 /* POSIX threads specific definitions.
34 Easy, since the interface is just one-to-one mapping. */
36 #define __GTHREADS 1
37 #define __GTHREADS_CXX0X 1
39 /* Some implementations of <pthread.h> require this to be defined. */
40 #if !defined(_REENTRANT) && defined(__osf__)
41 #define _REENTRANT 1
42 #endif
44 #include <pthread.h>
45 #include <unistd.h>
47 typedef pthread_t __gthread_t;
48 typedef pthread_key_t __gthread_key_t;
49 typedef pthread_once_t __gthread_once_t;
50 typedef pthread_mutex_t __gthread_mutex_t;
51 typedef pthread_mutex_t __gthread_recursive_mutex_t;
52 typedef pthread_cond_t __gthread_cond_t;
53 typedef struct timespec __gthread_time_t;
55 /* POSIX like conditional variables are supported. Please look at comments
56 in gthr.h for details. */
57 #define __GTHREAD_HAS_COND 1
59 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
60 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
61 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
62 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
63 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
64 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
65 #else
66 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
67 #endif
68 #define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
69 #define __GTHREAD_TIME_INIT {0,0}
71 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
72 # ifndef __gthrw_pragma
73 # define __gthrw_pragma(pragma)
74 # endif
75 # define __gthrw2(name,name2,type) \
76 static __typeof(type) name __attribute__ ((__weakref__(#name2))); \
77 __gthrw_pragma(weak type)
78 # define __gthrw_(name) __gthrw_ ## name
79 #else
80 # define __gthrw2(name,name2,type)
81 # define __gthrw_(name) name
82 #endif
84 /* Typically, __gthrw_foo is a weak reference to symbol foo. */
85 #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
87 /* On Tru64, /usr/include/pthread.h uses #pragma extern_prefix "__" to
88 map a subset of the POSIX pthread API to mangled versions of their
89 names. */
90 #if defined(__osf__) && defined(_PTHREAD_USE_MANGLED_NAMES_)
91 #define __gthrw3(name) __gthrw2(__gthrw_ ## name, __ ## name, name)
92 __gthrw3(pthread_once)
93 __gthrw3(pthread_getspecific)
94 __gthrw3(pthread_setspecific)
96 __gthrw3(pthread_create)
97 __gthrw3(pthread_join)
98 __gthrw3(pthread_detach)
99 __gthrw3(pthread_equal)
100 __gthrw3(pthread_self)
101 __gthrw3(pthread_cancel)
102 __gthrw3(sched_yield)
104 __gthrw3(pthread_mutex_lock)
105 __gthrw3(pthread_mutex_trylock)
106 #ifdef _POSIX_TIMEOUTS
107 #if _POSIX_TIMEOUTS >= 0
108 __gthrw3(pthread_mutex_timedlock)
109 #endif
110 #endif /* _POSIX_TIMEOUTS */
111 __gthrw3(pthread_mutex_unlock)
112 __gthrw3(pthread_mutex_init)
113 __gthrw3(pthread_mutex_destroy)
115 __gthrw3(pthread_cond_broadcast)
116 __gthrw3(pthread_cond_signal)
117 __gthrw3(pthread_cond_wait)
118 __gthrw3(pthread_cond_timedwait)
119 __gthrw3(pthread_cond_destroy)
120 #else
121 __gthrw(pthread_once)
122 __gthrw(pthread_getspecific)
123 __gthrw(pthread_setspecific)
125 __gthrw(pthread_create)
126 __gthrw(pthread_join)
127 __gthrw(pthread_equal)
128 __gthrw(pthread_self)
129 __gthrw(pthread_detach)
130 __gthrw(pthread_cancel)
131 __gthrw(sched_yield)
133 __gthrw(pthread_mutex_lock)
134 __gthrw(pthread_mutex_trylock)
135 #ifdef _POSIX_TIMEOUTS
136 #if _POSIX_TIMEOUTS >= 0
137 __gthrw(pthread_mutex_timedlock)
138 #endif
139 #endif /* _POSIX_TIMEOUTS */
140 __gthrw(pthread_mutex_unlock)
141 __gthrw(pthread_mutex_init)
142 __gthrw(pthread_mutex_destroy)
144 __gthrw(pthread_cond_broadcast)
145 __gthrw(pthread_cond_signal)
146 __gthrw(pthread_cond_wait)
147 __gthrw(pthread_cond_timedwait)
148 __gthrw(pthread_cond_destroy)
149 #endif
151 __gthrw(pthread_key_create)
152 __gthrw(pthread_key_delete)
153 __gthrw(pthread_mutexattr_init)
154 __gthrw(pthread_mutexattr_settype)
155 __gthrw(pthread_mutexattr_destroy)
158 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
159 /* Objective-C. */
160 #if defined(__osf__) && defined(_PTHREAD_USE_MANGLED_NAMES_)
161 __gthrw3(pthread_cond_init)
162 __gthrw3(pthread_exit)
163 #else
164 __gthrw(pthread_cond_init)
165 __gthrw(pthread_exit)
166 #endif /* __osf__ && _PTHREAD_USE_MANGLED_NAMES_ */
167 #ifdef _POSIX_PRIORITY_SCHEDULING
168 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
169 __gthrw(sched_get_priority_max)
170 __gthrw(sched_get_priority_min)
171 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
172 #endif /* _POSIX_PRIORITY_SCHEDULING */
173 __gthrw(pthread_attr_destroy)
174 __gthrw(pthread_attr_init)
175 __gthrw(pthread_attr_setdetachstate)
176 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
177 __gthrw(pthread_getschedparam)
178 __gthrw(pthread_setschedparam)
179 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
180 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
182 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
184 /* On Solaris 2.6 up to 9, the libc exposes a POSIX threads interface even if
185 -pthreads is not specified. The functions are dummies and most return an
186 error value. However pthread_once returns 0 without invoking the routine
187 it is passed so we cannot pretend that the interface is active if -pthreads
188 is not specified. On Solaris 2.5.1, the interface is not exposed at all so
189 we need to play the usual game with weak symbols. On Solaris 10 and up, a
190 working interface is always exposed. On FreeBSD 6 and later, libc also
191 exposes a dummy POSIX threads interface, similar to what Solaris 2.6 up
192 to 9 does. FreeBSD >= 700014 even provides a pthread_cancel stub in libc,
193 which means the alternate __gthread_active_p below cannot be used there. */
195 #if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
197 static volatile int __gthread_active = -1;
199 static void
200 __gthread_trigger (void)
202 __gthread_active = 1;
205 static inline int
206 __gthread_active_p (void)
208 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
209 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
211 /* Avoid reading __gthread_active twice on the main code path. */
212 int __gthread_active_latest_value = __gthread_active;
214 /* This test is not protected to avoid taking a lock on the main code
215 path so every update of __gthread_active in a threaded program must
216 be atomic with regard to the result of the test. */
217 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
219 if (__gthrw_(pthread_once))
221 /* If this really is a threaded program, then we must ensure that
222 __gthread_active has been set to 1 before exiting this block. */
223 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
224 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
225 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
228 /* Make sure we'll never enter this block again. */
229 if (__gthread_active < 0)
230 __gthread_active = 0;
232 __gthread_active_latest_value = __gthread_active;
235 return __gthread_active_latest_value != 0;
238 #else /* neither FreeBSD nor Solaris */
240 static inline int
241 __gthread_active_p (void)
243 static void *const __gthread_active_ptr
244 = __extension__ (void *) &__gthrw_(pthread_cancel);
245 return __gthread_active_ptr != 0;
248 #endif /* FreeBSD or Solaris */
250 #else /* not SUPPORTS_WEAK */
252 /* Similar to Solaris, HP-UX 11 for PA-RISC provides stubs for pthread
253 calls in shared flavors of the HP-UX C library. Most of the stubs
254 have no functionality. The details are described in the "libc cumulative
255 patch" for each subversion of HP-UX 11. There are two special interfaces
256 provided for checking whether an application is linked to a pthread
257 library or not. However, these interfaces aren't available in early
258 libc versions. We also can't use pthread_once as some libc versions
259 call the init function. So, we use pthread_create to check whether it
260 is possible to create a thread or not. The stub implementation returns
261 the error number ENOSYS. */
263 #if defined(__hppa__) && defined(__hpux__)
265 #include <errno.h>
267 static volatile int __gthread_active = -1;
269 static void *
270 __gthread_start (void *__arg __attribute__((unused)))
272 return NULL;
275 static void __gthread_active_init (void) __attribute__((noinline));
276 static void
277 __gthread_active_init (void)
279 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
280 pthread_t __t;
281 pthread_attr_t __a;
282 int __result;
284 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
285 if (__gthread_active < 0)
287 __gthrw_(pthread_attr_init) (&__a);
288 __gthrw_(pthread_attr_setdetachstate) (&__a, PTHREAD_CREATE_DETACHED);
289 __result = __gthrw_(pthread_create) (&__t, &__a, __gthread_start, NULL);
290 if (__result != ENOSYS)
291 __gthread_active = 1;
292 else
293 __gthread_active = 0;
294 __gthrw_(pthread_attr_destroy) (&__a);
296 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
299 static inline int
300 __gthread_active_p (void)
302 /* Avoid reading __gthread_active twice on the main code path. */
303 int __gthread_active_latest_value = __gthread_active;
305 /* This test is not protected to avoid taking a lock on the main code
306 path so every update of __gthread_active in a threaded program must
307 be atomic with regard to the result of the test. */
308 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
310 __gthread_active_init ();
311 __gthread_active_latest_value = __gthread_active;
314 return __gthread_active_latest_value != 0;
317 #else /* not hppa-hpux */
319 static inline int
320 __gthread_active_p (void)
322 return 1;
325 #endif /* hppa-hpux */
327 #endif /* SUPPORTS_WEAK */
329 #ifdef _LIBOBJC
331 /* This is the config.h file in libobjc/ */
332 #include <config.h>
334 #ifdef HAVE_SCHED_H
335 # include <sched.h>
336 #endif
338 /* Key structure for maintaining thread specific storage */
339 static pthread_key_t _objc_thread_storage;
340 static pthread_attr_t _objc_thread_attribs;
342 /* Thread local storage for a single thread */
343 static void *thread_local_storage = NULL;
345 /* Backend initialization functions */
347 /* Initialize the threads subsystem. */
348 static inline int
349 __gthread_objc_init_thread_system (void)
351 if (__gthread_active_p ())
353 /* Initialize the thread storage key. */
354 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
356 /* The normal default detach state for threads is
357 * PTHREAD_CREATE_JOINABLE which causes threads to not die
358 * when you think they should. */
359 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
360 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
361 PTHREAD_CREATE_DETACHED) == 0)
362 return 0;
366 return -1;
369 /* Close the threads subsystem. */
370 static inline int
371 __gthread_objc_close_thread_system (void)
373 if (__gthread_active_p ()
374 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
375 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
376 return 0;
378 return -1;
381 /* Backend thread functions */
383 /* Create a new thread of execution. */
384 static inline objc_thread_t
385 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
387 objc_thread_t thread_id;
388 pthread_t new_thread_handle;
390 if (!__gthread_active_p ())
391 return NULL;
393 if (!(__gthrw_(pthread_create) (&new_thread_handle, NULL, (void *) func, arg)))
394 thread_id = (objc_thread_t) new_thread_handle;
395 else
396 thread_id = NULL;
398 return thread_id;
401 /* Set the current thread's priority. */
402 static inline int
403 __gthread_objc_thread_set_priority (int priority)
405 if (!__gthread_active_p ())
406 return -1;
407 else
409 #ifdef _POSIX_PRIORITY_SCHEDULING
410 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
411 pthread_t thread_id = __gthrw_(pthread_self) ();
412 int policy;
413 struct sched_param params;
414 int priority_min, priority_max;
416 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, &params) == 0)
418 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
419 return -1;
421 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
422 return -1;
424 if (priority > priority_max)
425 priority = priority_max;
426 else if (priority < priority_min)
427 priority = priority_min;
428 params.sched_priority = priority;
431 * The solaris 7 and several other man pages incorrectly state that
432 * this should be a pointer to policy but pthread.h is universally
433 * at odds with this.
435 if (__gthrw_(pthread_setschedparam) (thread_id, policy, &params) == 0)
436 return 0;
438 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
439 #endif /* _POSIX_PRIORITY_SCHEDULING */
440 return -1;
444 /* Return the current thread's priority. */
445 static inline int
446 __gthread_objc_thread_get_priority (void)
448 #ifdef _POSIX_PRIORITY_SCHEDULING
449 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
450 if (__gthread_active_p ())
452 int policy;
453 struct sched_param params;
455 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, &params) == 0)
456 return params.sched_priority;
457 else
458 return -1;
460 else
461 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
462 #endif /* _POSIX_PRIORITY_SCHEDULING */
463 return OBJC_THREAD_INTERACTIVE_PRIORITY;
466 /* Yield our process time to another thread. */
467 static inline void
468 __gthread_objc_thread_yield (void)
470 if (__gthread_active_p ())
471 __gthrw_(sched_yield) ();
474 /* Terminate the current thread. */
475 static inline int
476 __gthread_objc_thread_exit (void)
478 if (__gthread_active_p ())
479 /* exit the thread */
480 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
482 /* Failed if we reached here */
483 return -1;
486 /* Returns an integer value which uniquely describes a thread. */
487 static inline objc_thread_t
488 __gthread_objc_thread_id (void)
490 if (__gthread_active_p ())
491 return (objc_thread_t) __gthrw_(pthread_self) ();
492 else
493 return (objc_thread_t) 1;
496 /* Sets the thread's local storage pointer. */
497 static inline int
498 __gthread_objc_thread_set_data (void *value)
500 if (__gthread_active_p ())
501 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
502 else
504 thread_local_storage = value;
505 return 0;
509 /* Returns the thread's local storage pointer. */
510 static inline void *
511 __gthread_objc_thread_get_data (void)
513 if (__gthread_active_p ())
514 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
515 else
516 return thread_local_storage;
519 /* Backend mutex functions */
521 /* Allocate a mutex. */
522 static inline int
523 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
525 if (__gthread_active_p ())
527 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
529 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
531 objc_free (mutex->backend);
532 mutex->backend = NULL;
533 return -1;
537 return 0;
540 /* Deallocate a mutex. */
541 static inline int
542 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
544 if (__gthread_active_p ())
546 int count;
549 * Posix Threads specifically require that the thread be unlocked
550 * for __gthrw_(pthread_mutex_destroy) to work.
555 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
556 if (count < 0)
557 return -1;
559 while (count);
561 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
562 return -1;
564 objc_free (mutex->backend);
565 mutex->backend = NULL;
567 return 0;
570 /* Grab a lock on a mutex. */
571 static inline int
572 __gthread_objc_mutex_lock (objc_mutex_t mutex)
574 if (__gthread_active_p ()
575 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
577 return -1;
580 return 0;
583 /* Try to grab a lock on a mutex. */
584 static inline int
585 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
587 if (__gthread_active_p ()
588 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
590 return -1;
593 return 0;
596 /* Unlock the mutex */
597 static inline int
598 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
600 if (__gthread_active_p ()
601 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
603 return -1;
606 return 0;
609 /* Backend condition mutex functions */
611 /* Allocate a condition. */
612 static inline int
613 __gthread_objc_condition_allocate (objc_condition_t condition)
615 if (__gthread_active_p ())
617 condition->backend = objc_malloc (sizeof (pthread_cond_t));
619 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
621 objc_free (condition->backend);
622 condition->backend = NULL;
623 return -1;
627 return 0;
630 /* Deallocate a condition. */
631 static inline int
632 __gthread_objc_condition_deallocate (objc_condition_t condition)
634 if (__gthread_active_p ())
636 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
637 return -1;
639 objc_free (condition->backend);
640 condition->backend = NULL;
642 return 0;
645 /* Wait on the condition */
646 static inline int
647 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
649 if (__gthread_active_p ())
650 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
651 (pthread_mutex_t *) mutex->backend);
652 else
653 return 0;
656 /* Wake up all threads waiting on this condition. */
657 static inline int
658 __gthread_objc_condition_broadcast (objc_condition_t condition)
660 if (__gthread_active_p ())
661 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
662 else
663 return 0;
666 /* Wake up one thread waiting on this condition. */
667 static inline int
668 __gthread_objc_condition_signal (objc_condition_t condition)
670 if (__gthread_active_p ())
671 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
672 else
673 return 0;
676 #else /* _LIBOBJC */
678 static inline int
679 __gthread_create (__gthread_t *__threadid, void *(*__func) (void*),
680 void *__args)
682 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
685 static inline int
686 __gthread_join (__gthread_t __threadid, void **__value_ptr)
688 return __gthrw_(pthread_join) (__threadid, __value_ptr);
691 static inline int
692 __gthread_detach (__gthread_t __threadid)
694 return __gthrw_(pthread_detach) (__threadid);
697 static inline int
698 __gthread_equal (__gthread_t __t1, __gthread_t __t2)
700 return __gthrw_(pthread_equal) (__t1, __t2);
703 static inline __gthread_t
704 __gthread_self (void)
706 return __gthrw_(pthread_self) ();
709 static inline int
710 __gthread_yield (void)
712 return __gthrw_(sched_yield) ();
715 static inline int
716 __gthread_once (__gthread_once_t *__once, void (*__func) (void))
718 if (__gthread_active_p ())
719 return __gthrw_(pthread_once) (__once, __func);
720 else
721 return -1;
724 static inline int
725 __gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
727 return __gthrw_(pthread_key_create) (__key, __dtor);
730 static inline int
731 __gthread_key_delete (__gthread_key_t __key)
733 return __gthrw_(pthread_key_delete) (__key);
736 static inline void *
737 __gthread_getspecific (__gthread_key_t __key)
739 return __gthrw_(pthread_getspecific) (__key);
742 static inline int
743 __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
745 return __gthrw_(pthread_setspecific) (__key, __ptr);
748 static inline int
749 __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
751 if (__gthread_active_p ())
752 return __gthrw_(pthread_mutex_destroy) (__mutex);
753 else
754 return 0;
757 static inline int
758 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
760 if (__gthread_active_p ())
761 return __gthrw_(pthread_mutex_lock) (__mutex);
762 else
763 return 0;
766 static inline int
767 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
769 if (__gthread_active_p ())
770 return __gthrw_(pthread_mutex_trylock) (__mutex);
771 else
772 return 0;
775 #ifdef _POSIX_TIMEOUTS
776 #if _POSIX_TIMEOUTS >= 0
777 static inline int
778 __gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
779 const __gthread_time_t *__abs_timeout)
781 if (__gthread_active_p ())
782 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
783 else
784 return 0;
786 #endif
787 #endif
789 static inline int
790 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
792 if (__gthread_active_p ())
793 return __gthrw_(pthread_mutex_unlock) (__mutex);
794 else
795 return 0;
798 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
799 static inline int
800 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
802 if (__gthread_active_p ())
804 pthread_mutexattr_t __attr;
805 int __r;
807 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
808 if (!__r)
809 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
810 PTHREAD_MUTEX_RECURSIVE);
811 if (!__r)
812 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
813 if (!__r)
814 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
815 return __r;
817 return 0;
819 #endif
821 static inline int
822 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
824 return __gthread_mutex_lock (__mutex);
827 static inline int
828 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
830 return __gthread_mutex_trylock (__mutex);
833 #ifdef _POSIX_TIMEOUTS
834 #if _POSIX_TIMEOUTS >= 0
835 static inline int
836 __gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
837 const __gthread_time_t *__abs_timeout)
839 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
841 #endif
842 #endif
844 static inline int
845 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
847 return __gthread_mutex_unlock (__mutex);
850 static inline int
851 __gthread_cond_broadcast (__gthread_cond_t *__cond)
853 return __gthrw_(pthread_cond_broadcast) (__cond);
856 static inline int
857 __gthread_cond_signal (__gthread_cond_t *__cond)
859 return __gthrw_(pthread_cond_signal) (__cond);
862 static inline int
863 __gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
865 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
868 static inline int
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);
875 static inline int
876 __gthread_cond_wait_recursive (__gthread_cond_t *__cond,
877 __gthread_recursive_mutex_t *__mutex)
879 return __gthread_cond_wait (__cond, __mutex);
882 static inline int
883 __gthread_cond_timedwait_recursive (__gthread_cond_t *__cond,
884 __gthread_recursive_mutex_t *__mutex,
885 const __gthread_time_t *__abs_timeout)
887 return __gthread_cond_timedwait (__cond, __mutex, __abs_timeout);
890 static inline int
891 __gthread_cond_destroy (__gthread_cond_t* __cond)
893 return __gthrw_(pthread_cond_destroy) (__cond);
896 #endif /* _LIBOBJC */
898 #endif /* ! GCC_GTHR_POSIX_H */