]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgcc/gthr-posix.h
Remove obsolete Tru64 UNIX V5.1B support
[thirdparty/gcc.git] / libgcc / gthr-posix.h
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.
5
6 This file is part of GCC.
7
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.
12
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.
17
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.
21
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/>. */
26
27 #ifndef GCC_GTHR_POSIX_H
28 #define GCC_GTHR_POSIX_H
29
30 /* POSIX threads specific definitions.
31 Easy, since the interface is just one-to-one mapping. */
32
33 #define __GTHREADS 1
34 #define __GTHREADS_CXX0X 1
35
36 #include <pthread.h>
37
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
47
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;
55
56 /* POSIX like conditional variables are supported. Please look at comments
57 in gthr.h for details. */
58 #define __GTHREAD_HAS_COND 1
59
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}
72
73 #ifdef _GTHREAD_USE_MUTEX_INIT_FUNC
74 # undef __GTHREAD_MUTEX_INIT
75 # define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
76 #endif
77 #ifdef _GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC
78 # undef __GTHREAD_RECURSIVE_MUTEX_INIT
79 # undef __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION
80 # define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
81 #endif
82 #ifdef _GTHREAD_USE_COND_INIT_FUNC
83 # undef __GTHREAD_COND_INIT
84 # define __GTHREAD_COND_INIT_FUNCTION __gthread_cond_init_function
85 #endif
86
87 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
88 # ifndef __gthrw_pragma
89 # define __gthrw_pragma(pragma)
90 # endif
91 # define __gthrw2(name,name2,type) \
92 static __typeof(type) name __attribute__ ((__weakref__(#name2))); \
93 __gthrw_pragma(weak type)
94 # define __gthrw_(name) __gthrw_ ## name
95 #else
96 # define __gthrw2(name,name2,type)
97 # define __gthrw_(name) name
98 #endif
99
100 /* Typically, __gthrw_foo is a weak reference to symbol foo. */
101 #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
102
103 __gthrw(pthread_once)
104 __gthrw(pthread_getspecific)
105 __gthrw(pthread_setspecific)
106
107 __gthrw(pthread_create)
108 __gthrw(pthread_join)
109 __gthrw(pthread_equal)
110 __gthrw(pthread_self)
111 __gthrw(pthread_detach)
112 #ifndef __BIONIC__
113 __gthrw(pthread_cancel)
114 #endif
115 __gthrw(sched_yield)
116
117 __gthrw(pthread_mutex_lock)
118 __gthrw(pthread_mutex_trylock)
119 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
120 __gthrw(pthread_mutex_timedlock)
121 #endif
122 __gthrw(pthread_mutex_unlock)
123 __gthrw(pthread_mutex_init)
124 __gthrw(pthread_mutex_destroy)
125
126 __gthrw(pthread_cond_init)
127 __gthrw(pthread_cond_broadcast)
128 __gthrw(pthread_cond_signal)
129 __gthrw(pthread_cond_wait)
130 __gthrw(pthread_cond_timedwait)
131 __gthrw(pthread_cond_destroy)
132
133 __gthrw(pthread_key_create)
134 __gthrw(pthread_key_delete)
135 __gthrw(pthread_mutexattr_init)
136 __gthrw(pthread_mutexattr_settype)
137 __gthrw(pthread_mutexattr_destroy)
138
139
140 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
141 /* Objective-C. */
142 __gthrw(pthread_exit)
143 #ifdef _POSIX_PRIORITY_SCHEDULING
144 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
145 __gthrw(sched_get_priority_max)
146 __gthrw(sched_get_priority_min)
147 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
148 #endif /* _POSIX_PRIORITY_SCHEDULING */
149 __gthrw(pthread_attr_destroy)
150 __gthrw(pthread_attr_init)
151 __gthrw(pthread_attr_setdetachstate)
152 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
153 __gthrw(pthread_getschedparam)
154 __gthrw(pthread_setschedparam)
155 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
156 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
157
158 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
159
160 /* On Solaris 2.6 up to 9, the libc exposes a POSIX threads interface even if
161 -pthreads is not specified. The functions are dummies and most return an
162 error value. However pthread_once returns 0 without invoking the routine
163 it is passed so we cannot pretend that the interface is active if -pthreads
164 is not specified. On Solaris 2.5.1, the interface is not exposed at all so
165 we need to play the usual game with weak symbols. On Solaris 10 and up, a
166 working interface is always exposed. On FreeBSD 6 and later, libc also
167 exposes a dummy POSIX threads interface, similar to what Solaris 2.6 up
168 to 9 does. FreeBSD >= 700014 even provides a pthread_cancel stub in libc,
169 which means the alternate __gthread_active_p below cannot be used there. */
170
171 #if defined(__FreeBSD__) || (defined(__sun) && defined(__svr4__))
172
173 static volatile int __gthread_active = -1;
174
175 static void
176 __gthread_trigger (void)
177 {
178 __gthread_active = 1;
179 }
180
181 static inline int
182 __gthread_active_p (void)
183 {
184 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
185 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
186
187 /* Avoid reading __gthread_active twice on the main code path. */
188 int __gthread_active_latest_value = __gthread_active;
189
190 /* This test is not protected to avoid taking a lock on the main code
191 path so every update of __gthread_active in a threaded program must
192 be atomic with regard to the result of the test. */
193 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
194 {
195 if (__gthrw_(pthread_once))
196 {
197 /* If this really is a threaded program, then we must ensure that
198 __gthread_active has been set to 1 before exiting this block. */
199 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
200 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
201 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
202 }
203
204 /* Make sure we'll never enter this block again. */
205 if (__gthread_active < 0)
206 __gthread_active = 0;
207
208 __gthread_active_latest_value = __gthread_active;
209 }
210
211 return __gthread_active_latest_value != 0;
212 }
213
214 #else /* neither FreeBSD nor Solaris */
215
216 static inline int
217 __gthread_active_p (void)
218 {
219 /* Android's C library does not provide pthread_cancel, check for
220 `pthread_create' instead. */
221 #ifndef __BIONIC__
222 static void *const __gthread_active_ptr
223 = __extension__ (void *) &__gthrw_(pthread_cancel);
224 #else
225 static void *const __gthread_active_ptr
226 = __extension__ (void *) &__gthrw_(pthread_create);
227 #endif
228 return __gthread_active_ptr != 0;
229 }
230
231 #endif /* FreeBSD or Solaris */
232
233 #else /* not SUPPORTS_WEAK */
234
235 /* Similar to Solaris, HP-UX 11 for PA-RISC provides stubs for pthread
236 calls in shared flavors of the HP-UX C library. Most of the stubs
237 have no functionality. The details are described in the "libc cumulative
238 patch" for each subversion of HP-UX 11. There are two special interfaces
239 provided for checking whether an application is linked to a shared pthread
240 library or not. However, these interfaces aren't available in early
241 libpthread libraries. We also need a test that works for archive
242 libraries. We can't use pthread_once as some libc versions call the
243 init function. We also can't use pthread_create or pthread_attr_init
244 as these create a thread and thereby prevent changing the default stack
245 size. The function pthread_default_stacksize_np is available in both
246 the archive and shared versions of libpthread. It can be used to
247 determine the default pthread stack size. There is a stub in some
248 shared libc versions which returns a zero size if pthreads are not
249 active. We provide an equivalent stub to handle cases where libc
250 doesn't provide one. */
251
252 #if defined(__hppa__) && defined(__hpux__)
253
254 static volatile int __gthread_active = -1;
255
256 static inline int
257 __gthread_active_p (void)
258 {
259 /* Avoid reading __gthread_active twice on the main code path. */
260 int __gthread_active_latest_value = __gthread_active;
261 size_t __s;
262
263 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
264 {
265 pthread_default_stacksize_np (0, &__s);
266 __gthread_active = __s ? 1 : 0;
267 __gthread_active_latest_value = __gthread_active;
268 }
269
270 return __gthread_active_latest_value != 0;
271 }
272
273 #else /* not hppa-hpux */
274
275 static inline int
276 __gthread_active_p (void)
277 {
278 return 1;
279 }
280
281 #endif /* hppa-hpux */
282
283 #endif /* SUPPORTS_WEAK */
284
285 #ifdef _LIBOBJC
286
287 /* This is the config.h file in libobjc/ */
288 #include <config.h>
289
290 #ifdef HAVE_SCHED_H
291 # include <sched.h>
292 #endif
293
294 /* Key structure for maintaining thread specific storage */
295 static pthread_key_t _objc_thread_storage;
296 static pthread_attr_t _objc_thread_attribs;
297
298 /* Thread local storage for a single thread */
299 static void *thread_local_storage = NULL;
300
301 /* Backend initialization functions */
302
303 /* Initialize the threads subsystem. */
304 static inline int
305 __gthread_objc_init_thread_system (void)
306 {
307 if (__gthread_active_p ())
308 {
309 /* Initialize the thread storage key. */
310 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
311 {
312 /* The normal default detach state for threads is
313 * PTHREAD_CREATE_JOINABLE which causes threads to not die
314 * when you think they should. */
315 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
316 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
317 PTHREAD_CREATE_DETACHED) == 0)
318 return 0;
319 }
320 }
321
322 return -1;
323 }
324
325 /* Close the threads subsystem. */
326 static inline int
327 __gthread_objc_close_thread_system (void)
328 {
329 if (__gthread_active_p ()
330 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
331 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
332 return 0;
333
334 return -1;
335 }
336
337 /* Backend thread functions */
338
339 /* Create a new thread of execution. */
340 static inline objc_thread_t
341 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
342 {
343 objc_thread_t thread_id;
344 pthread_t new_thread_handle;
345
346 if (!__gthread_active_p ())
347 return NULL;
348
349 if (!(__gthrw_(pthread_create) (&new_thread_handle, &_objc_thread_attribs,
350 (void *) func, arg)))
351 thread_id = (objc_thread_t) new_thread_handle;
352 else
353 thread_id = NULL;
354
355 return thread_id;
356 }
357
358 /* Set the current thread's priority. */
359 static inline int
360 __gthread_objc_thread_set_priority (int priority)
361 {
362 if (!__gthread_active_p ())
363 return -1;
364 else
365 {
366 #ifdef _POSIX_PRIORITY_SCHEDULING
367 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
368 pthread_t thread_id = __gthrw_(pthread_self) ();
369 int policy;
370 struct sched_param params;
371 int priority_min, priority_max;
372
373 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, &params) == 0)
374 {
375 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
376 return -1;
377
378 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
379 return -1;
380
381 if (priority > priority_max)
382 priority = priority_max;
383 else if (priority < priority_min)
384 priority = priority_min;
385 params.sched_priority = priority;
386
387 /*
388 * The solaris 7 and several other man pages incorrectly state that
389 * this should be a pointer to policy but pthread.h is universally
390 * at odds with this.
391 */
392 if (__gthrw_(pthread_setschedparam) (thread_id, policy, &params) == 0)
393 return 0;
394 }
395 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
396 #endif /* _POSIX_PRIORITY_SCHEDULING */
397 return -1;
398 }
399 }
400
401 /* Return the current thread's priority. */
402 static inline int
403 __gthread_objc_thread_get_priority (void)
404 {
405 #ifdef _POSIX_PRIORITY_SCHEDULING
406 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
407 if (__gthread_active_p ())
408 {
409 int policy;
410 struct sched_param params;
411
412 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, &params) == 0)
413 return params.sched_priority;
414 else
415 return -1;
416 }
417 else
418 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
419 #endif /* _POSIX_PRIORITY_SCHEDULING */
420 return OBJC_THREAD_INTERACTIVE_PRIORITY;
421 }
422
423 /* Yield our process time to another thread. */
424 static inline void
425 __gthread_objc_thread_yield (void)
426 {
427 if (__gthread_active_p ())
428 __gthrw_(sched_yield) ();
429 }
430
431 /* Terminate the current thread. */
432 static inline int
433 __gthread_objc_thread_exit (void)
434 {
435 if (__gthread_active_p ())
436 /* exit the thread */
437 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
438
439 /* Failed if we reached here */
440 return -1;
441 }
442
443 /* Returns an integer value which uniquely describes a thread. */
444 static inline objc_thread_t
445 __gthread_objc_thread_id (void)
446 {
447 if (__gthread_active_p ())
448 return (objc_thread_t) __gthrw_(pthread_self) ();
449 else
450 return (objc_thread_t) 1;
451 }
452
453 /* Sets the thread's local storage pointer. */
454 static inline int
455 __gthread_objc_thread_set_data (void *value)
456 {
457 if (__gthread_active_p ())
458 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
459 else
460 {
461 thread_local_storage = value;
462 return 0;
463 }
464 }
465
466 /* Returns the thread's local storage pointer. */
467 static inline void *
468 __gthread_objc_thread_get_data (void)
469 {
470 if (__gthread_active_p ())
471 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
472 else
473 return thread_local_storage;
474 }
475
476 /* Backend mutex functions */
477
478 /* Allocate a mutex. */
479 static inline int
480 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
481 {
482 if (__gthread_active_p ())
483 {
484 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
485
486 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
487 {
488 objc_free (mutex->backend);
489 mutex->backend = NULL;
490 return -1;
491 }
492 }
493
494 return 0;
495 }
496
497 /* Deallocate a mutex. */
498 static inline int
499 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
500 {
501 if (__gthread_active_p ())
502 {
503 int count;
504
505 /*
506 * Posix Threads specifically require that the thread be unlocked
507 * for __gthrw_(pthread_mutex_destroy) to work.
508 */
509
510 do
511 {
512 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
513 if (count < 0)
514 return -1;
515 }
516 while (count);
517
518 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
519 return -1;
520
521 objc_free (mutex->backend);
522 mutex->backend = NULL;
523 }
524 return 0;
525 }
526
527 /* Grab a lock on a mutex. */
528 static inline int
529 __gthread_objc_mutex_lock (objc_mutex_t mutex)
530 {
531 if (__gthread_active_p ()
532 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
533 {
534 return -1;
535 }
536
537 return 0;
538 }
539
540 /* Try to grab a lock on a mutex. */
541 static inline int
542 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
543 {
544 if (__gthread_active_p ()
545 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
546 {
547 return -1;
548 }
549
550 return 0;
551 }
552
553 /* Unlock the mutex */
554 static inline int
555 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
556 {
557 if (__gthread_active_p ()
558 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
559 {
560 return -1;
561 }
562
563 return 0;
564 }
565
566 /* Backend condition mutex functions */
567
568 /* Allocate a condition. */
569 static inline int
570 __gthread_objc_condition_allocate (objc_condition_t condition)
571 {
572 if (__gthread_active_p ())
573 {
574 condition->backend = objc_malloc (sizeof (pthread_cond_t));
575
576 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
577 {
578 objc_free (condition->backend);
579 condition->backend = NULL;
580 return -1;
581 }
582 }
583
584 return 0;
585 }
586
587 /* Deallocate a condition. */
588 static inline int
589 __gthread_objc_condition_deallocate (objc_condition_t condition)
590 {
591 if (__gthread_active_p ())
592 {
593 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
594 return -1;
595
596 objc_free (condition->backend);
597 condition->backend = NULL;
598 }
599 return 0;
600 }
601
602 /* Wait on the condition */
603 static inline int
604 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
605 {
606 if (__gthread_active_p ())
607 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
608 (pthread_mutex_t *) mutex->backend);
609 else
610 return 0;
611 }
612
613 /* Wake up all threads waiting on this condition. */
614 static inline int
615 __gthread_objc_condition_broadcast (objc_condition_t condition)
616 {
617 if (__gthread_active_p ())
618 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
619 else
620 return 0;
621 }
622
623 /* Wake up one thread waiting on this condition. */
624 static inline int
625 __gthread_objc_condition_signal (objc_condition_t condition)
626 {
627 if (__gthread_active_p ())
628 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
629 else
630 return 0;
631 }
632
633 #else /* _LIBOBJC */
634
635 static inline int
636 __gthread_create (__gthread_t *__threadid, void *(*__func) (void*),
637 void *__args)
638 {
639 return __gthrw_(pthread_create) (__threadid, NULL, __func, __args);
640 }
641
642 static inline int
643 __gthread_join (__gthread_t __threadid, void **__value_ptr)
644 {
645 return __gthrw_(pthread_join) (__threadid, __value_ptr);
646 }
647
648 static inline int
649 __gthread_detach (__gthread_t __threadid)
650 {
651 return __gthrw_(pthread_detach) (__threadid);
652 }
653
654 static inline int
655 __gthread_equal (__gthread_t __t1, __gthread_t __t2)
656 {
657 return __gthrw_(pthread_equal) (__t1, __t2);
658 }
659
660 static inline __gthread_t
661 __gthread_self (void)
662 {
663 return __gthrw_(pthread_self) ();
664 }
665
666 static inline int
667 __gthread_yield (void)
668 {
669 return __gthrw_(sched_yield) ();
670 }
671
672 static inline int
673 __gthread_once (__gthread_once_t *__once, void (*__func) (void))
674 {
675 if (__gthread_active_p ())
676 return __gthrw_(pthread_once) (__once, __func);
677 else
678 return -1;
679 }
680
681 static inline int
682 __gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
683 {
684 return __gthrw_(pthread_key_create) (__key, __dtor);
685 }
686
687 static inline int
688 __gthread_key_delete (__gthread_key_t __key)
689 {
690 return __gthrw_(pthread_key_delete) (__key);
691 }
692
693 static inline void *
694 __gthread_getspecific (__gthread_key_t __key)
695 {
696 return __gthrw_(pthread_getspecific) (__key);
697 }
698
699 static inline int
700 __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
701 {
702 return __gthrw_(pthread_setspecific) (__key, __ptr);
703 }
704
705 #ifdef _GTHREAD_USE_MUTEX_INIT_FUNC
706 static inline void
707 __gthread_mutex_init_function (__gthread_mutex_t *__mutex)
708 {
709 if (__gthread_active_p ())
710 __gthrw_(pthread_mutex_init) (__mutex, NULL);
711 }
712 #endif
713
714 static inline int
715 __gthread_mutex_init_function (__gthread_mutex_t *__mutex)
716 {
717 if (__gthread_active_p ())
718 return __gthrw_(pthread_mutex_init) (__mutex, NULL);
719 return 0;
720 }
721
722 static inline int
723 __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
724 {
725 if (__gthread_active_p ())
726 return __gthrw_(pthread_mutex_destroy) (__mutex);
727 else
728 return 0;
729 }
730
731 static inline int
732 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
733 {
734 if (__gthread_active_p ())
735 return __gthrw_(pthread_mutex_lock) (__mutex);
736 else
737 return 0;
738 }
739
740 static inline int
741 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
742 {
743 if (__gthread_active_p ())
744 return __gthrw_(pthread_mutex_trylock) (__mutex);
745 else
746 return 0;
747 }
748
749 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
750 static inline int
751 __gthread_mutex_timedlock (__gthread_mutex_t *__mutex,
752 const __gthread_time_t *__abs_timeout)
753 {
754 if (__gthread_active_p ())
755 return __gthrw_(pthread_mutex_timedlock) (__mutex, __abs_timeout);
756 else
757 return 0;
758 }
759 #endif
760
761 static inline int
762 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
763 {
764 if (__gthread_active_p ())
765 return __gthrw_(pthread_mutex_unlock) (__mutex);
766 else
767 return 0;
768 }
769
770 #if !defined( PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) \
771 || defined(_GTHREAD_USE_RECURSIVE_MUTEX_INIT_FUNC)
772 static inline int
773 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
774 {
775 if (__gthread_active_p ())
776 {
777 pthread_mutexattr_t __attr;
778 int __r;
779
780 __r = __gthrw_(pthread_mutexattr_init) (&__attr);
781 if (!__r)
782 __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
783 PTHREAD_MUTEX_RECURSIVE);
784 if (!__r)
785 __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
786 if (!__r)
787 __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
788 return __r;
789 }
790 return 0;
791 }
792 #endif
793
794 static inline int
795 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
796 {
797 return __gthread_mutex_lock (__mutex);
798 }
799
800 static inline int
801 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
802 {
803 return __gthread_mutex_trylock (__mutex);
804 }
805
806 #if _GTHREAD_USE_MUTEX_TIMEDLOCK
807 static inline int
808 __gthread_recursive_mutex_timedlock (__gthread_recursive_mutex_t *__mutex,
809 const __gthread_time_t *__abs_timeout)
810 {
811 return __gthread_mutex_timedlock (__mutex, __abs_timeout);
812 }
813 #endif
814
815 static inline int
816 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
817 {
818 return __gthread_mutex_unlock (__mutex);
819 }
820
821 #ifdef _GTHREAD_USE_COND_INIT_FUNC
822 static inline void
823 __gthread_cond_init_function (__gthread_cond_t *__cond)
824 {
825 if (__gthread_active_p ())
826 __gthrw_(pthread_cond_init) (__cond, NULL);
827 }
828 #endif
829
830 static inline int
831 __gthread_cond_broadcast (__gthread_cond_t *__cond)
832 {
833 return __gthrw_(pthread_cond_broadcast) (__cond);
834 }
835
836 static inline int
837 __gthread_cond_signal (__gthread_cond_t *__cond)
838 {
839 return __gthrw_(pthread_cond_signal) (__cond);
840 }
841
842 static inline int
843 __gthread_cond_wait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex)
844 {
845 return __gthrw_(pthread_cond_wait) (__cond, __mutex);
846 }
847
848 static inline int
849 __gthread_cond_timedwait (__gthread_cond_t *__cond, __gthread_mutex_t *__mutex,
850 const __gthread_time_t *__abs_timeout)
851 {
852 return __gthrw_(pthread_cond_timedwait) (__cond, __mutex, __abs_timeout);
853 }
854
855 static inline int
856 __gthread_cond_wait_recursive (__gthread_cond_t *__cond,
857 __gthread_recursive_mutex_t *__mutex)
858 {
859 return __gthread_cond_wait (__cond, __mutex);
860 }
861
862 static inline int
863 __gthread_cond_timedwait_recursive (__gthread_cond_t *__cond,
864 __gthread_recursive_mutex_t *__mutex,
865 const __gthread_time_t *__abs_timeout)
866 {
867 return __gthread_cond_timedwait (__cond, __mutex, __abs_timeout);
868 }
869
870 static inline int
871 __gthread_cond_destroy (__gthread_cond_t* __cond)
872 {
873 return __gthrw_(pthread_cond_destroy) (__cond);
874 }
875
876 #endif /* _LIBOBJC */
877
878 #endif /* ! GCC_GTHR_POSIX_H */