* invoke.texi: Add more options to summary list.
[official-gcc.git] / gcc / gthr-solaris.h
blobfca3084f9a70599280438f3223875973701a7edc
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)
10 any later version.
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. */
36 #define __GTHREADS 1
38 #include <thread.h>
39 #include <errno.h>
41 typedef thread_key_t __gthread_key_t;
42 typedef struct
44 mutex_t mutex;
45 int once;
46 } __gthread_once_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
63 #ifdef _LIBOBJC
64 #pragma weak thr_exit
65 #pragma weak thr_keycreate
66 #pragma weak thr_getprio
67 #pragma weak thr_self
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
79 #endif
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;
86 static inline int
87 __gthread_active_p (void)
89 return __gthread_active_ptr != 0;
92 #else /* not SUPPORTS_WEAK */
94 static inline int
95 __gthread_active_p (void)
97 return 1;
100 #endif /* SUPPORTS_WEAK */
102 #ifdef _LIBOBJC
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. */
113 static inline int
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)
119 return 0;
121 return -1;
124 /* Close the threads subsystem. */
125 static inline int
126 __gthread_objc_close_thread_system(void)
128 if (__gthread_active_p ())
129 return 0;
130 else
131 return -1;
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 ())
144 return NULL;
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;
150 else
151 thread_id = NULL;
153 return thread_id;
156 /* Set the current thread's priority. */
157 static inline int
158 __gthread_objc_thread_set_priority(int priority)
160 int sys_priority = 0;
162 if (!__gthread_active_p ())
163 return -1;
165 switch (priority)
167 case OBJC_THREAD_INTERACTIVE_PRIORITY:
168 sys_priority = 300;
169 break;
170 default:
171 case OBJC_THREAD_BACKGROUND_PRIORITY:
172 sys_priority = 200;
173 break;
174 case OBJC_THREAD_LOW_PRIORITY:
175 sys_priority = 1000;
176 break;
179 /* Change priority */
180 if (thr_setprio(thr_self(), sys_priority) == 0)
181 return 0;
182 else
183 return -1;
186 /* Return the current thread's priority. */
187 static inline int
188 __gthread_objc_thread_get_priority(void)
190 int sys_priority;
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. */
205 return -1;
208 /* Yield our process time to another thread. */
209 static inline void
210 __gthread_objc_thread_yield(void)
212 if (__gthread_active_p ())
213 thr_yield();
216 /* Terminate the current thread. */
217 static inline int
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 */
225 return -1;
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();
234 else
235 return (objc_thread_t)1;
238 /* Sets the thread's local storage pointer. */
239 static inline int
240 __gthread_objc_thread_set_data(void *value)
242 if (__gthread_active_p ())
244 if (thr_setspecific(_objc_thread_storage, value) == 0)
245 return 0;
246 else
247 return -1;
249 else
251 thread_local_storage = value;
252 return 0;
256 /* Returns the thread's local storage pointer. */
257 static inline void *
258 __gthread_objc_thread_get_data(void)
260 void *value = NULL;
262 if (__gthread_active_p ())
264 if (thr_getspecific(_objc_thread_storage, &value) == 0)
265 return value;
266 else
267 return NULL;
269 else
270 return thread_local_storage;
273 /* Backend mutex functions */
275 /* Allocate a mutex. */
276 static inline int
277 __gthread_objc_mutex_allocate(objc_mutex_t mutex)
279 if (__gthread_active_p ()
280 && mutex_init( (mutex_t *)(&(mutex->backend)), USYNC_THREAD, 0))
281 return -1;
283 return 0;
286 /* Deallocate a mutex. */
287 static inline int
288 __gthread_objc_mutex_deallocate(objc_mutex_t mutex)
290 if (__gthread_active_p ())
291 mutex_destroy((mutex_t *)(&(mutex->backend)));
293 return 0;
296 /* Grab a lock on a mutex. */
297 static inline int
298 __gthread_objc_mutex_lock(objc_mutex_t mutex)
300 if (__gthread_active_p ()
301 && mutex_lock((mutex_t *)(&(mutex->backend))) != 0)
302 return -1;
304 return 0;
307 /* Try to grab a lock on a mutex. */
308 static inline int
309 __gthread_objc_mutex_trylock(objc_mutex_t mutex)
311 if (__gthread_active_p ()
312 && mutex_trylock((mutex_t *)(&(mutex->backend))) != 0)
313 return -1;
315 return 0;
318 /* Unlock the mutex */
319 static inline int
320 __gthread_objc_mutex_unlock(objc_mutex_t mutex)
322 if (__gthread_active_p ()
323 && mutex_unlock((mutex_t *)(&(mutex->backend))) != 0)
324 return -1;
326 return 0;
329 /* Backend condition mutex functions */
331 /* Allocate a condition. */
332 static inline int
333 __gthread_objc_condition_allocate(objc_condition_t condition)
335 if (__gthread_active_p ())
336 return cond_init((cond_t *)(&(condition->backend)), USYNC_THREAD,
337 NULL);
338 else
339 return 0;
342 /* Deallocate a condition. */
343 static inline int
344 __gthread_objc_condition_deallocate(objc_condition_t condition)
346 if (__gthread_active_p ())
347 return cond_destroy((cond_t *)(&(condition->backend)));
348 else
349 return 0;
352 /* Wait on the condition */
353 static inline int
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)));
359 else
360 return 0;
363 /* Wake up all threads waiting on this condition. */
364 static inline int
365 __gthread_objc_condition_broadcast(objc_condition_t condition)
367 if (__gthread_active_p ())
368 return cond_broadcast((cond_t *)(&(condition->backend)));
369 else
370 return 0;
373 /* Wake up one thread waiting on this condition. */
374 static inline int
375 __gthread_objc_condition_signal(objc_condition_t condition)
377 if (__gthread_active_p ())
378 return cond_signal((cond_t *)(&(condition->backend)));
379 else
380 return 0;
383 #else /* _LIBOBJC */
385 static inline int
386 __gthread_once (__gthread_once_t *once, void (*func) (void))
388 if (! __gthread_active_p ())
389 return -1;
391 if (once == 0 || func == 0)
392 return EINVAL;
394 if (once->once == 0)
396 int status = mutex_lock (&once->mutex);
397 if (status != 0)
398 return status;
399 if (once->once == 0)
401 (*func) ();
402 once->once ++;
404 mutex_unlock (&once->mutex);
406 return 0;
409 static inline int
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. */
414 *key = -1;
415 if (thr_keycreate (key, dtor) != 0 || *key == -1)
416 return -1;
417 else
418 return 0;
421 static inline int
422 __gthread_key_dtor (__gthread_key_t key, void *ptr)
424 /* Nothing needed. */
425 return 0;
428 static inline int
429 __gthread_key_delete (__gthread_key_t key)
431 /* Not possible. */
432 return -1;
435 static inline void *
436 __gthread_getspecific (__gthread_key_t key)
438 void *ptr;
439 if (thr_getspecific (key, &ptr) == 0)
440 return ptr;
441 else
442 return 0;
445 static inline int
446 __gthread_setspecific (__gthread_key_t key, const void *ptr)
448 return thr_setspecific (key, (void *) ptr);
451 static inline int
452 __gthread_mutex_lock (__gthread_mutex_t *mutex)
454 if (__gthread_active_p ())
455 return mutex_lock (mutex);
456 else
457 return 0;
460 static inline int
461 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
463 if (__gthread_active_p ())
464 return mutex_trylock (mutex);
465 else
466 return 0;
469 static inline int
470 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
472 if (__gthread_active_p ())
473 return mutex_unlock (mutex);
474 else
475 return 0;
478 #endif /* _LIBOBJC */
480 #endif /* not __gthr_solaris_h */