1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2004, 2005
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_SOLARIS_H
31 #define GCC_GTHR_SOLARIS_H
33 /* Solaris threads as found in Solaris 2.[456].
34 Actually these are Unix International (UI) threads, but I don't
35 know if anyone else implements these. */
42 typedef thread_key_t __gthread_key_t
;
47 typedef mutex_t __gthread_mutex_t
;
53 } __gthread_recursive_mutex_t
;
55 #define __GTHREAD_ONCE_INIT { DEFAULTMUTEX, 0 }
56 #define __GTHREAD_MUTEX_INIT DEFAULTMUTEX
57 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
59 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
60 # define __gthrw(name) \
61 static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)));
62 # define __gthrw_(name) __gthrw_ ## name
64 # define __gthrw(name)
65 # define __gthrw_(name) name
68 __gthrw(thr_keycreate
)
69 __gthrw(thr_getspecific
)
70 __gthrw(thr_setspecific
)
74 __gthrw(mutex_trylock
)
79 __gthrw(thr_keycreate
)
88 __gthrw(cond_broadcast
)
92 __gthrw(mutex_destroy
)
95 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
97 /* This will not actually work in Solaris 2.5, since libc contains
98 dummy symbols of all thr_* routines. */
101 __gthread_active_p (void)
103 static void *const __gthread_active_ptr
= (void *) &__gthrw_(thr_create
);
104 return __gthread_active_ptr
!= 0;
107 #else /* not SUPPORTS_WEAK */
110 __gthread_active_p (void)
115 #endif /* SUPPORTS_WEAK */
119 /* Key structure for maintaining thread specific storage */
120 static thread_key_t _objc_thread_storage
;
122 /* Thread local storage for a single thread */
123 static void *thread_local_storage
= NULL
;
125 /* Backend initialization functions */
127 /* Initialize the threads subsystem. */
129 __gthread_objc_init_thread_system (void)
131 /* Initialize the thread storage key. */
132 if (__gthread_active_p ()
133 && __gthrw_(thr_keycreate
) (&_objc_thread_storage
, NULL
) == 0)
139 /* Close the threads subsystem. */
141 __gthread_objc_close_thread_system (void)
143 if (__gthread_active_p ())
149 /* Backend thread functions */
151 /* Create a new thread of execution. */
152 static inline objc_thread_t
153 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
155 objc_thread_t thread_id
;
156 thread_t new_thread_id
= 0;
158 if (!__gthread_active_p ())
161 if (__gthrw_(thr_create
) (NULL
, 0, (void *) func
, arg
,
162 THR_DETACHED
| THR_NEW_LWP
,
163 &new_thread_id
) == 0)
164 thread_id
= *(objc_thread_t
*) &new_thread_id
;
171 /* Set the current thread's priority. */
173 __gthread_objc_thread_set_priority (int priority
)
175 int sys_priority
= 0;
177 if (!__gthread_active_p ())
182 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
186 case OBJC_THREAD_BACKGROUND_PRIORITY
:
189 case OBJC_THREAD_LOW_PRIORITY
:
194 /* Change priority */
195 if (__gthrw_(thr_setprio
) (__gthrw_(thr_self
) (), sys_priority
) == 0)
201 /* Return the current thread's priority. */
203 __gthread_objc_thread_get_priority (void)
207 if (!__gthread_active_p ())
208 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
210 if (__gthrw_(thr_getprio
) (__gthrw_(thr_self
) (), &sys_priority
) == 0)
212 if (sys_priority
>= 250)
213 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
214 else if (sys_priority
>= 150)
215 return OBJC_THREAD_BACKGROUND_PRIORITY
;
216 return OBJC_THREAD_LOW_PRIORITY
;
219 /* Couldn't get priority. */
223 /* Yield our process time to another thread. */
225 __gthread_objc_thread_yield (void)
227 if (__gthread_active_p ())
228 __gthrw_(thr_yield
) ();
231 /* Terminate the current thread. */
233 __gthread_objc_thread_exit (void)
235 if (__gthread_active_p ())
236 /* exit the thread */
237 __gthrw_(thr_exit
) (&__objc_thread_exit_status
);
239 /* Failed if we reached here */
243 /* Returns an integer value which uniquely describes a thread. */
244 static inline objc_thread_t
245 __gthread_objc_thread_id (void)
247 if (__gthread_active_p ())
248 return (objc_thread_t
) __gthrw_(thr_self
) ();
250 return (objc_thread_t
) 1;
253 /* Sets the thread's local storage pointer. */
255 __gthread_objc_thread_set_data (void *value
)
257 if (__gthread_active_p ())
259 if (__gthrw_(thr_setspecific
) (_objc_thread_storage
, value
) == 0)
266 thread_local_storage
= value
;
271 /* Returns the thread's local storage pointer. */
273 __gthread_objc_thread_get_data (void)
277 if (__gthread_active_p ())
279 if (__gthrw_(thr_getspecific
) (_objc_thread_storage
, &value
) == 0)
285 return thread_local_storage
;
288 /* Backend mutex functions */
290 /* Allocate a mutex. */
292 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
294 if (__gthread_active_p ()
295 && __gthrw_(mutex_init
) ((mutex_t
*) (&(mutex
->backend
)), USYNC_THREAD
, 0))
301 /* Deallocate a mutex. */
303 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
305 if (__gthread_active_p ())
306 __gthrw_(mutex_destroy
) ((mutex_t
*) (&(mutex
->backend
)));
311 /* Grab a lock on a mutex. */
313 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
315 if (__gthread_active_p ()
316 && __gthrw_(mutex_lock
) ((mutex_t
*) (&(mutex
->backend
))) != 0)
322 /* Try to grab a lock on a mutex. */
324 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
326 if (__gthread_active_p ()
327 && __gthrw_(mutex_trylock
) ((mutex_t
*) (&(mutex
->backend
))) != 0)
333 /* Unlock the mutex */
335 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
337 if (__gthread_active_p ()
338 && __gthrw_(mutex_unlock
) ((mutex_t
*) (&(mutex
->backend
))) != 0)
344 /* Backend condition mutex functions */
346 /* Allocate a condition. */
348 __gthread_objc_condition_allocate (objc_condition_t condition
)
350 if (__gthread_active_p ())
351 return __gthrw_(cond_init
) ((cond_t
*) (&(condition
->backend
)), USYNC_THREAD
,
357 /* Deallocate a condition. */
359 __gthread_objc_condition_deallocate (objc_condition_t condition
)
361 if (__gthread_active_p ())
362 return __gthrw_(cond_destroy
) ((cond_t
*) (&(condition
->backend
)));
367 /* Wait on the condition */
369 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
371 if (__gthread_active_p ())
372 return __gthrw_(cond_wait
) ((cond_t
*) (&(condition
->backend
)),
373 (mutex_t
*) (&(mutex
->backend
)));
378 /* Wake up all threads waiting on this condition. */
380 __gthread_objc_condition_broadcast (objc_condition_t condition
)
382 if (__gthread_active_p ())
383 return __gthrw_(cond_broadcast
) ((cond_t
*) (&(condition
->backend
)));
388 /* Wake up one thread waiting on this condition. */
390 __gthread_objc_condition_signal (objc_condition_t condition
)
392 if (__gthread_active_p ())
393 return __gthrw_(cond_signal
) ((cond_t
*) (&(condition
->backend
)));
401 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
403 if (! __gthread_active_p ())
406 if (once
== 0 || func
== 0)
411 int status
= __gthrw_(mutex_lock
) (&once
->mutex
);
419 __gthrw_(mutex_unlock
) (&once
->mutex
);
425 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
427 /* Solaris 2.5 contains thr_* routines no-op in libc, so test if we actually
428 got a reasonable key value, and if not, fail. */
429 *key
= (__gthread_key_t
)-1;
430 if (__gthrw_(thr_keycreate
) (key
, dtor
) != 0 || *key
== (__gthread_key_t
)-1)
437 __gthread_key_delete (__gthread_key_t key
)
444 __gthread_getspecific (__gthread_key_t key
)
447 if (__gthrw_(thr_getspecific
) (key
, &ptr
) == 0)
454 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
456 return __gthrw_(thr_setspecific
) (key
, (void *) ptr
);
460 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
462 if (__gthread_active_p ())
463 return __gthrw_(mutex_lock
) (mutex
);
469 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
471 if (__gthread_active_p ())
472 return __gthrw_(mutex_trylock
) (mutex
);
478 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
480 if (__gthread_active_p ())
481 return __gthrw_(mutex_unlock
) (mutex
);
487 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*mutex
)
490 mutex
->owner
= (thread_t
) 0;
491 return __gthrw_(mutex_init
) (&mutex
->actual
, USYNC_THREAD
, 0);
495 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*mutex
)
497 if (__gthread_active_p ())
499 thread_t me
= __gthrw_(thr_self
) ();
501 if (mutex
->owner
!= me
)
503 __gthrw_(mutex_lock
) (&mutex
->actual
);
513 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*mutex
)
515 if (__gthread_active_p ())
517 thread_t me
= __gthrw_(thr_self
) ();
519 if (mutex
->owner
!= me
)
521 if (__gthrw_(mutex_trylock
) (&mutex
->actual
))
532 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*mutex
)
534 if (__gthread_active_p ())
536 if (--mutex
->depth
== 0)
538 mutex
->owner
= (thread_t
) 0;
539 __gthrw_(mutex_unlock
) (&mutex
->actual
);
545 #endif /* _LIBOBJC */
547 #endif /* ! GCC_GTHR_SOLARIS_H */