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_solaris_h
30 #define __gthr_solaris_h
32 /* Solaris threads as found in Solaris 2.[456].
33 Actually these are Unix International (UI) threads, but I don't
34 know if anyone else implements these. */
41 typedef thread_key_t __gthread_key_t
;
47 typedef mutex_t __gthread_mutex_t
;
49 #define __GTHREAD_ONCE_INIT { DEFAULTMUTEX, 0 }
50 #define __GTHREAD_MUTEX_INIT DEFAULTMUTEX
52 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
54 #pragma weak thr_keycreate
55 #pragma weak thr_getspecific
56 #pragma weak thr_setspecific
57 #pragma weak thr_create
59 #pragma weak mutex_lock
60 #pragma weak mutex_trylock
61 #pragma weak mutex_unlock
65 #pragma weak thr_keycreate
66 #pragma weak thr_getprio
68 #pragma weak thr_setprio
69 #pragma weak thr_yield
71 #pragma weak cond_init
72 #pragma weak cond_destroy
73 #pragma weak cond_wait
74 #pragma weak cond_broadcast
75 #pragma weak cond_signal
77 #pragma weak mutex_init
78 #pragma weak mutex_destroy
81 /* This will not actually work in Solaris 2.5, since libc contains
82 dummy symbols of all thr_* routines. */
84 static void *__gthread_active_ptr
= &thr_create
;
87 __gthread_active_p (void)
89 return __gthread_active_ptr
!= 0;
92 #else /* not SUPPORTS_WEAK */
95 __gthread_active_p (void)
100 #endif /* SUPPORTS_WEAK */
104 /* Key structure for maintaining thread specific storage */
105 static thread_key_t _objc_thread_storage
;
107 /* Thread local storage for a single thread */
108 static void *thread_local_storage
= NULL
;
110 /* Backend initialization functions */
112 /* Initialize the threads subsystem. */
114 __gthread_objc_init_thread_system(void)
116 /* Initialize the thread storage key */
117 if (__gthread_active_p ()
118 && thr_keycreate(&_objc_thread_storage
, NULL
) == 0)
124 /* Close the threads subsystem. */
126 __gthread_objc_close_thread_system(void)
128 if (__gthread_active_p ())
134 /* Backend thread functions */
136 /* Create a new thread of execution. */
137 static inline objc_thread_t
138 __gthread_objc_thread_detach(void (*func
)(void *), void *arg
)
140 objc_thread_t thread_id
;
141 thread_t new_thread_id
= 0;
143 if (!__gthread_active_p ())
146 if (thr_create(NULL
, 0, (void *)func
, arg
,
147 THR_DETACHED
| THR_NEW_LWP
,
148 &new_thread_id
) == 0)
149 thread_id
= *(objc_thread_t
*)&new_thread_id
;
156 /* Set the current thread's priority. */
158 __gthread_objc_thread_set_priority(int priority
)
160 int sys_priority
= 0;
162 if (!__gthread_active_p ())
167 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
171 case OBJC_THREAD_BACKGROUND_PRIORITY
:
174 case OBJC_THREAD_LOW_PRIORITY
:
179 /* Change priority */
180 if (thr_setprio(thr_self(), sys_priority
) == 0)
186 /* Return the current thread's priority. */
188 __gthread_objc_thread_get_priority(void)
192 if (!__gthread_active_p ())
193 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
195 if (thr_getprio(thr_self(), &sys_priority
) == 0)
197 if (sys_priority
>= 250)
198 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
199 else if (sys_priority
>= 150)
200 return OBJC_THREAD_BACKGROUND_PRIORITY
;
201 return OBJC_THREAD_LOW_PRIORITY
;
204 /* Couldn't get priority. */
208 /* Yield our process time to another thread. */
210 __gthread_objc_thread_yield(void)
212 if (__gthread_active_p ())
216 /* Terminate the current thread. */
218 __gthread_objc_thread_exit(void)
220 if (__gthread_active_p ())
221 /* exit the thread */
222 thr_exit(&__objc_thread_exit_status
);
224 /* Failed if we reached here */
228 /* Returns an integer value which uniquely describes a thread. */
229 static inline objc_thread_t
230 __gthread_objc_thread_id(void)
232 if (__gthread_active_p ())
233 return (objc_thread_t
)thr_self();
235 return (objc_thread_t
)1;
238 /* Sets the thread's local storage pointer. */
240 __gthread_objc_thread_set_data(void *value
)
242 if (__gthread_active_p ())
244 if (thr_setspecific(_objc_thread_storage
, value
) == 0)
251 thread_local_storage
= value
;
256 /* Returns the thread's local storage pointer. */
258 __gthread_objc_thread_get_data(void)
262 if (__gthread_active_p ())
264 if (thr_getspecific(_objc_thread_storage
, &value
) == 0)
270 return thread_local_storage
;
273 /* Backend mutex functions */
275 /* Allocate a mutex. */
277 __gthread_objc_mutex_allocate(objc_mutex_t mutex
)
279 if (__gthread_active_p ()
280 && mutex_init( (mutex_t
*)(&(mutex
->backend
)), USYNC_THREAD
, 0))
286 /* Deallocate a mutex. */
288 __gthread_objc_mutex_deallocate(objc_mutex_t mutex
)
290 if (__gthread_active_p ())
291 mutex_destroy((mutex_t
*)(&(mutex
->backend
)));
296 /* Grab a lock on a mutex. */
298 __gthread_objc_mutex_lock(objc_mutex_t mutex
)
300 if (__gthread_active_p ()
301 && mutex_lock((mutex_t
*)(&(mutex
->backend
))) != 0)
307 /* Try to grab a lock on a mutex. */
309 __gthread_objc_mutex_trylock(objc_mutex_t mutex
)
311 if (__gthread_active_p ()
312 && mutex_trylock((mutex_t
*)(&(mutex
->backend
))) != 0)
318 /* Unlock the mutex */
320 __gthread_objc_mutex_unlock(objc_mutex_t mutex
)
322 if (__gthread_active_p ()
323 && mutex_unlock((mutex_t
*)(&(mutex
->backend
))) != 0)
329 /* Backend condition mutex functions */
331 /* Allocate a condition. */
333 __gthread_objc_condition_allocate(objc_condition_t condition
)
335 if (__gthread_active_p ())
336 return cond_init((cond_t
*)(&(condition
->backend
)), USYNC_THREAD
,
342 /* Deallocate a condition. */
344 __gthread_objc_condition_deallocate(objc_condition_t condition
)
346 if (__gthread_active_p ())
347 return cond_destroy((cond_t
*)(&(condition
->backend
)));
352 /* Wait on the condition */
354 __gthread_objc_condition_wait(objc_condition_t condition
, objc_mutex_t mutex
)
356 if (__gthread_active_p ())
357 return cond_wait((cond_t
*)(&(condition
->backend
)),
358 (mutex_t
*)(&(mutex
->backend
)));
363 /* Wake up all threads waiting on this condition. */
365 __gthread_objc_condition_broadcast(objc_condition_t condition
)
367 if (__gthread_active_p ())
368 return cond_broadcast((cond_t
*)(&(condition
->backend
)));
373 /* Wake up one thread waiting on this condition. */
375 __gthread_objc_condition_signal(objc_condition_t condition
)
377 if (__gthread_active_p ())
378 return cond_signal((cond_t
*)(&(condition
->backend
)));
386 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
388 if (! __gthread_active_p ())
391 if (once
== 0 || func
== 0)
396 int status
= mutex_lock (&once
->mutex
);
404 mutex_unlock (&once
->mutex
);
410 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
412 /* Solaris 2.5 contains thr_* routines no-op in libc, so test if we actually
413 got a reasonable key value, and if not, fail. */
415 if (thr_keycreate (key
, dtor
) != 0 || *key
== -1)
422 __gthread_key_dtor (__gthread_key_t key
, void *ptr
)
424 /* Nothing needed. */
429 __gthread_key_delete (__gthread_key_t key
)
436 __gthread_getspecific (__gthread_key_t key
)
439 if (thr_getspecific (key
, &ptr
) == 0)
446 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
448 return thr_setspecific (key
, (void *) ptr
);
452 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
454 if (__gthread_active_p ())
455 return mutex_lock (mutex
);
461 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
463 if (__gthread_active_p ())
464 return mutex_trylock (mutex
);
470 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
472 if (__gthread_active_p ())
473 return mutex_unlock (mutex
);
478 #endif /* _LIBOBJC */
480 #endif /* not __gthr_solaris_h */