1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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 __gthr_posix_h
30 #define __gthr_posix_h
32 /* POSIX threads specific definitions.
33 Easy, since the interface is just one-to-one mapping. */
39 typedef pthread_key_t __gthread_key_t;
40 typedef pthread_once_t __gthread_once_t;
41 typedef pthread_mutex_t __gthread_mutex_t;
43 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
44 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
46 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
48 #pragma weak pthread_once
49 #pragma weak pthread_key_create
50 #pragma weak pthread_key_delete
51 #pragma weak pthread_getspecific
52 #pragma weak pthread_setspecific
53 #pragma weak pthread_create
55 #pragma weak pthread_mutex_lock
56 #pragma weak pthread_mutex_trylock
57 #pragma weak pthread_mutex_unlock
61 #pragma weak pthread_cond_broadcast
62 #pragma weak pthread_cond_destroy
63 #pragma weak pthread_cond_init
64 #pragma weak pthread_cond_signal
65 #pragma weak pthread_cond_wait
66 #pragma weak pthread_exit
67 #pragma weak pthread_mutex_init
68 #pragma weak pthread_mutex_destroy
69 #pragma weak pthread_self
70 #pragma weak sched_yield
73 static void *__gthread_active_ptr = &pthread_create;
76 __gthread_active_p (void)
78 return __gthread_active_ptr != 0;
81 #else /* not SUPPORTS_WEAK */
84 __gthread_active_p (void)
89 #endif /* SUPPORTS_WEAK */
93 /* Key structure for maintaining thread specific storage */
94 static pthread_key_t _objc_thread_storage;
95 static pthread_attr_t _objc_thread_attribs;
97 /* Thread local storage for a single thread */
98 static void *thread_local_storage = NULL;
100 /* Backend initialization functions */
102 /* Initialize the threads subsystem. */
104 __gthread_objc_init_thread_system(void)
106 if (__gthread_active_p ())
108 /* Initialize the thread storage key */
109 if (pthread_key_create(&_objc_thread_storage, NULL) == 0)
111 /* The normal default detach state for threads is
112 * PTHREAD_CREATE_JOINABLE which causes threads to not die
113 * when you think they should. */
114 if (pthread_attr_init(&_objc_thread_attribs) == 0
115 && pthread_attr_setdetachstate(&_objc_thread_attribs,
116 PTHREAD_CREATE_DETACHED) == 0)
124 /* Close the threads subsystem. */
126 __gthread_objc_close_thread_system(void)
128 if (__gthread_active_p ()
129 && pthread_key_delete(_objc_thread_storage) == 0
130 && pthread_attr_destroy(&_objc_thread_attribs) == 0)
136 /* Backend thread functions */
138 /* Create a new thread of execution. */
139 static inline objc_thread_t
140 __gthread_objc_thread_detach(void (*func)(void *), void *arg)
142 objc_thread_t thread_id;
143 pthread_t new_thread_handle;
145 if (!__gthread_active_p ())
148 if ( !(pthread_create(&new_thread_handle, NULL, (void *)func, arg)) )
149 thread_id = *(objc_thread_t *)&new_thread_handle;
156 /* Set the current thread's priority. */
158 __gthread_objc_thread_set_priority(int priority)
160 if (!__gthread_active_p())
163 pthread_t thread_id = pthread_self();
165 struct sched_param params;
166 int priority_min, priority_max;
168 if (pthread_getschedparam(thread_id, &policy, ¶ms) == 0)
170 if ((priority_max = sched_get_priority_max(policy)) != 0)
173 if ((priority_min = sched_get_priority_min(policy)) != 0)
176 if (priority > priority_max)
177 priority = priority_max;
178 else if (priority < priority_min)
179 priority = priority_min;
180 params.sched_priority = priority;
183 * The solaris 7 and several other man pages incorrectly state that
184 * this should be a pointer to policy but pthread.h is universally
187 if (pthread_setschedparam(thread_id, policy, ¶ms) == 0)
194 /* Return the current thread's priority. */
196 __gthread_objc_thread_get_priority(void)
198 if (__gthread_active_p ())
201 struct sched_param params;
203 if (pthread_getschedparam(pthread_self(), &policy, ¶ms) == 0)
204 return params.sched_priority;
209 return OBJC_THREAD_INTERACTIVE_PRIORITY;
212 /* Yield our process time to another thread. */
214 __gthread_objc_thread_yield(void)
216 if (__gthread_active_p ())
220 /* Terminate the current thread. */
222 __gthread_objc_thread_exit(void)
224 if (__gthread_active_p ())
225 /* exit the thread */
226 pthread_exit(&__objc_thread_exit_status);
228 /* Failed if we reached here */
232 /* Returns an integer value which uniquely describes a thread. */
233 static inline objc_thread_t
234 __gthread_objc_thread_id(void)
236 if (__gthread_active_p ())
238 pthread_t self = pthread_self();
240 return *(objc_thread_t *)&self;
243 return (objc_thread_t)1;
246 /* Sets the thread's local storage pointer. */
248 __gthread_objc_thread_set_data(void *value)
250 if (__gthread_active_p ())
251 return pthread_setspecific(_objc_thread_storage, value);
254 thread_local_storage = value;
259 /* Returns the thread's local storage pointer. */
261 __gthread_objc_thread_get_data(void)
263 if (__gthread_active_p ())
264 return pthread_getspecific(_objc_thread_storage);
266 return thread_local_storage;
269 /* Backend mutex functions */
271 /* Allocate a mutex. */
273 __gthread_objc_mutex_allocate(objc_mutex_t mutex)
275 if (__gthread_active_p ())
277 mutex->backend = objc_malloc(sizeof(pthread_mutex_t));
279 if (pthread_mutex_init((pthread_mutex_t *)mutex->backend, NULL))
281 objc_free(mutex->backend);
282 mutex->backend = NULL;
290 /* Deallocate a mutex. */
292 __gthread_objc_mutex_deallocate(objc_mutex_t mutex)
294 if (__gthread_active_p ())
299 * Posix Threads specifically require that the thread be unlocked
300 * for pthread_mutex_destroy to work.
305 count = pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
311 if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
314 objc_free(mutex->backend);
315 mutex->backend = NULL;
320 /* Grab a lock on a mutex. */
322 __gthread_objc_mutex_lock(objc_mutex_t mutex)
324 if (__gthread_active_p ())
325 return pthread_mutex_lock((pthread_mutex_t *)mutex->backend);
330 /* Try to grab a lock on a mutex. */
332 __gthread_objc_mutex_trylock(objc_mutex_t mutex)
334 if (__gthread_active_p ())
335 return pthread_mutex_trylock((pthread_mutex_t *)mutex->backend);
340 /* Unlock the mutex */
342 __gthread_objc_mutex_unlock(objc_mutex_t mutex)
344 if (__gthread_active_p ())
345 return pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
350 /* Backend condition mutex functions */
352 /* Allocate a condition. */
354 __gthread_objc_condition_allocate(objc_condition_t condition)
356 if (__gthread_active_p ())
358 condition->backend = objc_malloc(sizeof(pthread_cond_t));
360 if (pthread_cond_init((pthread_cond_t *)condition->backend, NULL))
362 objc_free(condition->backend);
363 condition->backend = NULL;
371 /* Deallocate a condition. */
373 __gthread_objc_condition_deallocate(objc_condition_t condition)
375 if (__gthread_active_p ())
377 if (pthread_cond_destroy((pthread_cond_t *)condition->backend))
380 objc_free(condition->backend);
381 condition->backend = NULL;
386 /* Wait on the condition */
388 __gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
390 if (__gthread_active_p ())
391 return pthread_cond_wait((pthread_cond_t *)condition->backend,
392 (pthread_mutex_t *)mutex->backend);
397 /* Wake up all threads waiting on this condition. */
399 __gthread_objc_condition_broadcast(objc_condition_t condition)
401 if (__gthread_active_p ())
402 return pthread_cond_broadcast((pthread_cond_t *)condition->backend);
407 /* Wake up one thread waiting on this condition. */
409 __gthread_objc_condition_signal(objc_condition_t condition)
411 if (__gthread_active_p ())
412 return pthread_cond_signal((pthread_cond_t *)condition->backend);
420 __gthread_once (__gthread_once_t *once, void (*func) (void))
422 if (__gthread_active_p ())
423 return pthread_once (once, func);
429 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
431 return pthread_key_create (key, dtor);
435 __gthread_key_dtor (__gthread_key_t key, void *ptr)
437 /* Just reset the key value to zero. */
439 return pthread_setspecific (key, 0);
445 __gthread_key_delete (__gthread_key_t key)
447 return pthread_key_delete (key);
451 __gthread_getspecific (__gthread_key_t key)
453 return pthread_getspecific (key);
457 __gthread_setspecific (__gthread_key_t key, const void *ptr)
459 return pthread_setspecific (key, ptr);
463 __gthread_mutex_lock (__gthread_mutex_t *mutex)
465 if (__gthread_active_p ())
466 return pthread_mutex_lock (mutex);
472 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
474 if (__gthread_active_p ())
475 return pthread_mutex_trylock (mutex);
481 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
483 if (__gthread_active_p ())
484 return pthread_mutex_unlock (mutex);
489 #endif /* _LIBOBJC */
491 #endif /* not __gthr_posix_h */