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 GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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 GCC_GTHR_SOLARIS_H
30 #define GCC_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. */
85 __gthread_active_p (void)
87 static void *const __gthread_active_ptr
= (void *) &thr_create
;
88 return __gthread_active_ptr
!= 0;
91 #else /* not SUPPORTS_WEAK */
94 __gthread_active_p (void)
99 #endif /* SUPPORTS_WEAK */
103 /* Key structure for maintaining thread specific storage */
104 static thread_key_t _objc_thread_storage
;
106 /* Thread local storage for a single thread */
107 static void *thread_local_storage
= NULL
;
109 /* Backend initialization functions */
111 /* Initialize the threads subsystem. */
113 __gthread_objc_init_thread_system(void)
115 /* Initialize the thread storage key */
116 if (__gthread_active_p ()
117 && thr_keycreate(&_objc_thread_storage
, NULL
) == 0)
123 /* Close the threads subsystem. */
125 __gthread_objc_close_thread_system(void)
127 if (__gthread_active_p ())
133 /* Backend thread functions */
135 /* Create a new thread of execution. */
136 static inline objc_thread_t
137 __gthread_objc_thread_detach(void (*func
)(void *), void *arg
)
139 objc_thread_t thread_id
;
140 thread_t new_thread_id
= 0;
142 if (!__gthread_active_p ())
145 if (thr_create(NULL
, 0, (void *)func
, arg
,
146 THR_DETACHED
| THR_NEW_LWP
,
147 &new_thread_id
) == 0)
148 thread_id
= *(objc_thread_t
*)&new_thread_id
;
155 /* Set the current thread's priority. */
157 __gthread_objc_thread_set_priority(int priority
)
159 int sys_priority
= 0;
161 if (!__gthread_active_p ())
166 case OBJC_THREAD_INTERACTIVE_PRIORITY
:
170 case OBJC_THREAD_BACKGROUND_PRIORITY
:
173 case OBJC_THREAD_LOW_PRIORITY
:
178 /* Change priority */
179 if (thr_setprio(thr_self(), sys_priority
) == 0)
185 /* Return the current thread's priority. */
187 __gthread_objc_thread_get_priority(void)
191 if (!__gthread_active_p ())
192 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
194 if (thr_getprio(thr_self(), &sys_priority
) == 0)
196 if (sys_priority
>= 250)
197 return OBJC_THREAD_INTERACTIVE_PRIORITY
;
198 else if (sys_priority
>= 150)
199 return OBJC_THREAD_BACKGROUND_PRIORITY
;
200 return OBJC_THREAD_LOW_PRIORITY
;
203 /* Couldn't get priority. */
207 /* Yield our process time to another thread. */
209 __gthread_objc_thread_yield(void)
211 if (__gthread_active_p ())
215 /* Terminate the current thread. */
217 __gthread_objc_thread_exit(void)
219 if (__gthread_active_p ())
220 /* exit the thread */
221 thr_exit(&__objc_thread_exit_status
);
223 /* Failed if we reached here */
227 /* Returns an integer value which uniquely describes a thread. */
228 static inline objc_thread_t
229 __gthread_objc_thread_id(void)
231 if (__gthread_active_p ())
232 return (objc_thread_t
)thr_self();
234 return (objc_thread_t
)1;
237 /* Sets the thread's local storage pointer. */
239 __gthread_objc_thread_set_data(void *value
)
241 if (__gthread_active_p ())
243 if (thr_setspecific(_objc_thread_storage
, value
) == 0)
250 thread_local_storage
= value
;
255 /* Returns the thread's local storage pointer. */
257 __gthread_objc_thread_get_data(void)
261 if (__gthread_active_p ())
263 if (thr_getspecific(_objc_thread_storage
, &value
) == 0)
269 return thread_local_storage
;
272 /* Backend mutex functions */
274 /* Allocate a mutex. */
276 __gthread_objc_mutex_allocate(objc_mutex_t mutex
)
278 if (__gthread_active_p ()
279 && mutex_init( (mutex_t
*)(&(mutex
->backend
)), USYNC_THREAD
, 0))
285 /* Deallocate a mutex. */
287 __gthread_objc_mutex_deallocate(objc_mutex_t mutex
)
289 if (__gthread_active_p ())
290 mutex_destroy((mutex_t
*)(&(mutex
->backend
)));
295 /* Grab a lock on a mutex. */
297 __gthread_objc_mutex_lock(objc_mutex_t mutex
)
299 if (__gthread_active_p ()
300 && mutex_lock((mutex_t
*)(&(mutex
->backend
))) != 0)
306 /* Try to grab a lock on a mutex. */
308 __gthread_objc_mutex_trylock(objc_mutex_t mutex
)
310 if (__gthread_active_p ()
311 && mutex_trylock((mutex_t
*)(&(mutex
->backend
))) != 0)
317 /* Unlock the mutex */
319 __gthread_objc_mutex_unlock(objc_mutex_t mutex
)
321 if (__gthread_active_p ()
322 && mutex_unlock((mutex_t
*)(&(mutex
->backend
))) != 0)
328 /* Backend condition mutex functions */
330 /* Allocate a condition. */
332 __gthread_objc_condition_allocate(objc_condition_t condition
)
334 if (__gthread_active_p ())
335 return cond_init((cond_t
*)(&(condition
->backend
)), USYNC_THREAD
,
341 /* Deallocate a condition. */
343 __gthread_objc_condition_deallocate(objc_condition_t condition
)
345 if (__gthread_active_p ())
346 return cond_destroy((cond_t
*)(&(condition
->backend
)));
351 /* Wait on the condition */
353 __gthread_objc_condition_wait(objc_condition_t condition
, objc_mutex_t mutex
)
355 if (__gthread_active_p ())
356 return cond_wait((cond_t
*)(&(condition
->backend
)),
357 (mutex_t
*)(&(mutex
->backend
)));
362 /* Wake up all threads waiting on this condition. */
364 __gthread_objc_condition_broadcast(objc_condition_t condition
)
366 if (__gthread_active_p ())
367 return cond_broadcast((cond_t
*)(&(condition
->backend
)));
372 /* Wake up one thread waiting on this condition. */
374 __gthread_objc_condition_signal(objc_condition_t condition
)
376 if (__gthread_active_p ())
377 return cond_signal((cond_t
*)(&(condition
->backend
)));
385 __gthread_once (__gthread_once_t
*once
, void (*func
) (void))
387 if (! __gthread_active_p ())
390 if (once
== 0 || func
== 0)
395 int status
= mutex_lock (&once
->mutex
);
403 mutex_unlock (&once
->mutex
);
409 __gthread_key_create (__gthread_key_t
*key
, void (*dtor
) (void *))
411 /* Solaris 2.5 contains thr_* routines no-op in libc, so test if we actually
412 got a reasonable key value, and if not, fail. */
414 if (thr_keycreate (key
, dtor
) != 0 || *key
== -1)
421 __gthread_key_dtor (__gthread_key_t key
, void *ptr
)
423 /* Nothing needed. */
428 __gthread_key_delete (__gthread_key_t key
)
435 __gthread_getspecific (__gthread_key_t key
)
438 if (thr_getspecific (key
, &ptr
) == 0)
445 __gthread_setspecific (__gthread_key_t key
, const void *ptr
)
447 return thr_setspecific (key
, (void *) ptr
);
451 __gthread_mutex_lock (__gthread_mutex_t
*mutex
)
453 if (__gthread_active_p ())
454 return mutex_lock (mutex
);
460 __gthread_mutex_trylock (__gthread_mutex_t
*mutex
)
462 if (__gthread_active_p ())
463 return mutex_trylock (mutex
);
469 __gthread_mutex_unlock (__gthread_mutex_t
*mutex
)
471 if (__gthread_active_p ())
472 return mutex_unlock (mutex
);
477 #endif /* _LIBOBJC */
479 #endif /* ! GCC_GTHR_SOLARIS_H */