]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gthr-solaris.h
final.c (output_asm_insn): Make sure assembly dialects are terminated, not nested.
[thirdparty/gcc.git] / gcc / gthr-solaris.h
CommitLineData
15794a95 1/* Threads compatibility routines for libgcc2 and libobjc. */
f24af81b 2/* Compile this one with gcc. */
15794a95 3/* Copyright (C) 1997, 1999, 2000 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
34 know if anyone else implements these. */
35
36#define __GTHREADS 1
37
38#include <thread.h>
39#include <errno.h>
40
41typedef thread_key_t __gthread_key_t;
42typedef struct
43{
44 mutex_t mutex;
45 int once;
46} __gthread_once_t;
47typedef mutex_t __gthread_mutex_t;
48
49#define __GTHREAD_ONCE_INIT { DEFAULTMUTEX, 0 }
50#define __GTHREAD_MUTEX_INIT DEFAULTMUTEX
51
52#if SUPPORTS_WEAK && GTHREAD_USE_WEAK
53
54#pragma weak thr_keycreate
55#pragma weak thr_getspecific
56#pragma weak thr_setspecific
57#pragma weak thr_create
58
59#pragma weak mutex_lock
60#pragma weak mutex_trylock
61#pragma weak mutex_unlock
62
15794a95
L
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
70
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
76
77#pragma weak mutex_init
78#pragma weak mutex_destroy
79#endif
80
f24af81b
TT
81/* This will not actually work in Solaris 2.5, since libc contains
82 dummy symbols of all thr_* routines. */
83
f5fa2384 84static void *__gthread_active_ptr = (void *) &thr_create;
f24af81b
TT
85
86static inline int
d1e51320 87__gthread_active_p (void)
f24af81b
TT
88{
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
112/* Initialize the threads subsystem. */
113static inline int
114__gthread_objc_init_thread_system(void)
115{
116 /* Initialize the thread storage key */
117 if (__gthread_active_p ()
118 && thr_keycreate(&_objc_thread_storage, NULL) == 0)
119 return 0;
120
121 return -1;
122}
123
124/* Close the threads subsystem. */
125static inline int
126__gthread_objc_close_thread_system(void)
127{
128 if (__gthread_active_p ())
129 return 0;
130 else
131 return -1;
132}
133
134/* Backend thread functions */
135
136/* Create a new thread of execution. */
137static inline objc_thread_t
138__gthread_objc_thread_detach(void (*func)(void *), void *arg)
139{
140 objc_thread_t thread_id;
141 thread_t new_thread_id = 0;
142
143 if (!__gthread_active_p ())
144 return NULL;
145
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;
152
153 return thread_id;
154}
155
156/* Set the current thread's priority. */
157static inline int
158__gthread_objc_thread_set_priority(int priority)
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 */
180 if (thr_setprio(thr_self(), sys_priority) == 0)
181 return 0;
182 else
183 return -1;
184}
185
186/* Return the current thread's priority. */
187static inline int
188__gthread_objc_thread_get_priority(void)
189{
190 int sys_priority;
191
192 if (!__gthread_active_p ())
193 return OBJC_THREAD_INTERACTIVE_PRIORITY;
194
195 if (thr_getprio(thr_self(), &sys_priority) == 0)
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
204 /* Couldn't get priority. */
205 return -1;
206}
207
208/* Yield our process time to another thread. */
209static inline void
210__gthread_objc_thread_yield(void)
211{
212 if (__gthread_active_p ())
213 thr_yield();
214}
215
216/* Terminate the current thread. */
217static inline int
218__gthread_objc_thread_exit(void)
219{
220 if (__gthread_active_p ())
221 /* exit the thread */
222 thr_exit(&__objc_thread_exit_status);
223
224 /* Failed if we reached here */
225 return -1;
226}
227
228/* Returns an integer value which uniquely describes a thread. */
229static inline objc_thread_t
230__gthread_objc_thread_id(void)
231{
232 if (__gthread_active_p ())
233 return (objc_thread_t)thr_self();
234 else
235 return (objc_thread_t)1;
236}
237
238/* Sets the thread's local storage pointer. */
239static inline int
240__gthread_objc_thread_set_data(void *value)
241{
242 if (__gthread_active_p ())
243 {
244 if (thr_setspecific(_objc_thread_storage, value) == 0)
245 return 0;
246 else
247 return -1;
248 }
249 else
250 {
251 thread_local_storage = value;
252 return 0;
253 }
254}
255
256/* Returns the thread's local storage pointer. */
257static inline void *
258__gthread_objc_thread_get_data(void)
259{
260 void *value = NULL;
261
262 if (__gthread_active_p ())
263 {
264 if (thr_getspecific(_objc_thread_storage, &value) == 0)
265 return value;
266 else
267 return NULL;
268 }
269 else
270 return thread_local_storage;
271}
272
273/* Backend mutex functions */
274
275/* Allocate a mutex. */
276static inline int
277__gthread_objc_mutex_allocate(objc_mutex_t mutex)
278{
279 if (__gthread_active_p ()
280 && mutex_init( (mutex_t *)(&(mutex->backend)), USYNC_THREAD, 0))
281 return -1;
282
283 return 0;
284}
285
286/* Deallocate a mutex. */
287static inline int
288__gthread_objc_mutex_deallocate(objc_mutex_t mutex)
289{
290 if (__gthread_active_p ())
291 mutex_destroy((mutex_t *)(&(mutex->backend)));
292
293 return 0;
294}
295
296/* Grab a lock on a mutex. */
297static inline int
298__gthread_objc_mutex_lock(objc_mutex_t mutex)
299{
300 if (__gthread_active_p ()
301 && mutex_lock((mutex_t *)(&(mutex->backend))) != 0)
302 return -1;
303
304 return 0;
305}
306
307/* Try to grab a lock on a mutex. */
308static inline int
309__gthread_objc_mutex_trylock(objc_mutex_t mutex)
310{
311 if (__gthread_active_p ()
312 && mutex_trylock((mutex_t *)(&(mutex->backend))) != 0)
313 return -1;
314
315 return 0;
316}
317
318/* Unlock the mutex */
319static inline int
320__gthread_objc_mutex_unlock(objc_mutex_t mutex)
321{
322 if (__gthread_active_p ()
323 && mutex_unlock((mutex_t *)(&(mutex->backend))) != 0)
324 return -1;
325
326 return 0;
327}
328
329/* Backend condition mutex functions */
330
331/* Allocate a condition. */
332static inline int
333__gthread_objc_condition_allocate(objc_condition_t condition)
334{
335 if (__gthread_active_p ())
336 return cond_init((cond_t *)(&(condition->backend)), USYNC_THREAD,
337 NULL);
338 else
339 return 0;
340}
341
342/* Deallocate a condition. */
343static inline int
344__gthread_objc_condition_deallocate(objc_condition_t condition)
345{
346 if (__gthread_active_p ())
347 return cond_destroy((cond_t *)(&(condition->backend)));
348 else
349 return 0;
350}
351
352/* Wait on the condition */
353static inline int
354__gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
355{
356 if (__gthread_active_p ())
357 return cond_wait((cond_t *)(&(condition->backend)),
358 (mutex_t *)(&(mutex->backend)));
359 else
360 return 0;
361}
362
363/* Wake up all threads waiting on this condition. */
364static inline int
365__gthread_objc_condition_broadcast(objc_condition_t condition)
366{
367 if (__gthread_active_p ())
368 return cond_broadcast((cond_t *)(&(condition->backend)));
369 else
370 return 0;
371}
372
373/* Wake up one thread waiting on this condition. */
374static inline int
375__gthread_objc_condition_signal(objc_condition_t condition)
376{
377 if (__gthread_active_p ())
378 return cond_signal((cond_t *)(&(condition->backend)));
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) ();
402 once->once ++;
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
413 got a reasonable key value, and if not, fail. */
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
421static inline int
422__gthread_key_dtor (__gthread_key_t key, void *ptr)
423{
424 /* Nothing needed. */
425 return 0;
426}
427
428static inline int
429__gthread_key_delete (__gthread_key_t key)
430{
431 /* Not possible. */
432 return -1;
433}
434
435static inline void *
436__gthread_getspecific (__gthread_key_t key)
437{
438 void *ptr;
439 if (thr_getspecific (key, &ptr) == 0)
440 return ptr;
441 else
442 return 0;
443}
444
445static inline int
446__gthread_setspecific (__gthread_key_t key, const void *ptr)
447{
448 return thr_setspecific (key, (void *) ptr);
449}
450
451static inline int
452__gthread_mutex_lock (__gthread_mutex_t *mutex)
453{
454 if (__gthread_active_p ())
455 return mutex_lock (mutex);
456 else
457 return 0;
458}
459
460static inline int
461__gthread_mutex_trylock (__gthread_mutex_t *mutex)
462{
463 if (__gthread_active_p ())
464 return mutex_trylock (mutex);
465 else
466 return 0;
467}
468
469static inline int
470__gthread_mutex_unlock (__gthread_mutex_t *mutex)
471{
472 if (__gthread_active_p ())
473 return mutex_unlock (mutex);
474 else
475 return 0;
476}
477
15794a95
L
478#endif /* _LIBOBJC */
479
88657302 480#endif /* ! GCC_GTHR_SOLARIS_H */