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 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
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
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
23 /* As a special exception, if you link this library with other files,
24 some of which are compiled with GCC, to produce an executable,
25 this library does not by itself cause the resulting executable
26 to be covered by the GNU General Public License.
27 This exception does not however invalidate any other reasons why
28 the executable file might be covered by the GNU General Public License. */
30 #ifndef GCC_GTHR_POSIX_H
31 #define GCC_GTHR_POSIX_H
33 /* POSIX threads specific definitions.
34 Easy, since the interface is just one-to-one mapping. */
38 /* Some implementations of <pthread.h> require this to be defined. */
39 #if !defined(_REENTRANT) && defined(__osf__)
46 typedef pthread_key_t __gthread_key_t
;
47 typedef pthread_once_t __gthread_once_t
;
48 typedef pthread_mutex_t __gthread_mutex_t
;
49 typedef pthread_mutex_t __gthread_recursive_mutex_t
;
51 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
52 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
53 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
54 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
55 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
56 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
58 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
61 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
62 # ifndef __gthrw_pragma
63 # define __gthrw_pragma(pragma)
65 # define __gthrw2(name,name2,type) \
66 extern __typeof(type) name __attribute__ ((__weakref__(#name2))); \
67 __gthrw_pragma(weak type)
68 # define __gthrw_(name) __gthrw_ ## name
70 # define __gthrw2(name,name2,type)
71 # define __gthrw_(name) name
74 /* Typically, __gthrw_foo is a weak reference to symbol foo. */
75 #define __gthrw(name) __gthrw2(__gthrw_ ## name,name,name)
77 /* On Tru64, /usr/include/pthread.h uses #pragma extern_prefix "__" to
78 map a subset of the POSIX pthread API to mangled versions of their
80 #if defined(__osf__) && defined(_PTHREAD_USE_MANGLED_NAMES_)
81 #define __gthrw3(name) __gthrw2(__gthrw_ ## name, __ ## name, name)
82 __gthrw3(pthread_once
)
83 __gthrw3(pthread_getspecific
)
84 __gthrw3(pthread_setspecific
)
85 __gthrw3(pthread_create
)
86 __gthrw3(pthread_cancel
)
87 __gthrw3(pthread_mutex_lock
)
88 __gthrw3(pthread_mutex_trylock
)
89 __gthrw3(pthread_mutex_unlock
)
90 __gthrw3(pthread_mutex_init
)
93 __gthrw(pthread_getspecific
)
94 __gthrw(pthread_setspecific
)
95 __gthrw(pthread_create
)
96 __gthrw(pthread_cancel
)
97 __gthrw(pthread_mutex_lock
)
98 __gthrw(pthread_mutex_trylock
)
99 __gthrw(pthread_mutex_unlock
)
100 __gthrw(pthread_mutex_init
)
103 __gthrw(pthread_key_create
)
104 __gthrw(pthread_key_delete
)
105 __gthrw(pthread_mutexattr_init
)
106 __gthrw(pthread_mutexattr_settype
)
107 __gthrw(pthread_mutexattr_destroy
)
110 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
112 #if defined(__osf__) && defined(_PTHREAD_USE_MANGLED_NAMES_)
113 __gthrw3(pthread_cond_broadcast
)
114 __gthrw3(pthread_cond_destroy
)
115 __gthrw3(pthread_cond_init
)
116 __gthrw3(pthread_cond_signal
)
117 __gthrw3(pthread_cond_wait
)
118 __gthrw3(pthread_exit
)
119 __gthrw3(pthread_mutex_destroy
)
120 __gthrw3(pthread_self
)
122 __gthrw(pthread_cond_broadcast
)
123 __gthrw(pthread_cond_destroy
)
124 __gthrw(pthread_cond_init
)
125 __gthrw(pthread_cond_signal
)
126 __gthrw(pthread_cond_wait
)
127 __gthrw(pthread_exit
)
128 __gthrw(pthread_mutex_destroy
)
129 __gthrw(pthread_self
)
130 #endif /* __osf__ && _PTHREAD_USE_MANGLED_NAMES_ */
131 #ifdef _POSIX_PRIORITY_SCHEDULING
132 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
133 __gthrw(sched_get_priority_max
)
134 __gthrw(sched_get_priority_min
)
135 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
136 #endif /* _POSIX_PRIORITY_SCHEDULING */
138 __gthrw(pthread_attr_destroy
)
139 __gthrw(pthread_attr_init
)
140 __gthrw(pthread_attr_setdetachstate
)
141 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
142 __gthrw(pthread_getschedparam
)
143 __gthrw(pthread_setschedparam
)
144 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
145 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
147 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
150 __gthread_active_p (void)
152 static void *const __gthread_active_ptr
153 = __extension__ (void *) &__gthrw_(pthread_cancel
);
154 return __gthread_active_ptr
!= 0;
157 #else /* not SUPPORTS_WEAK */
160 __gthread_active_p (void)
165 #endif /* SUPPORTS_WEAK */
169 /* This is the config.h file in libobjc/ */
176 /* Key structure for maintaining thread specific storage */
177 static pthread_key_t _objc_thread_storage
;
178 static pthread_attr_t _objc_thread_attribs
;
180 /* Thread local storage for a single thread */
181 static void *thread_local_storage
= NULL
;
183 /* Backend initialization functions */
185 /* Initialize the threads subsystem. */
187 __gthread_objc_init_thread_system (void)
189 if (__gthread_active_p ())
191 /* Initialize the thread storage key. */
192 if (__gthrw_(pthread_key_create
) (&_objc_thread_storage
, NULL
) == 0)
194 /* The normal default detach state for threads is
195 * PTHREAD_CREATE_JOINABLE which causes threads to not die
196 * when you think they should. */
197 if (__gthrw_(pthread_attr_init
) (&_objc_thread_attribs
) == 0
198 && __gthrw_(pthread_attr_setdetachstate
) (&_objc_thread_attribs
,
199 PTHREAD_CREATE_DETACHED
) == 0)
207 /* Close the threads subsystem. */
209 __gthread_objc_close_thread_system (void)
211 if (__gthread_active_p ()
212 && __gthrw_(pthread_key_delete
) (_objc_thread_storage
) == 0
213 && __gthrw_(pthread_attr_destroy
) (&_objc_thread_attribs
) == 0)
219 /* Backend thread functions */
221 /* Create a new thread of execution. */
222 static inline objc_thread_t
223 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
225 objc_thread_t thread_id
;
226 pthread_t new_thread_handle
;
228 if (!__gthread_active_p ())
231 if (!(__gthrw_(pthread_create
) (&new_thread_handle
, NULL
, (void *) func
, arg
)))
232 thread_id
= (objc_thread_t
) new_thread_handle
;
239 /* Set the current thread's priority. */
241 __gthread_objc_thread_set_priority (int priority
)
243 if (!__gthread_active_p ())
247 #ifdef _POSIX_PRIORITY_SCHEDULING
248 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
249 pthread_t thread_id
= __gthrw_(pthread_self
) ();
251 struct sched_param params
;
252 int priority_min
, priority_max
;
254 if (__gthrw_(pthread_getschedparam
) (thread_id
, &policy
, ¶ms
) == 0)
256 if ((priority_max
= __gthrw_(sched_get_priority_max
) (policy
)) == -1)
259 if ((priority_min
= __gthrw_(sched_get_priority_min
) (policy
)) == -1)
262 if (priority
> priority_max
)
263 priority
= priority_max
;
264 else if (priority
< priority_min
)
265 priority
= priority_min
;
266 params
.sched_priority
= priority
;
269 * The solaris 7 and several other man pages incorrectly state that
270 * this should be a pointer to policy but pthread.h is universally
273 if (__gthrw_(pthread_setschedparam
) (thread_id
, policy
, ¶ms
) == 0)
276 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
277 #endif /* _POSIX_PRIORITY_SCHEDULING */
282 /* Return the current thread's priority. */
284 __gthread_objc_thread_get_priority (void)
286 #ifdef _POSIX_PRIORITY_SCHEDULING
287 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
288 if (__gthread_active_p ())
291 struct sched_param params
;
293 if (__gthrw_(pthread_getschedparam
) (__gthrw_(pthread_self
) (), &policy
, ¶ms
) == 0)
294 return params
.sched_priority
;
299 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
300 #endif /* _POSIX_PRIORITY_SCHEDULING */
301 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
304 /* Yield our process time to another thread. */
306 __gthread_objc_thread_yield (void)
308 if (__gthread_active_p ())
309 __gthrw_(sched_yield
) ();
312 /* Terminate the current thread. */
314 __gthread_objc_thread_exit (void)
316 if (__gthread_active_p ())
317 /* exit the thread */
318 __gthrw_(pthread_exit
) (&__objc_thread_exit_status
);
320 /* Failed if we reached here */
324 /* Returns an integer value which uniquely describes a thread. */
325 static inline objc_thread_t
326 __gthread_objc_thread_id (void)
328 if (__gthread_active_p ())
329 return (objc_thread_t
) __gthrw_(pthread_self
) ();
331 return (objc_thread_t
) 1;
334 /* Sets the thread's local storage pointer. */
336 __gthread_objc_thread_set_data (void *value
)
338 if (__gthread_active_p ())
339 return __gthrw_(pthread_setspecific
) (_objc_thread_storage
, value
);
342 thread_local_storage
= value
;
347 /* Returns the thread's local storage pointer. */
349 __gthread_objc_thread_get_data (void)
351 if (__gthread_active_p ())
352 return __gthrw_(pthread_getspecific
) (_objc_thread_storage
);
354 return thread_local_storage
;
357 /* Backend mutex functions */
359 /* Allocate a mutex. */
361 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
363 if (__gthread_active_p ())
365 mutex
->backend
= objc_malloc (sizeof (pthread_mutex_t
));
367 if (__gthrw_(pthread_mutex_init
) ((pthread_mutex_t
*) mutex
->backend
, NULL
))
369 objc_free (mutex
->backend
);
370 mutex
->backend
= NULL
;
378 /* Deallocate a mutex. */
380 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
382 if (__gthread_active_p ())
387 * Posix Threads specifically require that the thread be unlocked
388 * for __gthrw_(pthread_mutex_destroy) to work.
393 count
= __gthrw_(pthread_mutex_unlock
) ((pthread_mutex_t
*) mutex
->backend
);
399 if (__gthrw_(pthread_mutex_destroy
) ((pthread_mutex_t
*) mutex
->backend
))
402 objc_free (mutex
->backend
);
403 mutex
->backend
= NULL
;
408 /* Grab a lock on a mutex. */
410 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
412 if (__gthread_active_p ()
413 && __gthrw_(pthread_mutex_lock
) ((pthread_mutex_t
*) mutex
->backend
) != 0)
421 /* Try to grab a lock on a mutex. */
423 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
425 if (__gthread_active_p ()
426 && __gthrw_(pthread_mutex_trylock
) ((pthread_mutex_t
*) mutex
->backend
) != 0)
434 /* Unlock the mutex */
436 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
438 if (__gthread_active_p ()
439 && __gthrw_(pthread_mutex_unlock
) ((pthread_mutex_t
*) mutex
->backend
) != 0)
447 /* Backend condition mutex functions */
449 /* Allocate a condition. */
451 __gthread_objc_condition_allocate (objc_condition_t condition
)
453 if (__gthread_active_p ())
455 condition
->backend
= objc_malloc (sizeof (pthread_cond_t
));
457 if (__gthrw_(pthread_cond_init
) ((pthread_cond_t
*) condition
->backend
, NULL
))
459 objc_free (condition
->backend
);
460 condition
->backend
= NULL
;
468 /* Deallocate a condition. */
470 __gthread_objc_condition_deallocate (objc_condition_t condition
)
472 if (__gthread_active_p ())
474 if (__gthrw_(pthread_cond_destroy
) ((pthread_cond_t
*) condition
->backend
))
477 objc_free (condition
->backend
);
478 condition
->backend
= NULL
;
483 /* Wait on the condition */
485 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
487 if (__gthread_active_p ())
488 return __gthrw_(pthread_cond_wait
) ((pthread_cond_t
*) condition
->backend
,
489 (pthread_mutex_t
*) mutex
->backend
);
494 /* Wake up all threads waiting on this condition. */
496 __gthread_objc_condition_broadcast (objc_condition_t condition
)
498 if (__gthread_active_p ())
499 return __gthrw_(pthread_cond_broadcast
) ((pthread_cond_t
*) condition
->backend
);
504 /* Wake up one thread waiting on this condition. */
506 __gthread_objc_condition_signal (objc_condition_t condition
)
508 if (__gthread_active_p ())
509 return __gthrw_(pthread_cond_signal
) ((pthread_cond_t
*) condition
->backend
);
517 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
519 if (__gthread_active_p ())
520 return __gthrw_(pthread_once
) (once
, func
);
526 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
528 return __gthrw_(pthread_key_create
) (key
, dtor
);
532 __gthread_key_delete (__gthread_key_t key
)
534 return __gthrw_(pthread_key_delete
) (key
);
538 __gthread_getspecific (__gthread_key_t key
)
540 return __gthrw_(pthread_getspecific
) (key
);
544 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
546 return __gthrw_(pthread_setspecific
) (key
, ptr
);
550 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
552 if (__gthread_active_p ())
553 return __gthrw_(pthread_mutex_lock
) (mutex
);
559 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
561 if (__gthread_active_p ())
562 return __gthrw_(pthread_mutex_trylock
) (mutex
);
568 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
570 if (__gthread_active_p ())
571 return __gthrw_(pthread_mutex_unlock
) (mutex
);
576 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
578 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
580 if (__gthread_active_p ())
582 pthread_mutexattr_t attr
;
585 r
= __gthrw_(pthread_mutexattr_init
) (&attr
);
587 r
= __gthrw_(pthread_mutexattr_settype
) (&attr
, PTHREAD_MUTEX_RECURSIVE
);
589 r
= __gthrw_(pthread_mutex_init
) (mutex
, &attr
);
591 r
= __gthrw_(pthread_mutexattr_destroy
) (&attr
);
599 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
601 return __gthread_mutex_lock (mutex
);
605 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
607 return __gthread_mutex_trylock (mutex
);
611 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
613 return __gthread_mutex_unlock (mutex
);
616 #endif /* _LIBOBJC */
618 #endif /* ! GCC_GTHR_POSIX_H */