1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004
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, 59 Temple Place - Suite 330, 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. */
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
57 #elif defined(PTHREAD_MUTEX_RECURSIVE)
58 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
60 #define __GTHREAD_RECURSIVE_MUTEX_INIT RECURSIVE_ERRORCHECKMUTEX
63 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
65 #pragma weak pthread_once
66 #pragma weak pthread_key_create
67 #pragma weak pthread_key_delete
68 #pragma weak pthread_getspecific
69 #pragma weak pthread_setspecific
70 #pragma weak pthread_create
72 #pragma weak pthread_mutex_lock
73 #pragma weak pthread_mutex_trylock
74 #pragma weak pthread_mutex_unlock
75 #pragma weak pthread_mutexattr_init
76 #pragma weak pthread_mutexattr_settype
77 #pragma weak pthread_mutexattr_destroy
79 #pragma weak pthread_mutex_init
81 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
83 #pragma weak pthread_cond_broadcast
84 #pragma weak pthread_cond_destroy
85 #pragma weak pthread_cond_init
86 #pragma weak pthread_cond_signal
87 #pragma weak pthread_cond_wait
88 #pragma weak pthread_exit
89 #pragma weak pthread_mutex_destroy
90 #pragma weak pthread_self
91 #ifdef _POSIX_PRIORITY_SCHEDULING
92 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
93 #pragma weak sched_get_priority_max
94 #pragma weak sched_get_priority_min
95 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
96 #endif /* _POSIX_PRIORITY_SCHEDULING */
97 #pragma weak sched_yield
98 #pragma weak pthread_attr_destroy
99 #pragma weak pthread_attr_init
100 #pragma weak pthread_attr_setdetachstate
101 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
102 #pragma weak pthread_getschedparam
103 #pragma weak pthread_setschedparam
104 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
105 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
108 __gthread_active_p (void)
110 static void *const __gthread_active_ptr
111 = __extension__ (void *) &pthread_create;
112 return __gthread_active_ptr != 0;
115 #else /* not SUPPORTS_WEAK */
118 __gthread_active_p (void)
123 #endif /* SUPPORTS_WEAK */
127 /* This is the config.h file in libobjc/ */
134 /* Key structure for maintaining thread specific storage */
135 static pthread_key_t _objc_thread_storage;
136 static pthread_attr_t _objc_thread_attribs;
138 /* Thread local storage for a single thread */
139 static void *thread_local_storage = NULL;
141 /* Backend initialization functions */
143 /* Initialize the threads subsystem. */
145 __gthread_objc_init_thread_system (void)
147 if (__gthread_active_p ())
149 /* Initialize the thread storage key. */
150 if (pthread_key_create (&_objc_thread_storage, NULL) == 0)
152 /* The normal default detach state for threads is
153 * PTHREAD_CREATE_JOINABLE which causes threads to not die
154 * when you think they should. */
155 if (pthread_attr_init (&_objc_thread_attribs) == 0
156 && pthread_attr_setdetachstate (&_objc_thread_attribs,
157 PTHREAD_CREATE_DETACHED) == 0)
165 /* Close the threads subsystem. */
167 __gthread_objc_close_thread_system (void)
169 if (__gthread_active_p ()
170 && pthread_key_delete (_objc_thread_storage) == 0
171 && pthread_attr_destroy (&_objc_thread_attribs) == 0)
177 /* Backend thread functions */
179 /* Create a new thread of execution. */
180 static inline objc_thread_t
181 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
183 objc_thread_t thread_id;
184 pthread_t new_thread_handle;
186 if (!__gthread_active_p ())
189 if (!(pthread_create (&new_thread_handle, NULL, (void *) func, arg)))
190 thread_id = (objc_thread_t) new_thread_handle;
197 /* Set the current thread's priority. */
199 __gthread_objc_thread_set_priority (int priority)
201 if (!__gthread_active_p ())
205 #ifdef _POSIX_PRIORITY_SCHEDULING
206 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
207 pthread_t thread_id = pthread_self ();
209 struct sched_param params;
210 int priority_min, priority_max;
212 if (pthread_getschedparam (thread_id, &policy, ¶ms) == 0)
214 if ((priority_max = sched_get_priority_max (policy)) == -1)
217 if ((priority_min = sched_get_priority_min (policy)) == -1)
220 if (priority > priority_max)
221 priority = priority_max;
222 else if (priority < priority_min)
223 priority = priority_min;
224 params.sched_priority = priority;
227 * The solaris 7 and several other man pages incorrectly state that
228 * this should be a pointer to policy but pthread.h is universally
231 if (pthread_setschedparam (thread_id, policy, ¶ms) == 0)
234 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
235 #endif /* _POSIX_PRIORITY_SCHEDULING */
240 /* Return the current thread's priority. */
242 __gthread_objc_thread_get_priority (void)
244 #ifdef _POSIX_PRIORITY_SCHEDULING
245 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
246 if (__gthread_active_p ())
249 struct sched_param params;
251 if (pthread_getschedparam (pthread_self (), &policy, ¶ms) == 0)
252 return params.sched_priority;
257 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
258 #endif /* _POSIX_PRIORITY_SCHEDULING */
259 return OBJC_THREAD_INTERACTIVE_PRIORITY;
262 /* Yield our process time to another thread. */
264 __gthread_objc_thread_yield (void)
266 if (__gthread_active_p ())
270 /* Terminate the current thread. */
272 __gthread_objc_thread_exit (void)
274 if (__gthread_active_p ())
275 /* exit the thread */
276 pthread_exit (&__objc_thread_exit_status);
278 /* Failed if we reached here */
282 /* Returns an integer value which uniquely describes a thread. */
283 static inline objc_thread_t
284 __gthread_objc_thread_id (void)
286 if (__gthread_active_p ())
287 return (objc_thread_t) pthread_self ();
289 return (objc_thread_t) 1;
292 /* Sets the thread's local storage pointer. */
294 __gthread_objc_thread_set_data (void *value)
296 if (__gthread_active_p ())
297 return pthread_setspecific (_objc_thread_storage, value);
300 thread_local_storage = value;
305 /* Returns the thread's local storage pointer. */
307 __gthread_objc_thread_get_data (void)
309 if (__gthread_active_p ())
310 return pthread_getspecific (_objc_thread_storage);
312 return thread_local_storage;
315 /* Backend mutex functions */
317 /* Allocate a mutex. */
319 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
321 if (__gthread_active_p ())
323 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
325 if (pthread_mutex_init ((pthread_mutex_t *) mutex->backend, NULL))
327 objc_free (mutex->backend);
328 mutex->backend = NULL;
336 /* Deallocate a mutex. */
338 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
340 if (__gthread_active_p ())
345 * Posix Threads specifically require that the thread be unlocked
346 * for pthread_mutex_destroy to work.
351 count = pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend);
357 if (pthread_mutex_destroy ((pthread_mutex_t *) mutex->backend))
360 objc_free (mutex->backend);
361 mutex->backend = NULL;
366 /* Grab a lock on a mutex. */
368 __gthread_objc_mutex_lock (objc_mutex_t mutex)
370 if (__gthread_active_p ()
371 && pthread_mutex_lock ((pthread_mutex_t *) mutex->backend) != 0)
379 /* Try to grab a lock on a mutex. */
381 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
383 if (__gthread_active_p ()
384 && pthread_mutex_trylock ((pthread_mutex_t *) mutex->backend) != 0)
392 /* Unlock the mutex */
394 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
396 if (__gthread_active_p ()
397 && pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend) != 0)
405 /* Backend condition mutex functions */
407 /* Allocate a condition. */
409 __gthread_objc_condition_allocate (objc_condition_t condition)
411 if (__gthread_active_p ())
413 condition->backend = objc_malloc (sizeof (pthread_cond_t));
415 if (pthread_cond_init ((pthread_cond_t *) condition->backend, NULL))
417 objc_free (condition->backend);
418 condition->backend = NULL;
426 /* Deallocate a condition. */
428 __gthread_objc_condition_deallocate (objc_condition_t condition)
430 if (__gthread_active_p ())
432 if (pthread_cond_destroy ((pthread_cond_t *) condition->backend))
435 objc_free (condition->backend);
436 condition->backend = NULL;
441 /* Wait on the condition */
443 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
445 if (__gthread_active_p ())
446 return pthread_cond_wait ((pthread_cond_t *) condition->backend,
447 (pthread_mutex_t *) mutex->backend);
452 /* Wake up all threads waiting on this condition. */
454 __gthread_objc_condition_broadcast (objc_condition_t condition)
456 if (__gthread_active_p ())
457 return pthread_cond_broadcast ((pthread_cond_t *) condition->backend);
462 /* Wake up one thread waiting on this condition. */
464 __gthread_objc_condition_signal (objc_condition_t condition)
466 if (__gthread_active_p ())
467 return pthread_cond_signal ((pthread_cond_t *) condition->backend);
475 __gthread_once (__gthread_once_t *once, void (*func) (void))
477 if (__gthread_active_p ())
478 return pthread_once (once, func);
484 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
486 return pthread_key_create (key, dtor);
490 __gthread_key_delete (__gthread_key_t key)
492 return pthread_key_delete (key);
496 __gthread_getspecific (__gthread_key_t key)
498 return pthread_getspecific (key);
502 __gthread_setspecific (__gthread_key_t key, const void *ptr)
504 return pthread_setspecific (key, ptr);
508 __gthread_mutex_lock (__gthread_mutex_t *mutex)
510 if (__gthread_active_p ())
511 return pthread_mutex_lock (mutex);
517 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
519 if (__gthread_active_p ())
520 return pthread_mutex_trylock (mutex);
526 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
528 if (__gthread_active_p ())
529 return pthread_mutex_unlock (mutex);
534 #if !defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) && defined(PTHREAD_MUTEX_RECURSIVE)
536 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
538 if (__gthread_active_p ())
540 pthread_mutexattr_t attr;
543 r = pthread_mutexattr_init (&attr);
545 r = pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
547 r = pthread_mutex_init (mutex, &attr);
549 r = pthread_mutexattr_destroy (&attr);
556 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
558 return __gthread_mutex_lock (mutex);
562 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
564 return __gthread_mutex_trylock (mutex);
568 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
570 return __gthread_mutex_unlock (mutex);
573 #endif /* _LIBOBJC */
575 #endif /* ! GCC_GTHR_POSIX_H */