]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gthr-posix.h
Make-lang.in (gnatbind): Readd rule that has been lost in last patch.
[thirdparty/gcc.git] / gcc / gthr-posix.h
CommitLineData
15794a95 1/* Threads compatibility routines for libgcc2 and libobjc. */
f24af81b 2/* Compile this one with gcc. */
fee013ef 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_POSIX_H
30#define GCC_GTHR_POSIX_H
f24af81b
TT
31
32/* POSIX threads specific definitions.
71287280 33 Easy, since the interface is just one-to-one mapping. */
f24af81b
TT
34
35#define __GTHREADS 1
36
37#include <pthread.h>
38
39typedef pthread_key_t __gthread_key_t;
40typedef pthread_once_t __gthread_once_t;
41typedef pthread_mutex_t __gthread_mutex_t;
42
43#define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
44#define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
45
46#if SUPPORTS_WEAK && GTHREAD_USE_WEAK
47
15794a95
L
48#pragma weak pthread_once
49#pragma weak pthread_key_create
50#pragma weak pthread_key_delete
51#pragma weak pthread_getspecific
52#pragma weak pthread_setspecific
53#pragma weak pthread_create
36244024 54
15794a95
L
55#pragma weak pthread_mutex_lock
56#pragma weak pthread_mutex_trylock
57#pragma weak pthread_mutex_unlock
58
59#ifdef _LIBOBJC
71287280 60/* Objective C. */
15794a95
L
61#pragma weak pthread_cond_broadcast
62#pragma weak pthread_cond_destroy
63#pragma weak pthread_cond_init
64#pragma weak pthread_cond_signal
65#pragma weak pthread_cond_wait
66#pragma weak pthread_exit
67#pragma weak pthread_mutex_init
68#pragma weak pthread_mutex_destroy
69#pragma weak pthread_self
f4b20cd9
RO
70#pragma weak sched_get_priority_max
71#pragma weak sched_get_priority_min
15794a95 72#pragma weak sched_yield
6f9b26b0
LR
73#pragma weak pthread_attr_destroy
74#pragma weak pthread_attr_init
75#pragma weak pthread_attr_setdetachstate
76#pragma weak pthread_getschedparam
77#pragma weak pthread_setschedparam
15794a95 78#endif
f24af81b 79
f24af81b 80static inline int
d1e51320 81__gthread_active_p (void)
f24af81b 82{
7a8de19b 83 static void *const __gthread_active_ptr = (void *) &pthread_create;
f24af81b
TT
84 return __gthread_active_ptr != 0;
85}
86
87#else /* not SUPPORTS_WEAK */
88
89static inline int
d1e51320 90__gthread_active_p (void)
f24af81b
TT
91{
92 return 1;
93}
94
95#endif /* SUPPORTS_WEAK */
96
15794a95
L
97#ifdef _LIBOBJC
98
5f974826
OP
99/* This is the config.h file in libobjc/ */
100#include <config.h>
101
102#ifdef HAVE_SCHED_H
103# include <sched.h>
104#endif
105
15794a95
L
106/* Key structure for maintaining thread specific storage */
107static pthread_key_t _objc_thread_storage;
447c11a5 108static pthread_attr_t _objc_thread_attribs;
15794a95
L
109
110/* Thread local storage for a single thread */
111static void *thread_local_storage = NULL;
112
113/* Backend initialization functions */
114
71287280 115/* Initialize the threads subsystem. */
15794a95
L
116static inline int
117__gthread_objc_init_thread_system(void)
118{
119 if (__gthread_active_p ())
447c11a5
OP
120 {
121 /* Initialize the thread storage key */
122 if (pthread_key_create(&_objc_thread_storage, NULL) == 0)
123 {
124 /* The normal default detach state for threads is
125 * PTHREAD_CREATE_JOINABLE which causes threads to not die
126 * when you think they should. */
127 if (pthread_attr_init(&_objc_thread_attribs) == 0
128 && pthread_attr_setdetachstate(&_objc_thread_attribs,
129 PTHREAD_CREATE_DETACHED) == 0)
130 return 0;
131 }
132 }
abc0360c
NP
133
134 return -1;
15794a95
L
135}
136
71287280 137/* Close the threads subsystem. */
15794a95
L
138static inline int
139__gthread_objc_close_thread_system(void)
140{
447c11a5
OP
141 if (__gthread_active_p ()
142 && pthread_key_delete(_objc_thread_storage) == 0
143 && pthread_attr_destroy(&_objc_thread_attribs) == 0)
15794a95 144 return 0;
447c11a5
OP
145
146 return -1;
15794a95
L
147}
148
149/* Backend thread functions */
150
71287280 151/* Create a new thread of execution. */
15794a95
L
152static inline objc_thread_t
153__gthread_objc_thread_detach(void (*func)(void *), void *arg)
154{
155 objc_thread_t thread_id;
156 pthread_t new_thread_handle;
157
158 if (!__gthread_active_p ())
159 return NULL;
160
161 if ( !(pthread_create(&new_thread_handle, NULL, (void *)func, arg)) )
fee013ef 162 thread_id = (objc_thread_t) new_thread_handle;
15794a95
L
163 else
164 thread_id = NULL;
165
166 return thread_id;
167}
168
71287280 169/* Set the current thread's priority. */
15794a95
L
170static inline int
171__gthread_objc_thread_set_priority(int priority)
172{
447c11a5
OP
173 if (!__gthread_active_p())
174 return -1;
175 else {
176 pthread_t thread_id = pthread_self();
177 int policy;
178 struct sched_param params;
179 int priority_min, priority_max;
180
181 if (pthread_getschedparam(thread_id, &policy, &params) == 0)
182 {
183 if ((priority_max = sched_get_priority_max(policy)) != 0)
184 return -1;
185
186 if ((priority_min = sched_get_priority_min(policy)) != 0)
187 return -1;
188
189 if (priority > priority_max)
190 priority = priority_max;
191 else if (priority < priority_min)
192 priority = priority_min;
193 params.sched_priority = priority;
194
195 /*
196 * The solaris 7 and several other man pages incorrectly state that
197 * this should be a pointer to policy but pthread.h is universally
198 * at odds with this.
199 */
200 if (pthread_setschedparam(thread_id, policy, &params) == 0)
201 return 0;
202 }
203 return -1;
204 }
15794a95
L
205}
206
71287280 207/* Return the current thread's priority. */
15794a95
L
208static inline int
209__gthread_objc_thread_get_priority(void)
210{
211 if (__gthread_active_p ())
447c11a5
OP
212 {
213 int policy;
214 struct sched_param params;
215
216 if (pthread_getschedparam(pthread_self(), &policy, &params) == 0)
217 return params.sched_priority;
218 else
219 return -1;
220 }
15794a95
L
221 else
222 return OBJC_THREAD_INTERACTIVE_PRIORITY;
223}
224
71287280 225/* Yield our process time to another thread. */
15794a95
L
226static inline void
227__gthread_objc_thread_yield(void)
228{
229 if (__gthread_active_p ())
230 sched_yield();
231}
232
71287280 233/* Terminate the current thread. */
15794a95
L
234static inline int
235__gthread_objc_thread_exit(void)
236{
237 if (__gthread_active_p ())
238 /* exit the thread */
239 pthread_exit(&__objc_thread_exit_status);
240
241 /* Failed if we reached here */
242 return -1;
243}
244
71287280 245/* Returns an integer value which uniquely describes a thread. */
15794a95
L
246static inline objc_thread_t
247__gthread_objc_thread_id(void)
248{
249 if (__gthread_active_p ())
fee013ef 250 return (objc_thread_t) pthread_self();
15794a95 251 else
fee013ef 252 return (objc_thread_t) 1;
15794a95
L
253}
254
71287280 255/* Sets the thread's local storage pointer. */
15794a95
L
256static inline int
257__gthread_objc_thread_set_data(void *value)
258{
259 if (__gthread_active_p ())
260 return pthread_setspecific(_objc_thread_storage, value);
261 else
262 {
263 thread_local_storage = value;
264 return 0;
265 }
266}
267
71287280 268/* Returns the thread's local storage pointer. */
15794a95
L
269static inline void *
270__gthread_objc_thread_get_data(void)
271{
272 if (__gthread_active_p ())
273 return pthread_getspecific(_objc_thread_storage);
274 else
275 return thread_local_storage;
276}
277
278/* Backend mutex functions */
279
71287280 280/* Allocate a mutex. */
15794a95
L
281static inline int
282__gthread_objc_mutex_allocate(objc_mutex_t mutex)
283{
284 if (__gthread_active_p ())
285 {
286 mutex->backend = objc_malloc(sizeof(pthread_mutex_t));
287
288 if (pthread_mutex_init((pthread_mutex_t *)mutex->backend, NULL))
289 {
290 objc_free(mutex->backend);
291 mutex->backend = NULL;
292 return -1;
293 }
294 }
295
296 return 0;
297}
298
71287280 299/* Deallocate a mutex. */
15794a95
L
300static inline int
301__gthread_objc_mutex_deallocate(objc_mutex_t mutex)
302{
303 if (__gthread_active_p ())
304 {
305 int count;
306
307 /*
308 * Posix Threads specifically require that the thread be unlocked
309 * for pthread_mutex_destroy to work.
310 */
311
312 do
313 {
314 count = pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
315 if (count < 0)
316 return -1;
317 }
318 while (count);
319
320 if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
321 return -1;
322
323 objc_free(mutex->backend);
324 mutex->backend = NULL;
325 }
326 return 0;
327}
328
71287280 329/* Grab a lock on a mutex. */
15794a95
L
330static inline int
331__gthread_objc_mutex_lock(objc_mutex_t mutex)
332{
054af139
NP
333 if (__gthread_active_p ()
334 && pthread_mutex_lock((pthread_mutex_t *)mutex->backend) != 0)
335 {
336 return -1;
337 }
338
339 return 0;
15794a95
L
340}
341
71287280 342/* Try to grab a lock on a mutex. */
15794a95
L
343static inline int
344__gthread_objc_mutex_trylock(objc_mutex_t mutex)
345{
054af139
NP
346 if (__gthread_active_p ()
347 && pthread_mutex_trylock((pthread_mutex_t *)mutex->backend) != 0)
348 {
349 return -1;
350 }
351
352 return 0;
15794a95
L
353}
354
355/* Unlock the mutex */
356static inline int
357__gthread_objc_mutex_unlock(objc_mutex_t mutex)
358{
054af139
NP
359 if (__gthread_active_p ()
360 && pthread_mutex_unlock((pthread_mutex_t *)mutex->backend) != 0)
361 {
362 return -1;
363 }
364
365 return 0;
15794a95
L
366}
367
368/* Backend condition mutex functions */
369
71287280 370/* Allocate a condition. */
15794a95
L
371static inline int
372__gthread_objc_condition_allocate(objc_condition_t condition)
373{
374 if (__gthread_active_p ())
375 {
376 condition->backend = objc_malloc(sizeof(pthread_cond_t));
377
378 if (pthread_cond_init((pthread_cond_t *)condition->backend, NULL))
379 {
380 objc_free(condition->backend);
381 condition->backend = NULL;
382 return -1;
383 }
384 }
385
386 return 0;
387}
388
71287280 389/* Deallocate a condition. */
15794a95
L
390static inline int
391__gthread_objc_condition_deallocate(objc_condition_t condition)
392{
393 if (__gthread_active_p ())
394 {
395 if (pthread_cond_destroy((pthread_cond_t *)condition->backend))
396 return -1;
397
398 objc_free(condition->backend);
399 condition->backend = NULL;
400 }
401 return 0;
402}
403
404/* Wait on the condition */
405static inline int
406__gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
407{
408 if (__gthread_active_p ())
409 return pthread_cond_wait((pthread_cond_t *)condition->backend,
410 (pthread_mutex_t *)mutex->backend);
411 else
412 return 0;
413}
414
71287280 415/* Wake up all threads waiting on this condition. */
15794a95
L
416static inline int
417__gthread_objc_condition_broadcast(objc_condition_t condition)
418{
419 if (__gthread_active_p ())
420 return pthread_cond_broadcast((pthread_cond_t *)condition->backend);
421 else
422 return 0;
423}
424
71287280 425/* Wake up one thread waiting on this condition. */
15794a95
L
426static inline int
427__gthread_objc_condition_signal(objc_condition_t condition)
428{
429 if (__gthread_active_p ())
430 return pthread_cond_signal((pthread_cond_t *)condition->backend);
431 else
432 return 0;
433}
434
435#else /* _LIBOBJC */
436
f24af81b 437static inline int
d1e51320 438__gthread_once (__gthread_once_t *once, void (*func) (void))
f24af81b
TT
439{
440 if (__gthread_active_p ())
441 return pthread_once (once, func);
442 else
443 return -1;
444}
445
446static inline int
447__gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
448{
449 return pthread_key_create (key, dtor);
450}
451
452static inline int
453__gthread_key_dtor (__gthread_key_t key, void *ptr)
454{
71287280 455 /* Just reset the key value to zero. */
f24af81b
TT
456 if (ptr)
457 return pthread_setspecific (key, 0);
458 else
459 return 0;
460}
461
462static inline int
463__gthread_key_delete (__gthread_key_t key)
464{
465 return pthread_key_delete (key);
466}
467
468static inline void *
469__gthread_getspecific (__gthread_key_t key)
470{
471 return pthread_getspecific (key);
472}
473
474static inline int
475__gthread_setspecific (__gthread_key_t key, const void *ptr)
476{
477 return pthread_setspecific (key, ptr);
478}
479
480static inline int
481__gthread_mutex_lock (__gthread_mutex_t *mutex)
482{
483 if (__gthread_active_p ())
484 return pthread_mutex_lock (mutex);
485 else
486 return 0;
487}
488
489static inline int
490__gthread_mutex_trylock (__gthread_mutex_t *mutex)
491{
492 if (__gthread_active_p ())
493 return pthread_mutex_trylock (mutex);
494 else
495 return 0;
496}
497
498static inline int
499__gthread_mutex_unlock (__gthread_mutex_t *mutex)
500{
501 if (__gthread_active_p ())
502 return pthread_mutex_unlock (mutex);
503 else
504 return 0;
505}
506
15794a95
L
507#endif /* _LIBOBJC */
508
88657302 509#endif /* ! GCC_GTHR_POSIX_H */