PR c++/26266
[official-gcc.git] / gcc / gthr-dce.h
blobd876c659e06df27089e86862dfde425eb9f690fc
1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
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. */
30 #ifndef GCC_GTHR_DCE_H
31 #define GCC_GTHR_DCE_H
33 /* If _DCE_THREADS is not defined, then we're building the single
34 threaded version of the libraries and do not want to reference
35 anything related to pthreads or dce. */
36 #ifndef _DCE_THREADS
37 #include "gthr-single.h"
38 #else
39 /* DCE threads interface.
40 DCE threads are based on POSIX threads draft 4, and many things
41 have changed since then. */
43 #define __GTHREADS 1
45 #include <pthread.h>
47 typedef pthread_key_t __gthread_key_t;
48 typedef pthread_once_t __gthread_once_t;
49 typedef pthread_mutex_t __gthread_mutex_t;
50 typedef pthread_mutex_t __gthread_recursive_mutex_t;
52 #define __GTHREAD_ONCE_INIT pthread_once_init
54 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
55 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
57 #define __GTHREAD_MUTEX_INIT_DEFAULT pthread_once_init
59 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
60 # define __gthrw(name) \
61 static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)));
62 # define __gthrw_(name) __gthrw_ ## name
63 #else
64 # define __gthrw(name)
65 # define __gthrw_(name) name
66 #endif
68 __gthrw(pthread_once)
69 __gthrw(pthread_keycreate)
70 __gthrw(pthread_getspecific)
71 __gthrw(pthread_setspecific)
72 __gthrw(pthread_create)
73 __gthrw(pthread_mutex_init)
74 __gthrw(pthread_mutex_lock)
75 __gthrw(pthread_mutex_trylock)
76 __gthrw(pthread_mutex_unlock)
77 __gthrw(pthread_mutexattr_create)
78 __gthrw(pthread_mutexattr_setkind_np)
79 __gthrw(pthread_mutexattr_delete)
81 #ifdef _LIBOBJC
82 /* Objective-C. */
83 __gthrw(pthread_cond_broadcast)
84 __gthrw(pthread_cond_destroy)
85 __gthrw(pthread_cond_init)
86 __gthrw(pthread_cond_signal)
87 __gthrw(pthread_cond_wait)
88 __gthrw(pthread_exit)
90 #ifdef pthread_getunique_np
91 # define __gthrw_pthread_getunique_np pthread_getunique_np
92 #else
93 __gthrw(pthread_getunique_np)
94 # define __gthrw_pthread_getunique_np __gthrw_(pthread_getunique_np)
95 #endif
97 __gthrw(pthread_mutex_destroy)
98 __gthrw(pthread_self)
99 __gthrw(pthread_yield)
100 #endif
102 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
104 static inline int
105 __gthread_active_p (void)
107 static void *const __gthread_active_ptr = (void *) &__gthrw_(pthread_create);
108 return __gthread_active_ptr != 0;
111 #else /* not SUPPORTS_WEAK */
113 static inline int
114 __gthread_active_p (void)
116 return 1;
119 #endif /* SUPPORTS_WEAK */
121 #ifdef _LIBOBJC
123 /* Key structure for maintaining thread specific storage */
124 static pthread_key_t _objc_thread_storage;
126 /* Thread local storage for a single thread */
127 static void *thread_local_storage = NULL;
129 /* Backend initialization functions */
131 /* Initialize the threads subsystem. */
132 static inline int
133 __gthread_objc_init_thread_system (void)
135 if (__gthread_active_p ())
136 /* Initialize the thread storage key. */
137 return __gthrw_(pthread_keycreate) (&_objc_thread_storage, NULL);
138 else
139 return -1;
142 /* Close the threads subsystem. */
143 static inline int
144 __gthread_objc_close_thread_system (void)
146 if (__gthread_active_p ())
147 return 0;
148 else
149 return -1;
152 /* Backend thread functions */
154 /* Create a new thread of execution. */
155 static inline objc_thread_t
156 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
158 objc_thread_t thread_id;
159 pthread_t new_thread_handle;
161 if (!__gthread_active_p ())
162 return NULL;
164 if (!(__gthrw_(pthread_create) (&new_thread_handle, pthread_attr_default,
165 (void *) func, arg)))
167 /* ??? May not work! (64bit) */
168 thread_id = *(objc_thread_t *) &new_thread_handle;
169 pthread_detach (&new_thread_handle); /* Fully detach thread. */
171 else
172 thread_id = NULL;
174 return thread_id;
177 /* Set the current thread's priority. */
178 static inline int
179 __gthread_objc_thread_set_priority (int priority)
181 int sys_priority = 0;
183 if (!__gthread_active_p ())
184 return -1;
186 switch (priority)
188 case OBJC_THREAD_INTERACTIVE_PRIORITY:
189 sys_priority = (PRI_FG_MIN_NP + PRI_FG_MAX_NP) / 2;
190 break;
191 default:
192 case OBJC_THREAD_BACKGROUND_PRIORITY:
193 sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
194 break;
195 case OBJC_THREAD_LOW_PRIORITY:
196 sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
197 break;
200 /* Change the priority. */
201 if (pthread_setprio (__gthrw_(pthread_self) (), sys_priority) >= 0)
202 return 0;
203 else
204 /* Failed */
205 return -1;
208 /* Return the current thread's priority. */
209 static inline int
210 __gthread_objc_thread_get_priority (void)
212 int sys_priority;
214 if (__gthread_active_p ())
216 if ((sys_priority = pthread_getprio (__gthrw_(pthread_self) ())) >= 0)
218 if (sys_priority >= PRI_FG_MIN_NP
219 && sys_priority <= PRI_FG_MAX_NP)
220 return OBJC_THREAD_INTERACTIVE_PRIORITY;
221 if (sys_priority >= PRI_BG_MIN_NP
222 && sys_priority <= PRI_BG_MAX_NP)
223 return OBJC_THREAD_BACKGROUND_PRIORITY;
224 return OBJC_THREAD_LOW_PRIORITY;
227 /* Failed */
228 return -1;
230 else
231 return OBJC_THREAD_INTERACTIVE_PRIORITY;
234 /* Yield our process time to another thread. */
235 static inline void
236 __gthread_objc_thread_yield (void)
238 if (__gthread_active_p ())
239 __gthrw_(pthread_yield) ();
242 /* Terminate the current thread. */
243 static inline int
244 __gthread_objc_thread_exit (void)
246 if (__gthread_active_p ())
247 /* exit the thread */
248 __gthrw_(pthread_exit) (&__objc_thread_exit_status);
250 /* Failed if we reached here */
251 return -1;
254 /* Returns an integer value which uniquely describes a thread. */
255 static inline objc_thread_t
256 __gthread_objc_thread_id (void)
258 if (__gthread_active_p ())
260 pthread_t self = __gthrw_(pthread_self) ();
262 return (objc_thread_t) __gthrw_pthread_getunique_np (&self);
264 else
265 return (objc_thread_t) 1;
268 /* Sets the thread's local storage pointer. */
269 static inline int
270 __gthread_objc_thread_set_data (void *value)
272 if (__gthread_active_p ())
273 return __gthrw_(pthread_setspecific) (_objc_thread_storage, value);
274 else
276 thread_local_storage = value;
277 return 0;
281 /* Returns the thread's local storage pointer. */
282 static inline void *
283 __gthread_objc_thread_get_data (void)
285 void *value = NULL;
287 if (__gthread_active_p ())
289 if (!(__gthrw_(pthread_getspecific) (_objc_thread_storage, &value)))
290 return value;
292 return NULL;
294 else
295 return thread_local_storage;
298 /* Backend mutex functions */
300 /* Allocate a mutex. */
301 static inline int
302 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
304 if (__gthread_active_p ())
306 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
308 if (__gthrw_(pthread_mutex_init) ((pthread_mutex_t *) mutex->backend,
309 pthread_mutexattr_default))
311 objc_free (mutex->backend);
312 mutex->backend = NULL;
313 return -1;
317 return 0;
320 /* Deallocate a mutex. */
321 static inline int
322 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
324 if (__gthread_active_p ())
326 if (__gthrw_(pthread_mutex_destroy) ((pthread_mutex_t *) mutex->backend))
327 return -1;
329 objc_free (mutex->backend);
330 mutex->backend = NULL;
333 return 0;
336 /* Grab a lock on a mutex. */
337 static inline int
338 __gthread_objc_mutex_lock (objc_mutex_t mutex)
340 if (__gthread_active_p ())
341 return __gthrw_(pthread_mutex_lock) ((pthread_mutex_t *) mutex->backend);
342 else
343 return 0;
346 /* Try to grab a lock on a mutex. */
347 static inline int
348 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
350 if (__gthread_active_p ()
351 && __gthrw_(pthread_mutex_trylock) ((pthread_mutex_t *) mutex->backend) != 1)
352 return -1;
354 return 0;
357 /* Unlock the mutex */
358 static inline int
359 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
361 if (__gthread_active_p ())
362 return __gthrw_(pthread_mutex_unlock) ((pthread_mutex_t *) mutex->backend);
363 else
364 return 0;
367 /* Backend condition mutex functions */
369 /* Allocate a condition. */
370 static inline int
371 __gthread_objc_condition_allocate (objc_condition_t condition
372 __attribute__ ((__unused__)))
374 if (__gthread_active_p ())
375 /* Unimplemented. */
376 return -1;
377 else
378 return 0;
381 /* Deallocate a condition. */
382 static inline int
383 __gthread_objc_condition_deallocate (objc_condition_t condition
384 __attribute__ ((__unused__)))
386 if (__gthread_active_p ())
387 /* Unimplemented. */
388 return -1;
389 else
390 return 0;
393 /* Wait on the condition */
394 static inline int
395 __gthread_objc_condition_wait (objc_condition_t condition
396 __attribute__ ((__unused__)),
397 objc_mutex_t mutex __attribute__ ((__unused__)))
399 if (__gthread_active_p ())
400 /* Unimplemented. */
401 return -1;
402 else
403 return 0;
406 /* Wake up all threads waiting on this condition. */
407 static inline int
408 __gthread_objc_condition_broadcast (objc_condition_t condition
409 __attribute__ ((__unused__)))
411 if (__gthread_active_p ())
412 /* Unimplemented. */
413 return -1;
414 else
415 return 0;
418 /* Wake up one thread waiting on this condition. */
419 static inline int
420 __gthread_objc_condition_signal (objc_condition_t condition
421 __attribute__ ((__unused__)))
423 if (__gthread_active_p ())
424 /* Unimplemented. */
425 return -1;
426 else
427 return 0;
430 #else /* _LIBOBJC */
432 static inline int
433 __gthread_once (__gthread_once_t *once, void (*func) (void))
435 if (__gthread_active_p ())
436 return __gthrw_(pthread_once) (once, func);
437 else
438 return -1;
441 static inline int
442 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
444 return __gthrw_(pthread_keycreate) (key, dtor);
447 static inline int
448 __gthread_key_delete (__gthread_key_t key __attribute__ ((__unused__)))
450 /* Operation is not supported. */
451 return -1;
454 static inline void *
455 __gthread_getspecific (__gthread_key_t key)
457 void *ptr;
458 if (__gthrw_(pthread_getspecific) (key, &ptr) == 0)
459 return ptr;
460 else
461 return 0;
464 static inline int
465 __gthread_setspecific (__gthread_key_t key, const void *ptr)
467 return __gthrw_(pthread_setspecific) (key, (void *) ptr);
470 static inline void
471 __gthread_mutex_init_function (__gthread_mutex_t *mutex)
473 if (__gthread_active_p ())
474 __gthrw_(pthread_mutex_init) (mutex, pthread_mutexattr_default);
477 static inline int
478 __gthread_mutex_lock (__gthread_mutex_t *mutex)
480 if (__gthread_active_p ())
481 return __gthrw_(pthread_mutex_lock) (mutex);
482 else
483 return 0;
486 static inline int
487 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
489 if (__gthread_active_p ())
490 return __gthrw_(pthread_mutex_trylock) (mutex);
491 else
492 return 0;
495 static inline int
496 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
498 if (__gthread_active_p ())
499 return __gthrw_(pthread_mutex_unlock) (mutex);
500 else
501 return 0;
504 static inline int
505 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
507 if (__gthread_active_p ())
509 pthread_mutexattr_t attr;
510 int r;
512 r = __gthrw_(pthread_mutexattr_create) (&attr);
513 if (!r)
514 r = __gthrw_(pthread_mutexattr_setkind_np) (&attr, MUTEX_RECURSIVE_NP);
515 if (!r)
516 r = __gthrw_(pthread_mutex_init) (mutex, attr);
517 if (!r)
518 r = __gthrw_(pthread_mutexattr_delete) (&attr);
519 return r;
521 return 0;
524 static inline int
525 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
527 return __gthread_mutex_lock (mutex);
530 static inline int
531 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
533 return __gthread_mutex_trylock (mutex);
536 static inline int
537 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
539 return __gthread_mutex_unlock (mutex);
542 #endif /* _LIBOBJC */
544 #endif
545 #endif /* ! GCC_GTHR_DCE_H */