PR c++/23171, c++/23172, c++/25417.
[official-gcc.git] / gcc / gthr-posix95.h
blobf4ee09fb0287ded86760928310fb7487773af3ac
1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
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. */
29 #ifndef GCC_GTHR_POSIX_H
30 #define GCC_GTHR_POSIX_H
32 /* POSIX threads specific definitions.
33 Easy, since the interface is just one-to-one mapping. */
35 #define __GTHREADS 1
37 /* Some implementations of <pthread.h> require this to be defined. */
38 #ifndef _REENTRANT
39 #define _REENTRANT 1
40 #endif
42 #include <pthread.h>
43 #include <unistd.h>
45 typedef pthread_key_t __gthread_key_t;
46 typedef pthread_once_t __gthread_once_t;
47 typedef pthread_mutex_t __gthread_mutex_t;
49 typedef struct {
50 long depth;
51 pthread_t owner;
52 pthread_mutex_t actual;
53 } __gthread_recursive_mutex_t;
55 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
56 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
57 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
59 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
60 # define __gthrw(name) \
61 static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)))
62 #else
63 # define __gthrw_asmname(cname) __gthrw_asmnamep (__USER_LABEL_PREFIX__, cname)
64 # define __gthrw_asmnamep(prefix, cname) __gthrw_string (prefix) cname
65 # define __gthrw_string(x) #x
66 # define __gthrw(name) \
67 extern __typeof(name) __gthrw_ ## name __asm (__gthrw_asmname (#name))
68 #endif
70 __gthrw(pthread_once);
71 __gthrw(pthread_key_create);
72 __gthrw(pthread_key_delete);
73 __gthrw(pthread_getspecific);
74 __gthrw(pthread_setspecific);
75 __gthrw(pthread_create);
76 __gthrw(pthread_cancel);
77 __gthrw(pthread_self);
79 __gthrw(pthread_mutex_lock);
80 __gthrw(pthread_mutex_trylock);
81 __gthrw(pthread_mutex_unlock);
82 __gthrw(pthread_mutexattr_init);
83 __gthrw(pthread_mutexattr_destroy);
85 __gthrw(pthread_mutex_init);
87 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
88 /* Objective-C. */
89 __gthrw(pthread_cond_broadcast);
90 __gthrw(pthread_cond_destroy);
91 __gthrw(pthread_cond_init);
92 __gthrw(pthread_cond_signal);
93 __gthrw(pthread_cond_wait);
94 __gthrw(pthread_exit);
95 __gthrw(pthread_mutex_destroy);
96 #ifdef _POSIX_PRIORITY_SCHEDULING
97 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
98 __gthrw(sched_get_priority_max);
99 __gthrw(sched_get_priority_min);
100 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
101 #endif /* _POSIX_PRIORITY_SCHEDULING */
102 __gthrw(sched_yield);
103 __gthrw(pthread_attr_destroy);
104 __gthrw(pthread_attr_init);
105 __gthrw(pthread_attr_setdetachstate);
106 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
107 __gthrw(pthread_getschedparam);
108 __gthrw(pthread_setschedparam);
109 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
110 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
112 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
114 static inline int
115 __gthread_active_p (void)
117 static void *const __gthread_active_ptr
118 = __extension__ (void *) &__gthrw_pthread_cancel;
119 return __gthread_active_ptr != 0;
122 #else /* not SUPPORTS_WEAK */
124 static inline int
125 __gthread_active_p (void)
127 return 1;
130 #endif /* SUPPORTS_WEAK */
132 #ifdef _LIBOBJC
134 /* This is the config.h file in libobjc/ */
135 #include <config.h>
137 #ifdef HAVE_SCHED_H
138 # include <sched.h>
139 #endif
141 /* Key structure for maintaining thread specific storage */
142 static pthread_key_t _objc_thread_storage;
143 static pthread_attr_t _objc_thread_attribs;
145 /* Thread local storage for a single thread */
146 static void *thread_local_storage = NULL;
148 /* Backend initialization functions */
150 /* Initialize the threads subsystem. */
151 static inline int
152 __gthread_objc_init_thread_system (void)
154 if (__gthread_active_p ())
156 /* Initialize the thread storage key. */
157 if (__gthrw_pthread_key_create (&_objc_thread_storage, NULL) == 0)
159 /* The normal default detach state for threads is
160 * PTHREAD_CREATE_JOINABLE which causes threads to not die
161 * when you think they should. */
162 if (__gthrw_pthread_attr_init (&_objc_thread_attribs) == 0
163 && __gthrw_pthread_attr_setdetachstate (&_objc_thread_attribs,
164 PTHREAD_CREATE_DETACHED) == 0)
165 return 0;
169 return -1;
172 /* Close the threads subsystem. */
173 static inline int
174 __gthread_objc_close_thread_system (void)
176 if (__gthread_active_p ()
177 && __gthrw_pthread_key_delete (_objc_thread_storage) == 0
178 && __gthrw_pthread_attr_destroy (&_objc_thread_attribs) == 0)
179 return 0;
181 return -1;
184 /* Backend thread functions */
186 /* Create a new thread of execution. */
187 static inline objc_thread_t
188 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
190 objc_thread_t thread_id;
191 pthread_t new_thread_handle;
193 if (!__gthread_active_p ())
194 return NULL;
196 if (!(__gthrw_pthread_create (&new_thread_handle, NULL, (void *) func, arg)))
197 thread_id = (objc_thread_t) new_thread_handle;
198 else
199 thread_id = NULL;
201 return thread_id;
204 /* Set the current thread's priority. */
205 static inline int
206 __gthread_objc_thread_set_priority (int priority)
208 if (!__gthread_active_p ())
209 return -1;
210 else
212 #ifdef _POSIX_PRIORITY_SCHEDULING
213 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
214 pthread_t thread_id = __gthrw_pthread_self ();
215 int policy;
216 struct sched_param params;
217 int priority_min, priority_max;
219 if (__gthrw_pthread_getschedparam (thread_id, &policy, &params) == 0)
221 if ((priority_max = __gthrw_sched_get_priority_max (policy)) == -1)
222 return -1;
224 if ((priority_min = __gthrw_sched_get_priority_min (policy)) == -1)
225 return -1;
227 if (priority > priority_max)
228 priority = priority_max;
229 else if (priority < priority_min)
230 priority = priority_min;
231 params.sched_priority = priority;
234 * The solaris 7 and several other man pages incorrectly state that
235 * this should be a pointer to policy but pthread.h is universally
236 * at odds with this.
238 if (__gthrw_pthread_setschedparam (thread_id, policy, &params) == 0)
239 return 0;
241 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
242 #endif /* _POSIX_PRIORITY_SCHEDULING */
243 return -1;
247 /* Return the current thread's priority. */
248 static inline int
249 __gthread_objc_thread_get_priority (void)
251 #ifdef _POSIX_PRIORITY_SCHEDULING
252 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
253 if (__gthread_active_p ())
255 int policy;
256 struct sched_param params;
258 if (__gthrw_pthread_getschedparam (__gthrw_pthread_self (), &policy, &params) == 0)
259 return params.sched_priority;
260 else
261 return -1;
263 else
264 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
265 #endif /* _POSIX_PRIORITY_SCHEDULING */
266 return OBJC_THREAD_INTERACTIVE_PRIORITY;
269 /* Yield our process time to another thread. */
270 static inline void
271 __gthread_objc_thread_yield (void)
273 if (__gthread_active_p ())
274 __gthrw_sched_yield ();
277 /* Terminate the current thread. */
278 static inline int
279 __gthread_objc_thread_exit (void)
281 if (__gthread_active_p ())
282 /* exit the thread */
283 __gthrw_pthread_exit (&__objc_thread_exit_status);
285 /* Failed if we reached here */
286 return -1;
289 /* Returns an integer value which uniquely describes a thread. */
290 static inline objc_thread_t
291 __gthread_objc_thread_id (void)
293 if (__gthread_active_p ())
294 return (objc_thread_t) __gthrw_pthread_self ();
295 else
296 return (objc_thread_t) 1;
299 /* Sets the thread's local storage pointer. */
300 static inline int
301 __gthread_objc_thread_set_data (void *value)
303 if (__gthread_active_p ())
304 return __gthrw_pthread_setspecific (_objc_thread_storage, value);
305 else
307 thread_local_storage = value;
308 return 0;
312 /* Returns the thread's local storage pointer. */
313 static inline void *
314 __gthread_objc_thread_get_data (void)
316 if (__gthread_active_p ())
317 return __gthrw_pthread_getspecific (_objc_thread_storage);
318 else
319 return thread_local_storage;
322 /* Backend mutex functions */
324 /* Allocate a mutex. */
325 static inline int
326 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
328 if (__gthread_active_p ())
330 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
332 if (__gthrw_pthread_mutex_init ((pthread_mutex_t *) mutex->backend, NULL))
334 objc_free (mutex->backend);
335 mutex->backend = NULL;
336 return -1;
340 return 0;
343 /* Deallocate a mutex. */
344 static inline int
345 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
347 if (__gthread_active_p ())
349 int count;
352 * Posix Threads specifically require that the thread be unlocked
353 * for __gthrw_pthread_mutex_destroy to work.
358 count = __gthrw_pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend);
359 if (count < 0)
360 return -1;
362 while (count);
364 if (__gthrw_pthread_mutex_destroy ((pthread_mutex_t *) mutex->backend))
365 return -1;
367 objc_free (mutex->backend);
368 mutex->backend = NULL;
370 return 0;
373 /* Grab a lock on a mutex. */
374 static inline int
375 __gthread_objc_mutex_lock (objc_mutex_t mutex)
377 if (__gthread_active_p ()
378 && __gthrw_pthread_mutex_lock ((pthread_mutex_t *) mutex->backend) != 0)
380 return -1;
383 return 0;
386 /* Try to grab a lock on a mutex. */
387 static inline int
388 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
390 if (__gthread_active_p ()
391 && __gthrw_pthread_mutex_trylock ((pthread_mutex_t *) mutex->backend) != 0)
393 return -1;
396 return 0;
399 /* Unlock the mutex */
400 static inline int
401 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
403 if (__gthread_active_p ()
404 && __gthrw_pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend) != 0)
406 return -1;
409 return 0;
412 /* Backend condition mutex functions */
414 /* Allocate a condition. */
415 static inline int
416 __gthread_objc_condition_allocate (objc_condition_t condition)
418 if (__gthread_active_p ())
420 condition->backend = objc_malloc (sizeof (pthread_cond_t));
422 if (__gthrw_pthread_cond_init ((pthread_cond_t *) condition->backend, NULL))
424 objc_free (condition->backend);
425 condition->backend = NULL;
426 return -1;
430 return 0;
433 /* Deallocate a condition. */
434 static inline int
435 __gthread_objc_condition_deallocate (objc_condition_t condition)
437 if (__gthread_active_p ())
439 if (__gthrw_pthread_cond_destroy ((pthread_cond_t *) condition->backend))
440 return -1;
442 objc_free (condition->backend);
443 condition->backend = NULL;
445 return 0;
448 /* Wait on the condition */
449 static inline int
450 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
452 if (__gthread_active_p ())
453 return __gthrw_pthread_cond_wait ((pthread_cond_t *) condition->backend,
454 (pthread_mutex_t *) mutex->backend);
455 else
456 return 0;
459 /* Wake up all threads waiting on this condition. */
460 static inline int
461 __gthread_objc_condition_broadcast (objc_condition_t condition)
463 if (__gthread_active_p ())
464 return __gthrw_pthread_cond_broadcast ((pthread_cond_t *) condition->backend);
465 else
466 return 0;
469 /* Wake up one thread waiting on this condition. */
470 static inline int
471 __gthread_objc_condition_signal (objc_condition_t condition)
473 if (__gthread_active_p ())
474 return __gthrw_pthread_cond_signal ((pthread_cond_t *) condition->backend);
475 else
476 return 0;
479 #else /* _LIBOBJC */
481 static inline int
482 __gthread_once (__gthread_once_t *once, void (*func) (void))
484 if (__gthread_active_p ())
485 return __gthrw_pthread_once (once, func);
486 else
487 return -1;
490 static inline int
491 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
493 return __gthrw_pthread_key_create (key, dtor);
496 static inline int
497 __gthread_key_delete (__gthread_key_t key)
499 return __gthrw_pthread_key_delete (key);
502 static inline void *
503 __gthread_getspecific (__gthread_key_t key)
505 return __gthrw_pthread_getspecific (key);
508 static inline int
509 __gthread_setspecific (__gthread_key_t key, const void *ptr)
511 return __gthrw_pthread_setspecific (key, ptr);
514 static inline int
515 __gthread_mutex_lock (__gthread_mutex_t *mutex)
517 if (__gthread_active_p ())
518 return __gthrw_pthread_mutex_lock (mutex);
519 else
520 return 0;
523 static inline int
524 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
526 if (__gthread_active_p ())
527 return __gthrw_pthread_mutex_trylock (mutex);
528 else
529 return 0;
532 static inline int
533 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
535 if (__gthread_active_p ())
536 return __gthrw_pthread_mutex_unlock (mutex);
537 else
538 return 0;
541 static inline int
542 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
544 mutex->depth = 0;
545 mutex->owner = (pthread_t) 0;
546 return __gthrw_pthread_mutex_init (&mutex->actual, NULL);
549 static inline int
550 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
552 if (__gthread_active_p ())
554 pthread_t me = __gthrw_pthread_self ();
556 if (mutex->owner != me)
558 __gthrw_pthread_mutex_lock (&mutex->actual);
559 mutex->owner = me;
562 mutex->depth++;
564 return 0;
567 static inline int
568 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
570 if (__gthread_active_p ())
572 pthread_t me = __gthrw_pthread_self ();
574 if (mutex->owner != me)
576 if (__gthrw_pthread_mutex_trylock (&mutex->actual))
577 return 1;
578 mutex->owner = me;
581 mutex->depth++;
583 return 0;
586 static inline int
587 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
589 if (__gthread_active_p ())
591 if (--mutex->depth == 0)
593 mutex->owner = (pthread_t) 0;
594 __gthrw_pthread_mutex_unlock (&mutex->actual);
597 return 0;
600 #endif /* _LIBOBJC */
602 #endif /* ! GCC_GTHR_POSIX_H */