1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2004, 2005, 2006, 2008, 2009
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 3, 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 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #ifndef GCC_GTHR_SOLARIS_H
28 #define GCC_GTHR_SOLARIS_H
30 /* Solaris threads as found in Solaris 2.[456].
31 Actually these are Unix International (UI) threads, but I don't
32 know if anyone else implements these. */
42 #define UNUSED(x) x __attribute__((unused))
45 typedef thread_key_t __gthread_key_t
;
50 typedef mutex_t __gthread_mutex_t
;
56 } __gthread_recursive_mutex_t
;
58 #define __GTHREAD_ONCE_INIT { DEFAULTMUTEX, 0 }
59 #define __GTHREAD_MUTEX_INIT DEFAULTMUTEX
60 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
62 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
63 # define __gthrw(name) \
64 static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)));
65 # define __gthrw_(name) __gthrw_ ## name
67 # define __gthrw(name)
68 # define __gthrw_(name) name
71 __gthrw(thr_keycreate
)
72 __gthrw(thr_getspecific
)
73 __gthrw(thr_setspecific
)
78 __gthrw(mutex_destroy
)
80 __gthrw(mutex_trylock
)
92 __gthrw(cond_broadcast
)
97 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
99 /* This will not actually work in Solaris 2.5, since libc contains
100 dummy symbols of all thr_* routines. */
103 __gthread_active_p (void)
105 static void *const __gthread_active_ptr
= (void *) &__gthrw_(thr_create
);
106 return __gthread_active_ptr
!= 0;
109 #else /* not SUPPORTS_WEAK */
112 __gthread_active_p (void)
117 #endif /* SUPPORTS_WEAK */
121 /* Key structure for maintaining thread specific storage */
122 static thread_key_t _objc_thread_storage
;
124 /* Thread local storage for a single thread */
125 static void *thread_local_storage
= NULL
;
127 /* Backend initialization functions */
129 /* Initialize the threads subsystem. */
131 __gthread_objc_init_thread_system (void)
133 /* Initialize the thread storage key. */
134 if (__gthread_active_p ()
135 && __gthrw_(thr_keycreate
) (&_objc_thread_storage
, NULL
) == 0)
141 /* Close the threads subsystem. */
143 __gthread_objc_close_thread_system (void)
145 if (__gthread_active_p ())
151 /* Backend thread functions */
153 /* Create a new thread of execution. */
154 static inline objc_thread_t
155 __gthread_objc_thread_detach (void (*func
)(void *), void *arg
)
157 objc_thread_t thread_id
;
158 thread_t new_thread_id
= 0;
160 if (!__gthread_active_p ())
163 if (__gthrw_(thr_create
) (NULL
, 0, (void *) func
, arg
,
164 THR_DETACHED
| THR_NEW_LWP
,
165 &new_thread_id
) == 0)
166 thread_id
= *(objc_thread_t
*) &new_thread_id
;
173 /* Set the current thread's priority. */
175 __gthread_objc_thread_set_priority (int priority
)
177 int sys_priority
= 0;
179 if (!__gthread_active_p ())
184 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
188 case OBJC_THREAD_BACKGROUND_PRIORITY
:
191 case OBJC_THREAD_LOW_PRIORITY
:
196 /* Change priority */
197 if (__gthrw_(thr_setprio
) (__gthrw_(thr_self
) (), sys_priority
) == 0)
203 /* Return the current thread's priority. */
205 __gthread_objc_thread_get_priority (void)
209 if (!__gthread_active_p ())
210 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
212 if (__gthrw_(thr_getprio
) (__gthrw_(thr_self
) (), &sys_priority
) == 0)
214 if (sys_priority
>= 250)
215 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
216 else if (sys_priority
>= 150)
217 return OBJC_THREAD_BACKGROUND_PRIORITY
;
218 return OBJC_THREAD_LOW_PRIORITY
;
221 /* Couldn't get priority. */
225 /* Yield our process time to another thread. */
227 __gthread_objc_thread_yield (void)
229 if (__gthread_active_p ())
230 __gthrw_(thr_yield
) ();
233 /* Terminate the current thread. */
235 __gthread_objc_thread_exit (void)
237 if (__gthread_active_p ())
238 /* exit the thread */
239 __gthrw_(thr_exit
) (&__objc_thread_exit_status
);
241 /* Failed if we reached here */
245 /* Returns an integer value which uniquely describes a thread. */
246 static inline objc_thread_t
247 __gthread_objc_thread_id (void)
249 if (__gthread_active_p ())
250 return (objc_thread_t
) __gthrw_(thr_self
) ();
252 return (objc_thread_t
) 1;
255 /* Sets the thread's local storage pointer. */
257 __gthread_objc_thread_set_data (void *value
)
259 if (__gthread_active_p ())
261 if (__gthrw_(thr_setspecific
) (_objc_thread_storage
, value
) == 0)
268 thread_local_storage
= value
;
273 /* Returns the thread's local storage pointer. */
275 __gthread_objc_thread_get_data (void)
279 if (__gthread_active_p ())
281 if (__gthrw_(thr_getspecific
) (_objc_thread_storage
, &value
) == 0)
287 return thread_local_storage
;
290 /* Backend mutex functions */
292 /* Allocate a mutex. */
294 __gthread_objc_mutex_allocate (objc_mutex_t mutex
)
296 if (__gthread_active_p ()
297 && __gthrw_(mutex_init
) ((mutex_t
*) (&(mutex
->backend
)), USYNC_THREAD
, 0))
303 /* Deallocate a mutex. */
305 __gthread_objc_mutex_deallocate (objc_mutex_t mutex
)
307 if (__gthread_active_p ())
308 __gthrw_(mutex_destroy
) ((mutex_t
*) (&(mutex
->backend
)));
313 /* Grab a lock on a mutex. */
315 __gthread_objc_mutex_lock (objc_mutex_t mutex
)
317 if (__gthread_active_p ()
318 && __gthrw_(mutex_lock
) ((mutex_t
*) (&(mutex
->backend
))) != 0)
324 /* Try to grab a lock on a mutex. */
326 __gthread_objc_mutex_trylock (objc_mutex_t mutex
)
328 if (__gthread_active_p ()
329 && __gthrw_(mutex_trylock
) ((mutex_t
*) (&(mutex
->backend
))) != 0)
335 /* Unlock the mutex */
337 __gthread_objc_mutex_unlock (objc_mutex_t mutex
)
339 if (__gthread_active_p ()
340 && __gthrw_(mutex_unlock
) ((mutex_t
*) (&(mutex
->backend
))) != 0)
346 /* Backend condition mutex functions */
348 /* Allocate a condition. */
350 __gthread_objc_condition_allocate (objc_condition_t condition
)
352 if (__gthread_active_p ())
353 return __gthrw_(cond_init
) ((cond_t
*) (&(condition
->backend
)), USYNC_THREAD
,
359 /* Deallocate a condition. */
361 __gthread_objc_condition_deallocate (objc_condition_t condition
)
363 if (__gthread_active_p ())
364 return __gthrw_(cond_destroy
) ((cond_t
*) (&(condition
->backend
)));
369 /* Wait on the condition */
371 __gthread_objc_condition_wait (objc_condition_t condition
, objc_mutex_t mutex
)
373 if (__gthread_active_p ())
374 return __gthrw_(cond_wait
) ((cond_t
*) (&(condition
->backend
)),
375 (mutex_t
*) (&(mutex
->backend
)));
380 /* Wake up all threads waiting on this condition. */
382 __gthread_objc_condition_broadcast (objc_condition_t condition
)
384 if (__gthread_active_p ())
385 return __gthrw_(cond_broadcast
) ((cond_t
*) (&(condition
->backend
)));
390 /* Wake up one thread waiting on this condition. */
392 __gthread_objc_condition_signal (objc_condition_t condition
)
394 if (__gthread_active_p ())
395 return __gthrw_(cond_signal
) ((cond_t
*) (&(condition
->backend
)));
403 __gthread_once (__gthread_once_t
*__once
, void (*__func
) (void))
405 if (! __gthread_active_p ())
408 if (__once
== 0 || __func
== 0)
411 if (__once
->once
== 0)
413 int __status
= __gthrw_(mutex_lock
) (&__once
->mutex
);
416 if (__once
->once
== 0)
421 __gthrw_(mutex_unlock
) (&__once
->mutex
);
427 __gthread_key_create (__gthread_key_t
*__key
, void (*__dtor
) (void *))
429 /* Solaris 2.5 contains thr_* routines no-op in libc, so test if we actually
430 got a reasonable key value, and if not, fail. */
431 *__key
= (__gthread_key_t
)-1;
432 if (__gthrw_(thr_keycreate
) (__key
, __dtor
) != 0
433 || *__key
== (__gthread_key_t
)-1)
440 __gthread_key_delete (__gthread_key_t
UNUSED (__key
))
447 __gthread_getspecific (__gthread_key_t __key
)
450 if (__gthrw_(thr_getspecific
) (__key
, &__ptr
) == 0)
457 __gthread_setspecific (__gthread_key_t __key
, const void *__ptr
)
459 return __gthrw_(thr_setspecific
) (__key
, (void *) __ptr
);
463 __gthread_mutex_destroy (__gthread_mutex_t
* UNUSED(__mutex
))
465 if (__gthread_active_p ())
466 return __gthrw_(mutex_destroy
) (__mutex
);
472 __gthread_mutex_lock (__gthread_mutex_t
*__mutex
)
474 if (__gthread_active_p ())
475 return __gthrw_(mutex_lock
) (__mutex
);
481 __gthread_mutex_trylock (__gthread_mutex_t
*__mutex
)
483 if (__gthread_active_p ())
484 return __gthrw_(mutex_trylock
) (__mutex
);
490 __gthread_mutex_unlock (__gthread_mutex_t
*__mutex
)
492 if (__gthread_active_p ())
493 return __gthrw_(mutex_unlock
) (__mutex
);
499 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t
*__mutex
)
502 __mutex
->owner
= (thread_t
) 0;
503 return __gthrw_(mutex_init
) (&__mutex
->actual
, USYNC_THREAD
, 0);
507 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t
*__mutex
)
509 if (__gthread_active_p ())
511 thread_t __me
= __gthrw_(thr_self
) ();
513 if (__mutex
->owner
!= __me
)
515 __gthrw_(mutex_lock
) (&__mutex
->actual
);
516 __mutex
->owner
= __me
;
525 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t
*__mutex
)
527 if (__gthread_active_p ())
529 thread_t __me
= __gthrw_(thr_self
) ();
531 if (__mutex
->owner
!= __me
)
533 if (__gthrw_(mutex_trylock
) (&__mutex
->actual
))
535 __mutex
->owner
= __me
;
544 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t
*__mutex
)
546 if (__gthread_active_p ())
548 if (--__mutex
->depth
== 0)
550 __mutex
->owner
= (thread_t
) 0;
551 __gthrw_(mutex_unlock
) (&__mutex
->actual
);
557 #endif /* _LIBOBJC */
561 #endif /* ! GCC_GTHR_SOLARIS_H */