2012-01-31 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgcc / gthr-posix.h
blob46054f6a7c212a054879952932d8e598da686674
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 /* Android's C library does not provide pthread_cancel, check for
248 `pthread_create' instead. */
249 #ifndef __BIONIC__
250 static void *const __gthread_active_ptr
251 = __extension__ (void *) &__gthrw_(pthread_cancel);
252 #else
253 static void *const __gthread_active_ptr
254 = __extension__ (void *) &__gthrw_(pthread_create);
255 #endif
256 return __gthread_active_ptr != 0;
259 #endif /* FreeBSD or Solaris */
261 #else /* not SUPPORTS_WEAK */
263 /* Similar to Solaris, HP-UX 11 for PA-RISC provides stubs for pthread
264 calls in shared flavors of the HP-UX C library. Most of the stubs
265 have no functionality. The details are described in the "libc cumulative
266 patch" for each subversion of HP-UX 11. There are two special interfaces
267 provided for checking whether an application is linked to a shared pthread
268 library or not. However, these interfaces aren't available in early
269 libpthread libraries. We also need a test that works for archive
270 libraries. We can't use pthread_once as some libc versions call the
271 init function. We also can't use pthread_create or pthread_attr_init
272 as these create a thread and thereby prevent changing the default stack
273 size. The function pthread_default_stacksize_np is available in both
274 the archive and shared versions of libpthread. It can be used to
275 determine the default pthread stack size. There is a stub in some
276 shared libc versions which returns a zero size if pthreads are not
277 active. We provide an equivalent stub to handle cases where libc
278 doesn't provide one. */
280 #if defined(__hppa__) && defined(__hpux__)
282 static volatile int __gthread_active = -1;
284 static inline int
285 __gthread_active_p (void)
287 /* Avoid reading __gthread_active twice on the main code path. */
288 int __gthread_active_latest_value = __gthread_active;
289 size_t __s;
291 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
293 pthread_default_stacksize_np (0, &__s);
294 __gthread_active = __s ? 1 : 0;
295 __gthread_active_latest_value = __gthread_active;
298 return __gthread_active_latest_value != 0;
301 #else /* not hppa-hpux */
303 static inline int
304 __gthread_active_p (void)
306 return 1;
309 #endif /* hppa-hpux */
311 #endif /* SUPPORTS_WEAK */
313 #ifdef _LIBOBJC
315 /* This is the config.h file in libobjc/ */
316 #include <config.h>
318 #ifdef HAVE_SCHED_H
319 # include <sched.h>
320 #endif
322 /* Key structure for maintaining thread specific storage */
323 static pthread_key_t _objc_thread_storage;
324 static pthread_attr_t _objc_thread_attribs;
326 /* Thread local storage for a single thread */
327 static void *thread_local_storage = NULL;
329 /* Backend initialization functions */
331 /* Initialize the threads subsystem. */
332 static inline int
333 __gthread_objc_init_thread_system (void)
335 if (__gthread_active_p ())
337 /* Initialize the thread storage key. */
338 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
340 /* The normal default detach state for threads is
341 * PTHREAD_CREATE_JOINABLE which causes threads to not die
342 * when you think they should. */
343 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
344 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
345 PTHREAD_CREATE_DETACHED) == 0)
346 return 0;
350 return -1;
353 /* Close the threads subsystem. */
354 static inline int
355 __gthread_objc_close_thread_system (void)
357 if (__gthread_active_p ()
358 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
359 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
360 return 0;
362 return -1;
365 /* Backend thread functions */
367 /* Create a new thread of execution. */
368 static inline objc_thread_t
369 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
371 objc_thread_t thread_id;
372 pthread_t new_thread_handle;
374 if (!__gthread_active_p ())
375 return NULL;
377 if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
378 (void *) func, arg)))
379 thread_id = (objc_thread_t) new_thread_handle;
380 else
381 thread_id = NULL;
383 return thread_id;
386 /* Set the current thread's priority. */
387 static inline int
388 __gthread_objc_thread_set_priority (int priority)
390 if (!__gthread_active_p ())
391 return -1;
392 else
394 #ifdef _POSIX_PRIORITY_SCHEDULING
395 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
396 pthread_t thread_id = __gthrw_(pthread_self) ();
397 int policy;
398 struct sched_param params;
399 int priority_min, priority_max;
401 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, &params) == 0)
403 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
404 return -1;
406 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
407 return -1;
409 if (priority > priority_max)
410 priority = priority_max;
411 else if (priority < priority_min)
412 priority = priority_min;
413 params.sched_priority = priority;
416 * The solaris 7 and several other man pages incorrectly state that
417 * this should be a pointer to policy but pthread.h is universally
418 * at odds with this.
420 if (__gthrw_(pthread_setschedparam) (thread_id, policy, &params) == 0)
421 return 0;
423 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
424 #endif /* _POSIX_PRIORITY_SCHEDULING */
425 return -1;
429 /* Return the current thread's priority. */
430 static inline int
431 __gthread_objc_thread_get_priority (void)
433 #ifdef _POSIX_PRIORITY_SCHEDULING
434 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
435 if (__gthread_active_p ())
437 int policy;
438 struct sched_param params;
440 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, &params) == 0)
441 return params.sched_priority;
442 else
443 return -1;
445 else
446 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
447 #endif /* _POSIX_PRIORITY_SCHEDULING */
448 return OBJC_THREAD_INTERACTIVE_PRIORITY;
451 /* Yield our process time to another thread. */
452 static inline void
453 __gthread_objc_thread_yield (void)
455 if (__gthread_active_p ())
456 __gthrw_(sched_yield) ();
459 /* Terminate the current thread. */
460 static inline int
461 __gthread_objc_thread_exit (void)
463 if (__gthread_active_p ())
464 /* exit the thread */
465 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
467 /* Failed if we reached here */
468 return -1;
471 /* Returns an integer value which uniquely describes a thread. */
472 static inline objc_thread_t
473 __gthread_objc_thread_id (void)
475 if (__gthread_active_p ())
476 return (objc_thread_t) __gthrw_(pthread_self) ();
477 else
478 return (objc_thread_t) 1;
481 /* Sets the thread's local storage pointer. */
482 static inline int
483 __gthread_objc_thread_set_data (void *value)
485 if (__gthread_active_p ())
486 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
487 else
489 thread_local_storage = value;
490 return 0;
494 /* Returns the thread's local storage pointer. */
495 static inline void *
496 __gthread_objc_thread_get_data (void)
498 if (__gthread_active_p ())
499 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
500 else
501 return thread_local_storage;
504 /* Backend mutex functions */
506 /* Allocate a mutex. */
507 static inline int
508 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
510 if (__gthread_active_p ())
512 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
514 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
516 objc_free (mutex->backend);
517 mutex->backend = NULL;
518 return -1;
522 return 0;
525 /* Deallocate a mutex. */
526 static inline int
527 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
529 if (__gthread_active_p ())
531 int count;
534 * Posix Threads specifically require that the thread be unlocked
535 * for __gthrw_(pthread_mutex_destroy) to work.
540 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
541 if (count < 0)
542 return -1;
544 while (count);
546 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
547 return -1;
549 objc_free (mutex->backend);
550 mutex->backend = NULL;
552 return 0;
555 /* Grab a lock on a mutex. */
556 static inline int
557 __gthread_objc_mutex_lock (objc_mutex_t mutex)
559 if (__gthread_active_p ()
560 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
562 return -1;
565 return 0;
568 /* Try to grab a lock on a mutex. */
569 static inline int
570 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
572 if (__gthread_active_p ()
573 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
575 return -1;
578 return 0;
581 /* Unlock the mutex */
582 static inline int
583 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
585 if (__gthread_active_p ()
586 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
588 return -1;
591 return 0;
594 /* Backend condition mutex functions */
596 /* Allocate a condition. */
597 static inline int
598 __gthread_objc_condition_allocate (objc_condition_t condition)
600 if (__gthread_active_p ())
602 condition->backend = objc_malloc (sizeof (pthread_cond_t));
604 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
606 objc_free (condition->backend);
607 condition->backend = NULL;
608 return -1;
612 return 0;
615 /* Deallocate a condition. */
616 static inline int
617 __gthread_objc_condition_deallocate (objc_condition_t condition)
619 if (__gthread_active_p ())
621 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
622 return -1;
624 objc_free (condition->backend);
625 condition->backend = NULL;
627 return 0;
630 /* Wait on the condition */
631 static inline int
632 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
634 if (__gthread_active_p ())
635 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
636 (pthread_mutex_t *) mutex->backend);
637 else
638 return 0;
641 /* Wake up all threads waiting on this condition. */
642 static inline int
643 __gthread_objc_condition_broadcast (objc_condition_t condition)
645 if (__gthread_active_p ())
646 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
647 else
648 return 0;
651 /* Wake up one thread waiting on this condition. */
652 static inline int
653 __gthread_objc_condition_signal (objc_condition_t condition)
655 if (__gthread_active_p ())
656 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
657 else
658 return 0;
661 #else /* _LIBOBJC */
663 static inline int
664 __gthread_create (__gthread_t *__threadid, void *(*__func) (void*),
665 void *__args)
667 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
670 static inline int
671 __gthread_join (__gthread_t __threadid, void **__value_ptr)
673 return __gthrw_(pthread_join) (__threadid, __value_ptr);
676 static inline int
677 __gthread_detach (__gthread_t __threadid)
679 return __gthrw_(pthread_detach) (__threadid);
682 static inline int
683 __gthread_equal (__gthread_t __t1, __gthread_t __t2)
685 return __gthrw_(pthread_equal) (__t1, __t2);
688 static inline __gthread_t
689 __gthread_self (void)
691 return __gthrw_(pthread_self) ();
694 static inline int
695 __gthread_yield (void)
697 return __gthrw_(sched_yield) ();
700 static inline int
701 __gthread_once (__gthread_once_t *__once, void (*__func) (void))
703 if (__gthread_active_p ())
704 return __gthrw_(pthread_once) (__once, __func);
705 else
706 return -1;
709 static inline int
710 __gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
712 return __gthrw_(pthread_key_create) (__key, __dtor);
715 static inline int
716 __gthread_key_delete (__gthread_key_t __key)
718 return __gthrw_(pthread_key_delete) (__key);
721 static inline void *
722 __gthread_getspecific (__gthread_key_t __key)
724 return __gthrw_(pthread_getspecific) (__key);
727 static inline int
728 __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
730 return __gthrw_(pthread_setspecific) (__key, __ptr);
733 static inline int
734 __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
736 if (__gthread_active_p ())
737 return __gthrw_(pthread_mutex_destroy) (__mutex);
738 else
739 return 0;
742 static inline int
743 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
745 if (__gthread_active_p ())
746 return __gthrw_(pthread_mutex_lock) (__mutex);
747 else
748 return 0;
751 static inline int
752 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
754 if (__gthread_active_p ())
755 return __gthrw_(pthread_mutex_trylock) (__mutex);
756 else
757 return 0;
760 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
761 static inline int
762 __gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
763 const __gthread_time_t *__abs_timeout)
765 if (__gthread_active_p ())
766 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
767 else
768 return 0;
770 #endif
772 static inline int
773 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
775 if (__gthread_active_p ())
776 return __gthrw_(pthread_mutex_unlock) (__mutex);
777 else
778 return 0;
781 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
782 static inline int
783 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
785 if (__gthread_active_p ())
787 pthread_mutexattr_t __attr;
788 int __r;
790 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
791 if (!__r)
792 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
793 PTHREAD_MUTEX_RECURSIVE);
794 if (!__r)
795 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
796 if (!__r)
797 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
798 return __r;
800 return 0;
802 #endif
804 static inline int
805 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
807 return __gthread_mutex_lock (__mutex);
810 static inline int
811 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
813 return __gthread_mutex_trylock (__mutex);
816 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
817 static inline int
818 __gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
819 const __gthread_time_t *__abs_timeout)
821 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
823 #endif
825 static inline int
826 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
828 return __gthread_mutex_unlock (__mutex);
831 static inline int
832 __gthread_cond_broadcast (__gthread_cond_t *__cond)
834 return __gthrw_(pthread_cond_broadcast) (__cond);
837 static inline int
838 __gthread_cond_signal (__gthread_cond_t *__cond)
840 return __gthrw_(pthread_cond_signal) (__cond);
843 static inline int
844 __gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
846 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
849 static inline int
850 __gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
851 const __gthread_time_t *__abs_timeout)
853 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
856 static inline int
857 __gthread_cond_wait_recursive (__gthread_cond_t *__cond,
858 __gthread_recursive_mutex_t *__mutex)
860 return __gthread_cond_wait (__cond, __mutex);
863 static inline int
864 __gthread_cond_timedwait_recursive (__gthread_cond_t *__cond,
865 __gthread_recursive_mutex_t *__mutex,
866 const __gthread_time_t *__abs_timeout)
868 return __gthread_cond_timedwait (__cond, __mutex, __abs_timeout);
871 static inline int
872 __gthread_cond_destroy (__gthread_cond_t* __cond)
874 return __gthrw_(pthread_cond_destroy) (__cond);
877 #endif /* _LIBOBJC */
879 #endif /* ! GCC_GTHR_POSIX_H */