2012-09-30 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / libgcc / gthr-posix.h
blobcc4e518f5b9907db18cdfc0a37fbeb67451b63d5
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 /* For a program to be multi-threaded the only thing that it certainly must
216 be using is pthread_create. However, there may be other libraries that
217 intercept pthread_create with their own definitions to wrap pthreads
218 functionality for some purpose. In those cases, pthread_create being
219 defined might not necessarily mean that libpthread is actually linked
222 For the GNU C library, we can use a known internal name. This is always
223 available in the ABI, but no other library would define it. That is
224 ideal, since any public pthread function might be intercepted just as
225 pthread_create might be. __pthread_key_create is an "internal"
226 implementation symbol, but it is part of the public exported ABI. Also,
227 it's among the symbols that the static libpthread.a always links in
228 whenever pthread_create is used, so there is no danger of a false
229 negative result in any statically-linked, multi-threaded program.
231 For others, we choose pthread_cancel as a function that seems unlikely
232 to be redefined by an interceptor library. The bionic (Android) C
233 library does not provide pthread_cancel, so we do use pthread_create
234 there (and interceptor libraries lose). */
236 #ifdef __GLIBC__
237 __gthrw2(__gthrw_(__pthread_key_create),
238 __pthread_key_create,
239 pthread_key_create)
240 # define GTHR_ACTIVE_PROXY __gthrw_(__pthread_key_create)
241 #elif defined (__BIONIC__)
242 # define GTHR_ACTIVE_PROXY __gthrw_(pthread_create)
243 #else
244 # define GTHR_ACTIVE_PROXY __gthrw_(pthread_cancel)
245 #endif
247 static inline int
248 __gthread_active_p (void)
250 static void *const __gthread_active_ptr
251 = __extension__ (void *) &GTHR_ACTIVE_PROXY;
252 return __gthread_active_ptr != 0;
255 #endif /* FreeBSD or Solaris */
257 #else /* not SUPPORTS_WEAK */
259 /* Similar to Solaris, HP-UX 11 for PA-RISC provides stubs for pthread
260 calls in shared flavors of the HP-UX C library. Most of the stubs
261 have no functionality. The details are described in the "libc cumulative
262 patch" for each subversion of HP-UX 11. There are two special interfaces
263 provided for checking whether an application is linked to a shared pthread
264 library or not. However, these interfaces aren't available in early
265 libpthread libraries. We also need a test that works for archive
266 libraries. We can't use pthread_once as some libc versions call the
267 init function. We also can't use pthread_create or pthread_attr_init
268 as these create a thread and thereby prevent changing the default stack
269 size. The function pthread_default_stacksize_np is available in both
270 the archive and shared versions of libpthread. It can be used to
271 determine the default pthread stack size. There is a stub in some
272 shared libc versions which returns a zero size if pthreads are not
273 active. We provide an equivalent stub to handle cases where libc
274 doesn't provide one. */
276 #if defined(__hppa__) && defined(__hpux__)
278 static volatile int __gthread_active = -1;
280 static inline int
281 __gthread_active_p (void)
283 /* Avoid reading __gthread_active twice on the main code path. */
284 int __gthread_active_latest_value = __gthread_active;
285 size_t __s;
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;
297 #else /* not hppa-hpux */
299 static inline int
300 __gthread_active_p (void)
302 return 1;
305 #endif /* hppa-hpux */
307 #endif /* SUPPORTS_WEAK */
309 #ifdef _LIBOBJC
311 /* This is the config.h file in libobjc/ */
312 #include <config.h>
314 #ifdef HAVE_SCHED_H
315 # include <sched.h>
316 #endif
318 /* Key structure for maintaining thread specific storage */
319 static pthread_key_t _objc_thread_storage;
320 static pthread_attr_t _objc_thread_attribs;
322 /* Thread local storage for a single thread */
323 static void *thread_local_storage = NULL;
325 /* Backend initialization functions */
327 /* Initialize the threads subsystem. */
328 static inline int
329 __gthread_objc_init_thread_system (void)
331 if (__gthread_active_p ())
333 /* Initialize the thread storage key. */
334 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
336 /* The normal default detach state for threads is
337 * PTHREAD_CREATE_JOINABLE which causes threads to not die
338 * when you think they should. */
339 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
340 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
341 PTHREAD_CREATE_DETACHED) == 0)
342 return 0;
346 return -1;
349 /* Close the threads subsystem. */
350 static inline int
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)
356 return 0;
358 return -1;
361 /* Backend thread functions */
363 /* Create a new thread of execution. */
364 static inline objc_thread_t
365 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
367 objc_thread_t thread_id;
368 pthread_t new_thread_handle;
370 if (!__gthread_active_p ())
371 return NULL;
373 if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
374 (void *) func, arg)))
375 thread_id = (objc_thread_t) new_thread_handle;
376 else
377 thread_id = NULL;
379 return thread_id;
382 /* Set the current thread's priority. */
383 static inline int
384 __gthread_objc_thread_set_priority (int priority)
386 if (!__gthread_active_p ())
387 return -1;
388 else
390 #ifdef _POSIX_PRIORITY_SCHEDULING
391 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
392 pthread_t thread_id = __gthrw_(pthread_self) ();
393 int policy;
394 struct sched_param params;
395 int priority_min, priority_max;
397 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, &params) == 0)
399 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
400 return -1;
402 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
403 return -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;
412 * The solaris 7 and several other man pages incorrectly state that
413 * this should be a pointer to policy but pthread.h is universally
414 * at odds with this.
416 if (__gthrw_(pthread_setschedparam) (thread_id, policy, &params) == 0)
417 return 0;
419 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
420 #endif /* _POSIX_PRIORITY_SCHEDULING */
421 return -1;
425 /* Return the current thread's priority. */
426 static inline int
427 __gthread_objc_thread_get_priority (void)
429 #ifdef _POSIX_PRIORITY_SCHEDULING
430 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
431 if (__gthread_active_p ())
433 int policy;
434 struct sched_param params;
436 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, &params) == 0)
437 return params.sched_priority;
438 else
439 return -1;
441 else
442 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
443 #endif /* _POSIX_PRIORITY_SCHEDULING */
444 return OBJC_THREAD_INTERACTIVE_PRIORITY;
447 /* Yield our process time to another thread. */
448 static inline void
449 __gthread_objc_thread_yield (void)
451 if (__gthread_active_p ())
452 __gthrw_(sched_yield) ();
455 /* Terminate the current thread. */
456 static inline int
457 __gthread_objc_thread_exit (void)
459 if (__gthread_active_p ())
460 /* exit the thread */
461 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
463 /* Failed if we reached here */
464 return -1;
467 /* Returns an integer value which uniquely describes a thread. */
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) ();
473 else
474 return (objc_thread_t) 1;
477 /* Sets the thread's local storage pointer. */
478 static inline int
479 __gthread_objc_thread_set_data (void *value)
481 if (__gthread_active_p ())
482 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
483 else
485 thread_local_storage = value;
486 return 0;
490 /* Returns the thread's local storage pointer. */
491 static inline void *
492 __gthread_objc_thread_get_data (void)
494 if (__gthread_active_p ())
495 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
496 else
497 return thread_local_storage;
500 /* Backend mutex functions */
502 /* Allocate a mutex. */
503 static inline int
504 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
506 if (__gthread_active_p ())
508 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
510 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
512 objc_free (mutex->backend);
513 mutex->backend = NULL;
514 return -1;
518 return 0;
521 /* Deallocate a mutex. */
522 static inline int
523 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
525 if (__gthread_active_p ())
527 int count;
530 * Posix Threads specifically require that the thread be unlocked
531 * for __gthrw_(pthread_mutex_destroy) to work.
536 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
537 if (count < 0)
538 return -1;
540 while (count);
542 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
543 return -1;
545 objc_free (mutex->backend);
546 mutex->backend = NULL;
548 return 0;
551 /* Grab a lock on a mutex. */
552 static inline int
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)
558 return -1;
561 return 0;
564 /* Try to grab a lock on a mutex. */
565 static inline int
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)
571 return -1;
574 return 0;
577 /* Unlock the mutex */
578 static inline int
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)
584 return -1;
587 return 0;
590 /* Backend condition mutex functions */
592 /* Allocate a condition. */
593 static inline int
594 __gthread_objc_condition_allocate (objc_condition_t condition)
596 if (__gthread_active_p ())
598 condition->backend = objc_malloc (sizeof (pthread_cond_t));
600 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
602 objc_free (condition->backend);
603 condition->backend = NULL;
604 return -1;
608 return 0;
611 /* Deallocate a condition. */
612 static inline int
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))
618 return -1;
620 objc_free (condition->backend);
621 condition->backend = NULL;
623 return 0;
626 /* Wait on the condition */
627 static inline int
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,
632 (pthread_mutex_t *) mutex->backend);
633 else
634 return 0;
637 /* Wake up all threads waiting on this condition. */
638 static inline int
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);
643 else
644 return 0;
647 /* Wake up one thread waiting on this condition. */
648 static inline int
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);
653 else
654 return 0;
657 #else /* _LIBOBJC */
659 static inline int
660 __gthread_create (__gthread_t *__threadid, void *(*__func) (void*),
661 void *__args)
663 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
666 static inline int
667 __gthread_join (__gthread_t __threadid, void **__value_ptr)
669 return __gthrw_(pthread_join) (__threadid, __value_ptr);
672 static inline int
673 __gthread_detach (__gthread_t __threadid)
675 return __gthrw_(pthread_detach) (__threadid);
678 static inline int
679 __gthread_equal (__gthread_t __t1, __gthread_t __t2)
681 return __gthrw_(pthread_equal) (__t1, __t2);
684 static inline __gthread_t
685 __gthread_self (void)
687 return __gthrw_(pthread_self) ();
690 static inline int
691 __gthread_yield (void)
693 return __gthrw_(sched_yield) ();
696 static inline int
697 __gthread_once (__gthread_once_t *__once, void (*__func) (void))
699 if (__gthread_active_p ())
700 return __gthrw_(pthread_once) (__once, __func);
701 else
702 return -1;
705 static inline int
706 __gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
708 return __gthrw_(pthread_key_create) (__key, __dtor);
711 static inline int
712 __gthread_key_delete (__gthread_key_t __key)
714 return __gthrw_(pthread_key_delete) (__key);
717 static inline void *
718 __gthread_getspecific (__gthread_key_t __key)
720 return __gthrw_(pthread_getspecific) (__key);
723 static inline int
724 __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
726 return __gthrw_(pthread_setspecific) (__key, __ptr);
729 static inline void
730 __gthread_mutex_init_function (__gthread_mutex_t *__mutex)
732 if (__gthread_active_p ())
733 __gthrw_(pthread_mutex_init) (__mutex, NULL);
736 static inline int
737 __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
739 if (__gthread_active_p ())
740 return __gthrw_(pthread_mutex_destroy) (__mutex);
741 else
742 return 0;
745 static inline int
746 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
748 if (__gthread_active_p ())
749 return __gthrw_(pthread_mutex_lock) (__mutex);
750 else
751 return 0;
754 static inline int
755 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
757 if (__gthread_active_p ())
758 return __gthrw_(pthread_mutex_trylock) (__mutex);
759 else
760 return 0;
763 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
764 static inline int
765 __gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
766 const __gthread_time_t *__abs_timeout)
768 if (__gthread_active_p ())
769 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
770 else
771 return 0;
773 #endif
775 static inline int
776 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
778 if (__gthread_active_p ())
779 return __gthrw_(pthread_mutex_unlock) (__mutex);
780 else
781 return 0;
784 #if !defined( PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) \
785 || defined(_GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC)
786 static inline int
787 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
789 if (__gthread_active_p ())
791 pthread_mutexattr_t __attr;
792 int __r;
794 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
795 if (!__r)
796 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
797 PTHREAD_MUTEX_RECURSIVE);
798 if (!__r)
799 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
800 if (!__r)
801 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
802 return __r;
804 return 0;
806 #endif
808 static inline int
809 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
811 return __gthread_mutex_lock (__mutex);
814 static inline int
815 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
817 return __gthread_mutex_trylock (__mutex);
820 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
821 static inline int
822 __gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
823 const __gthread_time_t *__abs_timeout)
825 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
827 #endif
829 static inline int
830 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
832 return __gthread_mutex_unlock (__mutex);
835 #ifdef _GTHREAD_USE_COND_INIT_FUNC
836 static inline void
837 __gthread_cond_init_function (__gthread_cond_t *__cond)
839 if (__gthread_active_p ())
840 __gthrw_(pthread_cond_init) (__cond, NULL);
842 #endif
844 static inline int
845 __gthread_cond_broadcast (__gthread_cond_t *__cond)
847 return __gthrw_(pthread_cond_broadcast) (__cond);
850 static inline int
851 __gthread_cond_signal (__gthread_cond_t *__cond)
853 return __gthrw_(pthread_cond_signal) (__cond);
856 static inline int
857 __gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
859 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
862 static inline int
863 __gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
864 const __gthread_time_t *__abs_timeout)
866 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
869 static inline int
870 __gthread_cond_wait_recursive (__gthread_cond_t *__cond,
871 __gthread_recursive_mutex_t *__mutex)
873 return __gthread_cond_wait (__cond, __mutex);
876 static inline int
877 __gthread_cond_timedwait_recursive (__gthread_cond_t *__cond,
878 __gthread_recursive_mutex_t *__mutex,
879 const __gthread_time_t *__abs_timeout)
881 return __gthread_cond_timedwait (__cond, __mutex, __abs_timeout);
884 static inline int
885 __gthread_cond_destroy (__gthread_cond_t* __cond)
887 return __gthrw_(pthread_cond_destroy) (__cond);
890 #endif /* _LIBOBJC */
892 #endif /* ! GCC_GTHR_POSIX_H */