]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gthr-dce.h
final.c (output_asm_insn): Make sure assembly dialects are terminated, not nested.
[thirdparty/gcc.git] / gcc / gthr-dce.h
CommitLineData
f24af81b
TT
1
2/* Compile this one with gcc. */
5467baef 3/* Copyright (C) 1997, 1999, 2000, 2001 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_DCE_H
30#define GCC_GTHR_DCE_H
f24af81b
TT
31
32/* DCE threads interface.
33 DCE threads are based on POSIX threads draft 4, and many things
34 have changed since then. */
35
36#define __GTHREADS 1
37
38#include <pthread.h>
39
5467baef
JDA
40#ifdef __cplusplus
41#define UNUSED(x) x
42#else
43#define UNUSED(x) x __attribute__((unused))
44#endif
45
f24af81b
TT
46typedef pthread_key_t __gthread_key_t;
47typedef pthread_once_t __gthread_once_t;
48typedef pthread_mutex_t __gthread_mutex_t;
49
50#define __GTHREAD_ONCE_INIT pthread_once_init
5241c227
JDA
51
52#define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
53
54#define __GTHREAD_MUTEX_INIT_DEFAULT pthread_once_init
f24af81b
TT
55
56#if SUPPORTS_WEAK && GTHREAD_USE_WEAK
57
58#pragma weak pthread_once
59#pragma weak pthread_once_init
15794a95 60#pragma weak pthread_keycreate
f24af81b
TT
61#pragma weak pthread_key_delete
62#pragma weak pthread_getspecific
63#pragma weak pthread_setspecific
64#pragma weak pthread_create
5241c227 65#pragma weak pthread_mutex_init
f24af81b
TT
66#pragma weak pthread_mutex_lock
67#pragma weak pthread_mutex_trylock
68#pragma weak pthread_mutex_unlock
69
15794a95
L
70#ifdef _LIBOBJC
71/* Objective C. */
72#pragma weak pthread_cond_broadcast
73#pragma weak pthread_cond_destroy
74#pragma weak pthread_cond_init
75#pragma weak pthread_cond_signal
76#pragma weak pthread_cond_wait
77#pragma weak pthread_exit
78#pragma weak pthread_getunique_np
15794a95
L
79#pragma weak pthread_mutex_destroy
80#pragma weak pthread_self
81#pragma weak pthread_yield
82#endif
83
f5fa2384 84static void *__gthread_active_ptr = (void *) &pthread_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 pthread_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 if (__gthread_active_p ())
117 /* Initialize the thread storage key */
118 return pthread_keycreate (&_objc_thread_storage, NULL);
119 else
120 return -1;
121}
122
123/* Close the threads subsystem. */
124static inline int
125__gthread_objc_close_thread_system(void)
126{
127 if (__gthread_active_p ())
128 return 0;
129 else
130 return -1;
131}
132
133/* Backend thread functions */
134
135/* Create a new thread of execution. */
136static inline objc_thread_t
137__gthread_objc_thread_detach(void (*func)(void *), void *arg)
138{
139 objc_thread_t thread_id;
140 pthread_t new_thread_handle;
141
142 if (!__gthread_active_p ())
143 return NULL;
144
145 if ( !(pthread_create(&new_thread_handle, pthread_attr_default,
146 (void *)func, arg)) )
147 {
148 /* ??? May not work! (64bit) */
149 thread_id = *(objc_thread_t *)&new_thread_handle;
150 pthread_detach(&new_thread_handle); /* Fully detach thread. */
151 }
152 else
153 thread_id = NULL;
154
155 return thread_id;
156}
157
158/* Set the current thread's priority. */
159static inline int
160__gthread_objc_thread_set_priority(int priority)
161{
162 int sys_priority = 0;
163
164 if (!__gthread_active_p ())
165 return -1;
166
167 switch (priority)
168 {
169 case OBJC_THREAD_INTERACTIVE_PRIORITY:
170 sys_priority = (PRI_FG_MIN_NP + PRI_FG_MAX_NP) / 2;
171 break;
172 default:
173 case OBJC_THREAD_BACKGROUND_PRIORITY:
174 sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
175 break;
176 case OBJC_THREAD_LOW_PRIORITY:
177 sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
178 break;
179 }
180
181 /* Change the priority. */
182 if (pthread_setprio(pthread_self(), sys_priority) >= 0)
183 return 0;
184 else
185 /* Failed */
186 return -1;
187}
188
189/* Return the current thread's priority. */
190static inline int
191__gthread_objc_thread_get_priority(void)
192{
193 int sys_priority;
194
195 if (__gthread_active_p ())
196 {
197 if ((sys_priority = pthread_getprio(pthread_self())) >= 0)
198 {
199 if (sys_priority >= PRI_FG_MIN_NP
200 && sys_priority <= PRI_FG_MAX_NP)
201 return OBJC_THREAD_INTERACTIVE_PRIORITY;
202 if (sys_priority >= PRI_BG_MIN_NP
203 && sys_priority <= PRI_BG_MAX_NP)
204 return OBJC_THREAD_BACKGROUND_PRIORITY;
205 return OBJC_THREAD_LOW_PRIORITY;
206 }
207
208 /* Failed */
209 return -1;
210 }
211 else
212 return OBJC_THREAD_INTERACTIVE_PRIORITY;
213}
214
215/* Yield our process time to another thread. */
216static inline void
217__gthread_objc_thread_yield(void)
218{
219 if (__gthread_active_p ())
220 pthread_yield();
221}
222
223/* Terminate the current thread. */
224static inline int
225__gthread_objc_thread_exit(void)
226{
227 if (__gthread_active_p ())
228 /* exit the thread */
229 pthread_exit(&__objc_thread_exit_status);
230
231 /* Failed if we reached here */
232 return -1;
233}
234
235/* Returns an integer value which uniquely describes a thread. */
236static inline objc_thread_t
237__gthread_objc_thread_id(void)
238{
239 if (__gthread_active_p ())
240 {
241 pthread_t self = pthread_self();
242
243 return (objc_thread_t) pthread_getunique_np (&self);
244 }
245 else
246 return (objc_thread_t)1;
247}
248
249/* Sets the thread's local storage pointer. */
250static inline int
251__gthread_objc_thread_set_data(void *value)
252{
253 if (__gthread_active_p ())
254 return pthread_setspecific(_objc_thread_storage, value);
255 else
256 {
257 thread_local_storage = value;
258 return 0;
259 }
260}
261
262/* Returns the thread's local storage pointer. */
263static inline void *
264__gthread_objc_thread_get_data(void)
265{
266 void *value = NULL;
267
268 if (__gthread_active_p ())
269 {
270 if ( !(pthread_getspecific(_objc_thread_storage, &value)) )
271 return value;
272
273 return NULL;
274 }
275 else
276 return thread_local_storage;
277}
278
279/* Backend mutex functions */
280
281/* Allocate a mutex. */
282static inline int
283__gthread_objc_mutex_allocate(objc_mutex_t mutex)
284{
802a8181
DA
285 if (__gthread_active_p ())
286 {
287 mutex->backend = objc_malloc(sizeof(pthread_mutex_t));
288
289 if (pthread_mutex_init((pthread_mutex_t *)mutex->backend,
15794a95 290 pthread_mutexattr_default))
802a8181
DA
291 {
292 objc_free(mutex->backend);
293 mutex->backend = NULL;
294 return -1;
295 }
296 }
15794a95
L
297
298 return 0;
299}
300
301/* Deallocate a mutex. */
302static inline int
303__gthread_objc_mutex_deallocate(objc_mutex_t mutex)
304{
6bb92770
DA
305 if (__gthread_active_p ())
306 {
307 if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
308 return -1;
309
310 objc_free(mutex->backend);
311 mutex->backend = NULL;
312 }
15794a95
L
313
314 return 0;
315}
316
317/* Grab a lock on a mutex. */
318static inline int
319__gthread_objc_mutex_lock(objc_mutex_t mutex)
320{
321 if (__gthread_active_p ())
322 return pthread_mutex_lock((pthread_mutex_t *)mutex->backend);
323 else
324 return 0;
325}
326
327/* Try to grab a lock on a mutex. */
328static inline int
329__gthread_objc_mutex_trylock(objc_mutex_t mutex)
330{
331 if (__gthread_active_p ()
332 && pthread_mutex_trylock((pthread_mutex_t *)mutex->backend) != 1)
333 return -1;
334
335 return 0;
336}
337
338/* Unlock the mutex */
339static inline int
340__gthread_objc_mutex_unlock(objc_mutex_t mutex)
341{
342 if (__gthread_active_p ())
343 return pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
344 else
345 return 0;
346}
347
348/* Backend condition mutex functions */
349
350/* Allocate a condition. */
351static inline int
352__gthread_objc_condition_allocate(objc_condition_t condition)
353{
c54dbf8b 354 if (__gthread_active_p ())
15794a95
L
355 /* Unimplemented. */
356 return -1;
357 else
358 return 0;
359}
360
361/* Deallocate a condition. */
362static inline int
363__gthread_objc_condition_deallocate(objc_condition_t condition)
364{
365 if (__gthread_active_p ())
366 /* Unimplemented. */
367 return -1;
368 else
369 return 0;
370}
371
372/* Wait on the condition */
373static inline int
374__gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
375{
376 if (__gthread_active_p ())
377 /* Unimplemented. */
378 return -1;
379 else
380 return 0;
381}
382
383/* Wake up all threads waiting on this condition. */
384static inline int
385__gthread_objc_condition_broadcast(objc_condition_t condition)
386{
387 if (__gthread_active_p ())
388 /* Unimplemented. */
389 return -1;
390 else
391 return 0;
392}
393
394/* Wake up one thread waiting on this condition. */
395static inline int
396__gthread_objc_condition_signal(objc_condition_t condition)
397{
398 if (__gthread_active_p ())
399 /* Unimplemented. */
400 return -1;
401 else
402 return 0;
403}
404
405#else /* _LIBOBJC */
406
f24af81b 407static inline int
d1e51320 408__gthread_once (__gthread_once_t *once, void (*func) (void))
f24af81b
TT
409{
410 if (__gthread_active_p ())
411 return pthread_once (once, func);
412 else
413 return -1;
414}
415
416static inline int
417__gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
418{
419 return pthread_keycreate (key, dtor);
420}
421
422static inline int
5467baef 423__gthread_key_dtor (UNUSED (__gthread_key_t key), UNUSED (void *ptr))
f24af81b
TT
424{
425 /* Nothing needed. */
426 return 0;
427}
428
5467baef
JDA
429static inline int
430__gthread_key_delete (UNUSED (__gthread_key_t key))
431{
432 /* Operation is not supported. */
433 return -1;
434}
f24af81b
TT
435
436static inline void *
437__gthread_getspecific (__gthread_key_t key)
438{
439 void *ptr;
440 if (pthread_getspecific (key, &ptr) == 0)
441 return ptr;
442 else
443 return 0;
444}
445
446static inline int
447__gthread_setspecific (__gthread_key_t key, const void *ptr)
448{
449 return pthread_setspecific (key, (void *) ptr);
450}
451
5241c227
JDA
452static inline void
453__gthread_mutex_init_function (__gthread_mutex_t *mutex)
454{
455 if (__gthread_active_p ())
456 pthread_mutex_init (mutex, pthread_mutexattr_default);
457}
458
f24af81b
TT
459static inline int
460__gthread_mutex_lock (__gthread_mutex_t *mutex)
461{
462 if (__gthread_active_p ())
463 return pthread_mutex_lock (mutex);
464 else
465 return 0;
466}
467
468static inline int
469__gthread_mutex_trylock (__gthread_mutex_t *mutex)
470{
471 if (__gthread_active_p ())
472 return pthread_mutex_trylock (mutex);
473 else
474 return 0;
475}
476
477static inline int
478__gthread_mutex_unlock (__gthread_mutex_t *mutex)
479{
480 if (__gthread_active_p ())
481 return pthread_mutex_unlock (mutex);
482 else
483 return 0;
484}
485
15794a95
L
486#endif /* _LIBOBJC */
487
5467baef
JDA
488#undef UNUSED
489
88657302 490#endif /* ! GCC_GTHR_DCE_H */