]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gthr-solaris.h
Fix formatting nit.
[thirdparty/gcc.git] / gcc / gthr-solaris.h
CommitLineData
15794a95 1/* Threads compatibility routines for libgcc2 and libobjc. */
f24af81b 2/* Compile this one with gcc. */
279b5b3c 3/* Copyright (C) 1997, 1999, 2000, 2004 Free Software Foundation, Inc.
f24af81b 4
1322177d 5This file is part of GCC.
f24af81b 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
f24af81b 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
f24af81b
TT
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
f24af81b
TT
21
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. */
28
88657302
RH
29#ifndef GCC_GTHR_SOLARIS_H
30#define GCC_GTHR_SOLARIS_H
f24af81b
TT
31
32/* Solaris threads as found in Solaris 2.[456].
33 Actually these are Unix International (UI) threads, but I don't
71287280 34 know if anyone else implements these. */
f24af81b
TT
35
36#define __GTHREADS 1
37
38#include <thread.h>
39#include <errno.h>
40
41typedef thread_key_t __gthread_key_t;
e6179f45 42typedef struct {
f24af81b
TT
43 mutex_t mutex;
44 int once;
45} __gthread_once_t;
46typedef mutex_t __gthread_mutex_t;
40aac948 47typedef mutex_t __gthread_recursive_mutex_t;
f24af81b
TT
48
49#define __GTHREAD_ONCE_INIT { DEFAULTMUTEX, 0 }
50#define __GTHREAD_MUTEX_INIT DEFAULTMUTEX
40aac948 51#define __GTHREAD_RECURSIVE_MUTEX_INIT RECURSIVE_ERRORCHECKMUTEX
f24af81b
TT
52
53#if SUPPORTS_WEAK && GTHREAD_USE_WEAK
54
55#pragma weak thr_keycreate
56#pragma weak thr_getspecific
57#pragma weak thr_setspecific
58#pragma weak thr_create
59
60#pragma weak mutex_lock
61#pragma weak mutex_trylock
62#pragma weak mutex_unlock
63
15794a95
L
64#ifdef _LIBOBJC
65#pragma weak thr_exit
66#pragma weak thr_keycreate
67#pragma weak thr_getprio
68#pragma weak thr_self
69#pragma weak thr_setprio
70#pragma weak thr_yield
71
72#pragma weak cond_init
73#pragma weak cond_destroy
74#pragma weak cond_wait
75#pragma weak cond_broadcast
76#pragma weak cond_signal
77
78#pragma weak mutex_init
79#pragma weak mutex_destroy
80#endif
81
f24af81b 82/* This will not actually work in Solaris 2.5, since libc contains
71287280 83 dummy symbols of all thr_* routines. */
f24af81b 84
f24af81b 85static inline int
d1e51320 86__gthread_active_p (void)
f24af81b 87{
7a8de19b 88 static void *const __gthread_active_ptr = (void *) &thr_create;
f24af81b
TT
89 return __gthread_active_ptr != 0;
90}
91
92#else /* not SUPPORTS_WEAK */
93
94static inline int
d1e51320 95__gthread_active_p (void)
f24af81b
TT
96{
97 return 1;
98}
99
100#endif /* SUPPORTS_WEAK */
101
15794a95
L
102#ifdef _LIBOBJC
103
104/* Key structure for maintaining thread specific storage */
105static thread_key_t _objc_thread_storage;
106
107/* Thread local storage for a single thread */
108static void *thread_local_storage = NULL;
109
110/* Backend initialization functions */
111
71287280 112/* Initialize the threads subsystem. */
15794a95 113static inline int
e6179f45 114__gthread_objc_init_thread_system (void)
15794a95 115{
ea4b7848 116 /* Initialize the thread storage key. */
15794a95 117 if (__gthread_active_p ()
e6179f45 118 && thr_keycreate (&_objc_thread_storage, NULL) == 0)
15794a95
L
119 return 0;
120
121 return -1;
122}
123
71287280 124/* Close the threads subsystem. */
15794a95 125static inline int
e6179f45 126__gthread_objc_close_thread_system (void)
15794a95
L
127{
128 if (__gthread_active_p ())
129 return 0;
130 else
131 return -1;
132}
133
134/* Backend thread functions */
135
71287280 136/* Create a new thread of execution. */
15794a95 137static inline objc_thread_t
e6179f45 138__gthread_objc_thread_detach (void (*func)(void *), void *arg)
15794a95
L
139{
140 objc_thread_t thread_id;
141 thread_t new_thread_id = 0;
142
143 if (!__gthread_active_p ())
144 return NULL;
589005ff 145
e6179f45
KH
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;
15794a95
L
150 else
151 thread_id = NULL;
589005ff 152
15794a95
L
153 return thread_id;
154}
155
71287280 156/* Set the current thread's priority. */
15794a95 157static inline int
e6179f45 158__gthread_objc_thread_set_priority (int priority)
15794a95
L
159{
160 int sys_priority = 0;
161
162 if (!__gthread_active_p ())
163 return -1;
164
165 switch (priority)
166 {
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;
177 }
178
179 /* Change priority */
e6179f45 180 if (thr_setprio (thr_self (), sys_priority) == 0)
15794a95
L
181 return 0;
182 else
183 return -1;
184}
185
71287280 186/* Return the current thread's priority. */
15794a95 187static inline int
e6179f45 188__gthread_objc_thread_get_priority (void)
15794a95
L
189{
190 int sys_priority;
191
192 if (!__gthread_active_p ())
193 return OBJC_THREAD_INTERACTIVE_PRIORITY;
589005ff 194
e6179f45 195 if (thr_getprio (thr_self (), &sys_priority) == 0)
15794a95
L
196 {
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;
202 }
203
71287280 204 /* Couldn't get priority. */
15794a95
L
205 return -1;
206}
207
71287280 208/* Yield our process time to another thread. */
15794a95 209static inline void
e6179f45 210__gthread_objc_thread_yield (void)
15794a95
L
211{
212 if (__gthread_active_p ())
e6179f45 213 thr_yield ();
15794a95
L
214}
215
71287280 216/* Terminate the current thread. */
15794a95 217static inline int
e6179f45 218__gthread_objc_thread_exit (void)
15794a95
L
219{
220 if (__gthread_active_p ())
221 /* exit the thread */
e6179f45 222 thr_exit (&__objc_thread_exit_status);
15794a95
L
223
224 /* Failed if we reached here */
225 return -1;
226}
227
71287280 228/* Returns an integer value which uniquely describes a thread. */
15794a95 229static inline objc_thread_t
e6179f45 230__gthread_objc_thread_id (void)
15794a95
L
231{
232 if (__gthread_active_p ())
e6179f45 233 return (objc_thread_t) thr_self ();
15794a95 234 else
e6179f45 235 return (objc_thread_t) 1;
15794a95
L
236}
237
71287280 238/* Sets the thread's local storage pointer. */
15794a95 239static inline int
e6179f45 240__gthread_objc_thread_set_data (void *value)
15794a95
L
241{
242 if (__gthread_active_p ())
243 {
e6179f45 244 if (thr_setspecific (_objc_thread_storage, value) == 0)
15794a95
L
245 return 0;
246 else
247 return -1;
248 }
249 else
250 {
251 thread_local_storage = value;
252 return 0;
253 }
254}
255
71287280 256/* Returns the thread's local storage pointer. */
15794a95 257static inline void *
e6179f45 258__gthread_objc_thread_get_data (void)
15794a95
L
259{
260 void *value = NULL;
261
262 if (__gthread_active_p ())
263 {
e6179f45 264 if (thr_getspecific (_objc_thread_storage, &value) == 0)
15794a95
L
265 return value;
266 else
267 return NULL;
268 }
269 else
270 return thread_local_storage;
271}
272
273/* Backend mutex functions */
274
71287280 275/* Allocate a mutex. */
15794a95 276static inline int
e6179f45 277__gthread_objc_mutex_allocate (objc_mutex_t mutex)
15794a95
L
278{
279 if (__gthread_active_p ()
e6179f45 280 && mutex_init ((mutex_t *) (&(mutex->backend)), USYNC_THREAD, 0))
15794a95
L
281 return -1;
282
283 return 0;
284}
285
71287280 286/* Deallocate a mutex. */
15794a95 287static inline int
e6179f45 288__gthread_objc_mutex_deallocate (objc_mutex_t mutex)
15794a95
L
289{
290 if (__gthread_active_p ())
e6179f45 291 mutex_destroy ((mutex_t *) (&(mutex->backend)));
15794a95
L
292
293 return 0;
294}
295
71287280 296/* Grab a lock on a mutex. */
15794a95 297static inline int
e6179f45 298__gthread_objc_mutex_lock (objc_mutex_t mutex)
15794a95
L
299{
300 if (__gthread_active_p ()
e6179f45 301 && mutex_lock ((mutex_t *) (&(mutex->backend))) != 0)
15794a95
L
302 return -1;
303
304 return 0;
305}
306
71287280 307/* Try to grab a lock on a mutex. */
15794a95 308static inline int
e6179f45 309__gthread_objc_mutex_trylock (objc_mutex_t mutex)
15794a95
L
310{
311 if (__gthread_active_p ()
e6179f45 312 && mutex_trylock ((mutex_t *) (&(mutex->backend))) != 0)
15794a95
L
313 return -1;
314
315 return 0;
316}
317
318/* Unlock the mutex */
319static inline int
e6179f45 320__gthread_objc_mutex_unlock (objc_mutex_t mutex)
15794a95
L
321{
322 if (__gthread_active_p ()
e6179f45 323 && mutex_unlock ((mutex_t *) (&(mutex->backend))) != 0)
15794a95
L
324 return -1;
325
326 return 0;
327}
328
329/* Backend condition mutex functions */
330
71287280 331/* Allocate a condition. */
15794a95 332static inline int
e6179f45 333__gthread_objc_condition_allocate (objc_condition_t condition)
15794a95
L
334{
335 if (__gthread_active_p ())
e6179f45
KH
336 return cond_init ((cond_t *) (&(condition->backend)), USYNC_THREAD,
337 NULL);
15794a95
L
338 else
339 return 0;
340}
341
71287280 342/* Deallocate a condition. */
15794a95 343static inline int
e6179f45 344__gthread_objc_condition_deallocate (objc_condition_t condition)
15794a95
L
345{
346 if (__gthread_active_p ())
e6179f45 347 return cond_destroy ((cond_t *) (&(condition->backend)));
15794a95
L
348 else
349 return 0;
350}
351
352/* Wait on the condition */
353static inline int
e6179f45 354__gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
15794a95
L
355{
356 if (__gthread_active_p ())
e6179f45
KH
357 return cond_wait ((cond_t *) (&(condition->backend)),
358 (mutex_t *) (&(mutex->backend)));
15794a95
L
359 else
360 return 0;
361}
362
71287280 363/* Wake up all threads waiting on this condition. */
15794a95 364static inline int
e6179f45 365__gthread_objc_condition_broadcast (objc_condition_t condition)
15794a95
L
366{
367 if (__gthread_active_p ())
e6179f45 368 return cond_broadcast ((cond_t *) (&(condition->backend)));
15794a95
L
369 else
370 return 0;
371}
372
71287280 373/* Wake up one thread waiting on this condition. */
15794a95 374static inline int
e6179f45 375__gthread_objc_condition_signal (objc_condition_t condition)
15794a95
L
376{
377 if (__gthread_active_p ())
e6179f45 378 return cond_signal ((cond_t *) (&(condition->backend)));
15794a95
L
379 else
380 return 0;
381}
382
383#else /* _LIBOBJC */
384
f24af81b 385static inline int
d1e51320 386__gthread_once (__gthread_once_t *once, void (*func) (void))
f24af81b
TT
387{
388 if (! __gthread_active_p ())
389 return -1;
390
391 if (once == 0 || func == 0)
754d1a92 392 return EINVAL;
f24af81b
TT
393
394 if (once->once == 0)
395 {
754d1a92
TT
396 int status = mutex_lock (&once->mutex);
397 if (status != 0)
398 return status;
f24af81b
TT
399 if (once->once == 0)
400 {
401 (*func) ();
e6179f45 402 once->once++;
f24af81b
TT
403 }
404 mutex_unlock (&once->mutex);
405 }
406 return 0;
407}
408
409static inline int
410__gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
411{
412 /* Solaris 2.5 contains thr_* routines no-op in libc, so test if we actually
71287280 413 got a reasonable key value, and if not, fail. */
f24af81b 414 *key = -1;
754d1a92 415 if (thr_keycreate (key, dtor) != 0 || *key == -1)
f24af81b
TT
416 return -1;
417 else
418 return 0;
419}
420
f24af81b
TT
421static inline int
422__gthread_key_delete (__gthread_key_t key)
423{
71287280 424 /* Not possible. */
f24af81b
TT
425 return -1;
426}
427
428static inline void *
429__gthread_getspecific (__gthread_key_t key)
430{
431 void *ptr;
432 if (thr_getspecific (key, &ptr) == 0)
433 return ptr;
434 else
435 return 0;
436}
437
438static inline int
439__gthread_setspecific (__gthread_key_t key, const void *ptr)
440{
441 return thr_setspecific (key, (void *) ptr);
442}
443
444static inline int
445__gthread_mutex_lock (__gthread_mutex_t *mutex)
446{
447 if (__gthread_active_p ())
448 return mutex_lock (mutex);
449 else
450 return 0;
451}
452
453static inline int
454__gthread_mutex_trylock (__gthread_mutex_t *mutex)
455{
456 if (__gthread_active_p ())
457 return mutex_trylock (mutex);
458 else
459 return 0;
460}
461
462static inline int
463__gthread_mutex_unlock (__gthread_mutex_t *mutex)
464{
465 if (__gthread_active_p ())
466 return mutex_unlock (mutex);
467 else
468 return 0;
469}
470
40aac948
JM
471static inline int
472__gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
473{
474 return __gthread_mutex_lock (mutex);
475}
476
477static inline int
478__gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
479{
480 return __gthread_mutex_trylock (mutex);
481}
482
483static inline int
484__gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
485{
486 return __gthread_mutex_unlock (mutex);
487}
488
15794a95
L
489#endif /* _LIBOBJC */
490
88657302 491#endif /* ! GCC_GTHR_SOLARIS_H */