1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22 /* As a special exception, if you link this library with other files,
23 some of which are compiled with GCC, to produce an executable,
24 this library does not by itself cause the resulting executable
25 to be covered by the GNU General Public License.
26 This exception does not however invalidate any other reasons why
27 the executable file might be covered by the GNU General Public License. */
29 #ifndef GCC_GTHR_POSIX_H
30 #define GCC_GTHR_POSIX_H
32 /* POSIX threads specific definitions.
33 Easy, since the interface is just one-to-one mapping. */
37 /* Some implementations of <pthread.h> require this to be defined. */
45 typedef pthread_key_t __gthread_key_t;
46 typedef pthread_once_t __gthread_once_t;
47 typedef pthread_mutex_t __gthread_mutex_t;
48 typedef pthread_cond_t __gthread_cond_t;
50 /* POSIX like conditional variables are supported. Please look at comments
51 in gthr.h for details. */
52 #define __GTHREAD_HAS_COND 1
57 pthread_mutex_t actual;
58 } __gthread_recursive_mutex_t;
60 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
61 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
62 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
63 #define __GTHREAD_COND_INIT PTHREAD_COND_INITIALIZER
65 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
66 # define __gthrw(name) \
67 static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)));
68 # define __gthrw_(name) __gthrw_ ## name
70 # define __gthrw(name)
71 # define __gthrw_(name) name
75 __gthrw(pthread_key_create)
76 __gthrw(pthread_key_delete)
77 __gthrw(pthread_getspecific)
78 __gthrw(pthread_setspecific)
79 __gthrw(pthread_create)
80 __gthrw(pthread_cancel)
83 __gthrw(pthread_mutex_init)
84 __gthrw(pthread_mutex_destroy)
85 __gthrw(pthread_mutex_lock)
86 __gthrw(pthread_mutex_trylock)
87 __gthrw(pthread_mutex_unlock)
88 __gthrw(pthread_mutexattr_init)
89 __gthrw(pthread_mutexattr_destroy)
91 __gthrw(pthread_cond_broadcast)
92 __gthrw(pthread_cond_wait)
94 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
96 __gthrw(pthread_cond_destroy)
97 __gthrw(pthread_cond_init)
98 __gthrw(pthread_cond_signal)
100 __gthrw(pthread_mutex_destroy)
101 #ifdef _POSIX_PRIORITY_SCHEDULING
102 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
103 __gthrw(sched_get_priority_max)
104 __gthrw(sched_get_priority_min)
105 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
106 #endif /* _POSIX_PRIORITY_SCHEDULING */
108 __gthrw(pthread_attr_destroy)
109 __gthrw(pthread_attr_init)
110 __gthrw(pthread_attr_setdetachstate)
111 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
112 __gthrw(pthread_getschedparam)
113 __gthrw(pthread_setschedparam)
114 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
115 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
117 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
119 /* On Solaris 2.6 up to 9, the libc exposes a POSIX threads interface even if
120 -pthreads is not specified. The functions are dummies and most return an
121 error value. However pthread_once returns 0 without invoking the routine
122 it is passed so we cannot pretend that the interface is active if -pthreads
123 is not specified. On Solaris 2.5.1, the interface is not exposed at all so
124 we need to play the usual game with weak symbols. On Solaris 10 and up, a
125 working interface is always exposed. */
127 #if defined(__sun) && defined(__svr4__)
129 static volatile int __gthread_active = -1;
132 __gthread_trigger (void)
134 __gthread_active = 1;
138 __gthread_active_p (void)
140 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
141 static pthread_once_t __gthread_active_once = PTHREAD_ONCE_INIT;
143 /* Avoid reading __gthread_active twice on the main code path. */
144 int __gthread_active_latest_value = __gthread_active;
146 /* This test is not protected to avoid taking a lock on the main code
147 path so every update of __gthread_active in a threaded program must
148 be atomic with regard to the result of the test. */
149 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
151 if (__gthrw_(pthread_once))
153 /* If this really is a threaded program, then we must ensure that
154 __gthread_active has been set to 1 before exiting this block. */
155 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
156 __gthrw_(pthread_once) (&__gthread_active_once, __gthread_trigger);
157 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
160 /* Make sure we'll never enter this block again. */
161 if (__gthread_active < 0)
162 __gthread_active = 0;
164 __gthread_active_latest_value = __gthread_active;
167 return __gthread_active_latest_value != 0;
170 #else /* not Solaris */
173 __gthread_active_p (void)
175 static void *const __gthread_active_ptr
176 = __extension__ (void *) &__gthrw_(pthread_cancel);
177 return __gthread_active_ptr != 0;
182 #else /* not SUPPORTS_WEAK */
184 /* Similar to Solaris, HP-UX 11 for PA-RISC provides stubs for pthread
185 calls in shared flavors of the HP-UX C library. Most of the stubs
186 have no functionality. The details are described in the "libc cumulative
187 patch" for each subversion of HP-UX 11. There are two special interfaces
188 provided for checking whether an application is linked to a pthread
189 library or not. However, these interfaces aren't available in early
190 libc versions. We also can't use pthread_once as some libc versions
191 call the init function. So, we use pthread_create to check whether it
192 is possible to create a thread or not. The stub implementation returns
193 the error number ENOSYS. */
195 #if defined(__hppa__) && defined(__hpux__)
199 static volatile int __gthread_active = -1;
202 __gthread_start (void *arg __attribute__((unused)))
207 static void __gthread_active_init (void) __attribute__((noinline));
209 __gthread_active_init (void)
211 static pthread_mutex_t __gthread_active_mutex = PTHREAD_MUTEX_INITIALIZER;
216 __gthrw_(pthread_mutex_lock) (&__gthread_active_mutex);
217 if (__gthread_active < 0)
219 __gthrw_(pthread_attr_init) (&a);
220 __gthrw_(pthread_attr_setdetachstate) (&a, PTHREAD_CREATE_DETACHED);
221 result = __gthrw_(pthread_create) (&t, &a, __gthread_start, NULL);
222 if (result != ENOSYS)
223 __gthread_active = 1;
225 __gthread_active = 0;
226 __gthrw_(pthread_attr_destroy) (&a);
228 __gthrw_(pthread_mutex_unlock) (&__gthread_active_mutex);
232 __gthread_active_p (void)
234 /* Avoid reading __gthread_active twice on the main code path. */
235 int __gthread_active_latest_value = __gthread_active;
237 /* This test is not protected to avoid taking a lock on the main code
238 path so every update of __gthread_active in a threaded program must
239 be atomic with regard to the result of the test. */
240 if (__builtin_expect (__gthread_active_latest_value < 0, 0))
242 __gthread_active_init ();
243 __gthread_active_latest_value = __gthread_active;
246 return __gthread_active_latest_value != 0;
249 #else /* not hppa-hpux */
252 __gthread_active_p (void)
257 #endif /* hppa-hpux */
259 #endif /* SUPPORTS_WEAK */
263 /* This is the config.h file in libobjc/ */
270 /* Key structure for maintaining thread specific storage */
271 static pthread_key_t _objc_thread_storage;
272 static pthread_attr_t _objc_thread_attribs;
274 /* Thread local storage for a single thread */
275 static void *thread_local_storage = NULL;
277 /* Backend initialization functions */
279 /* Initialize the threads subsystem. */
281 __gthread_objc_init_thread_system (void)
283 if (__gthread_active_p ())
285 /* Initialize the thread storage key. */
286 if (__gthrw_(pthread_key_create) (&_objc_thread_storage, NULL) == 0)
288 /* The normal default detach state for threads is
289 * PTHREAD_CREATE_JOINABLE which causes threads to not die
290 * when you think they should. */
291 if (__gthrw_(pthread_attr_init) (&_objc_thread_attribs) == 0
292 && __gthrw_(pthread_attr_setdetachstate) (&_objc_thread_attribs,
293 PTHREAD_CREATE_DETACHED) == 0)
301 /* Close the threads subsystem. */
303 __gthread_objc_close_thread_system (void)
305 if (__gthread_active_p ()
306 && __gthrw_(pthread_key_delete) (_objc_thread_storage) == 0
307 && __gthrw_(pthread_attr_destroy) (&_objc_thread_attribs) == 0)
313 /* Backend thread functions */
315 /* Create a new thread of execution. */
316 static inline objc_thread_t
317 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
319 objc_thread_t thread_id;
320 pthread_t new_thread_handle;
322 if (!__gthread_active_p ())
325 if (!(__gthrw_(pthread_create) (&new_thread_handle, NULL, (void *) func, arg)))
326 thread_id = (objc_thread_t) new_thread_handle;
333 /* Set the current thread's priority. */
335 __gthread_objc_thread_set_priority (int priority)
337 if (!__gthread_active_p ())
341 #ifdef _POSIX_PRIORITY_SCHEDULING
342 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
343 pthread_t thread_id = __gthrw_(pthread_self) ();
345 struct sched_param params;
346 int priority_min, priority_max;
348 if (__gthrw_(pthread_getschedparam) (thread_id, &policy, ¶ms) == 0)
350 if ((priority_max = __gthrw_(sched_get_priority_max) (policy)) == -1)
353 if ((priority_min = __gthrw_(sched_get_priority_min) (policy)) == -1)
356 if (priority > priority_max)
357 priority = priority_max;
358 else if (priority < priority_min)
359 priority = priority_min;
360 params.sched_priority = priority;
363 * The solaris 7 and several other man pages incorrectly state that
364 * this should be a pointer to policy but pthread.h is universally
367 if (__gthrw_(pthread_setschedparam) (thread_id, policy, ¶ms) == 0)
370 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
371 #endif /* _POSIX_PRIORITY_SCHEDULING */
376 /* Return the current thread's priority. */
378 __gthread_objc_thread_get_priority (void)
380 #ifdef _POSIX_PRIORITY_SCHEDULING
381 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
382 if (__gthread_active_p ())
385 struct sched_param params;
387 if (__gthrw_(pthread_getschedparam) (__gthrw_(pthread_self) (), &policy, ¶ms) == 0)
388 return params.sched_priority;
393 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
394 #endif /* _POSIX_PRIORITY_SCHEDULING */
395 return OBJC_THREAD_INTERACTIVE_PRIORITY;
398 /* Yield our process time to another thread. */
400 __gthread_objc_thread_yield (void)
402 if (__gthread_active_p ())
403 __gthrw_(sched_yield) ();
406 /* Terminate the current thread. */
408 __gthread_objc_thread_exit (void)
410 if (__gthread_active_p ())
411 /* exit the thread */
412 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
414 /* Failed if we reached here */
418 /* Returns an integer value which uniquely describes a thread. */
419 static inline objc_thread_t
420 __gthread_objc_thread_id (void)
422 if (__gthread_active_p ())
423 return (objc_thread_t) __gthrw_(pthread_self) ();
425 return (objc_thread_t) 1;
428 /* Sets the thread's local storage pointer. */
430 __gthread_objc_thread_set_data (void *value)
432 if (__gthread_active_p ())
433 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
436 thread_local_storage = value;
441 /* Returns the thread's local storage pointer. */
443 __gthread_objc_thread_get_data (void)
445 if (__gthread_active_p ())
446 return __gthrw_(pthread_getspecific) (_objc_thread_storage);
448 return thread_local_storage;
451 /* Backend mutex functions */
453 /* Allocate a mutex. */
455 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
457 if (__gthread_active_p ())
459 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
461 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend, NULL))
463 objc_free (mutex->backend);
464 mutex->backend = NULL;
472 /* Deallocate a mutex. */
474 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
476 if (__gthread_active_p ())
481 * Posix Threads specifically require that the thread be unlocked
482 * for __gthrw_(pthread_mutex_destroy) to work.
487 count = __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
493 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
496 objc_free (mutex->backend);
497 mutex->backend = NULL;
502 /* Grab a lock on a mutex. */
504 __gthread_objc_mutex_lock (objc_mutex_t mutex)
506 if (__gthread_active_p ()
507 && __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend) != 0)
515 /* Try to grab a lock on a mutex. */
517 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
519 if (__gthread_active_p ()
520 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 0)
528 /* Unlock the mutex */
530 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
532 if (__gthread_active_p ()
533 && __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend) != 0)
541 /* Backend condition mutex functions */
543 /* Allocate a condition. */
545 __gthread_objc_condition_allocate (objc_condition_t condition)
547 if (__gthread_active_p ())
549 condition->backend = objc_malloc (sizeof (pthread_cond_t));
551 if (__gthrw_(pthread_cond_init) ((pthread_cond_t *) condition->backend, NULL))
553 objc_free (condition->backend);
554 condition->backend = NULL;
562 /* Deallocate a condition. */
564 __gthread_objc_condition_deallocate (objc_condition_t condition)
566 if (__gthread_active_p ())
568 if (__gthrw_(pthread_cond_destroy) ((pthread_cond_t *) condition->backend))
571 objc_free (condition->backend);
572 condition->backend = NULL;
577 /* Wait on the condition */
579 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
581 if (__gthread_active_p ())
582 return __gthrw_(pthread_cond_wait) ((pthread_cond_t *) condition->backend,
583 (pthread_mutex_t *) mutex->backend);
588 /* Wake up all threads waiting on this condition. */
590 __gthread_objc_condition_broadcast (objc_condition_t condition)
592 if (__gthread_active_p ())
593 return __gthrw_(pthread_cond_broadcast) ((pthread_cond_t *) condition->backend);
598 /* Wake up one thread waiting on this condition. */
600 __gthread_objc_condition_signal (objc_condition_t condition)
602 if (__gthread_active_p ())
603 return __gthrw_(pthread_cond_signal) ((pthread_cond_t *) condition->backend);
611 __gthread_once (__gthread_once_t *once, void (*func) (void))
613 if (__gthread_active_p ())
614 return __gthrw_(pthread_once) (once, func);
620 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
622 return __gthrw_(pthread_key_create) (key, dtor);
626 __gthread_key_delete (__gthread_key_t key)
628 return __gthrw_(pthread_key_delete) (key);
632 __gthread_getspecific (__gthread_key_t key)
634 return __gthrw_(pthread_getspecific) (key);
638 __gthread_setspecific (__gthread_key_t key, const void *ptr)
640 return __gthrw_(pthread_setspecific) (key, ptr);
644 __gthread_mutex_destroy (__gthread_mutex_t *mutex)
646 if (__gthread_active_p ())
647 return __gthrw_(pthread_mutex_destroy) (mutex);
653 __gthread_mutex_lock (__gthread_mutex_t *mutex)
655 if (__gthread_active_p ())
656 return __gthrw_(pthread_mutex_lock) (mutex);
662 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
664 if (__gthread_active_p ())
665 return __gthrw_(pthread_mutex_trylock) (mutex);
671 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
673 if (__gthread_active_p ())
674 return __gthrw_(pthread_mutex_unlock) (mutex);
680 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
683 mutex->owner = (pthread_t) 0;
684 return __gthrw_(pthread_mutex_init) (&mutex->actual, NULL);
688 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
690 if (__gthread_active_p ())
692 pthread_t me = __gthrw_(pthread_self) ();
694 if (mutex->owner != me)
696 __gthrw_(pthread_mutex_lock) (&mutex->actual);
706 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
708 if (__gthread_active_p ())
710 pthread_t me = __gthrw_(pthread_self) ();
712 if (mutex->owner != me)
714 if (__gthrw_(pthread_mutex_trylock) (&mutex->actual))
725 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
727 if (__gthread_active_p ())
729 if (--mutex->depth == 0)
731 mutex->owner = (pthread_t) 0;
732 __gthrw_(pthread_mutex_unlock) (&mutex->actual);
739 __gthread_cond_broadcast (__gthread_cond_t *cond)
741 return __gthrw_(pthread_cond_broadcast) (cond);
745 __gthread_cond_wait (__gthread_cond_t *cond, __gthread_mutex_t *mutex)
747 return __gthrw_(pthread_cond_wait) (cond, mutex);
751 __gthread_cond_wait_recursive (__gthread_cond_t *cond,
752 __gthread_recursive_mutex_t *mutex)
754 return __gthrw_(pthread_cond_wait) (cond, mutex->actual);
757 #endif /* _LIBOBJC */
759 #endif /* ! GCC_GTHR_POSIX_H */