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