* varasm.c (assemble_start_function): Remove reset of in_section.
[official-gcc.git] / gcc / gthr-dce.h
blob6a3d906c6460a7d297076df816f23f59b07a6bc3
1 /* Threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001, 2004 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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, 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_DCE_H
30 #define GCC_GTHR_DCE_H
32 /* If _DCE_THREADS is not defined, then we're building the single
33 threaded version of the libraries and do not want to reference
34 anything related to pthreads or dce. */
35 #ifndef _DCE_THREADS
36 #include "gthr-single.h"
37 #else
38 /* DCE threads interface.
39 DCE threads are based on POSIX threads draft 4, and many things
40 have changed since then. */
42 #define __GTHREADS 1
44 #include <pthread.h>
46 #ifdef __cplusplus
47 #define UNUSED(x) x
48 #else
49 #define UNUSED(x) x __attribute__((unused))
50 #endif
52 typedef pthread_key_t __gthread_key_t;
53 typedef pthread_once_t __gthread_once_t;
54 typedef pthread_mutex_t __gthread_mutex_t;
55 typedef pthread_mutex_t __gthread_recursive_mutex_t;
57 #define __GTHREAD_ONCE_INIT pthread_once_init
59 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
60 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
62 #define __GTHREAD_MUTEX_INIT_DEFAULT pthread_once_init
64 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
66 #pragma weak pthread_once
67 #pragma weak pthread_once_init
68 #pragma weak pthread_keycreate
69 #pragma weak pthread_key_delete
70 #pragma weak pthread_getspecific
71 #pragma weak pthread_setspecific
72 #pragma weak pthread_create
73 #pragma weak pthread_mutex_init
74 #pragma weak pthread_mutex_lock
75 #pragma weak pthread_mutex_trylock
76 #pragma weak pthread_mutex_unlock
77 #pragma weak pthread_mutexattr_create
78 #pragma weak pthread_mutexattr_setkind_np
79 #pragma weak pthread_mutexattr_delete
81 #ifdef _LIBOBJC
82 /* Objective-C. */
83 #pragma weak pthread_cond_broadcast
84 #pragma weak pthread_cond_destroy
85 #pragma weak pthread_cond_init
86 #pragma weak pthread_cond_signal
87 #pragma weak pthread_cond_wait
88 #pragma weak pthread_exit
89 #pragma weak pthread_getunique_np
90 #pragma weak pthread_mutex_destroy
91 #pragma weak pthread_self
92 #pragma weak pthread_yield
93 #endif
95 static inline int
96 __gthread_active_p (void)
98 static void *const __gthread_active_ptr = (void *) &pthread_create;
99 return __gthread_active_ptr != 0;
102 #else /* not SUPPORTS_WEAK */
104 static inline int
105 __gthread_active_p (void)
107 return 1;
110 #endif /* SUPPORTS_WEAK */
112 #ifdef _LIBOBJC
114 /* Key structure for maintaining thread specific storage */
115 static pthread_key_t _objc_thread_storage;
117 /* Thread local storage for a single thread */
118 static void *thread_local_storage = NULL;
120 /* Backend initialization functions */
122 /* Initialize the threads subsystem. */
123 static inline int
124 __gthread_objc_init_thread_system (void)
126 if (__gthread_active_p ())
127 /* Initialize the thread storage key. */
128 return pthread_keycreate (&_objc_thread_storage, NULL);
129 else
130 return -1;
133 /* Close the threads subsystem. */
134 static inline int
135 __gthread_objc_close_thread_system (void)
137 if (__gthread_active_p ())
138 return 0;
139 else
140 return -1;
143 /* Backend thread functions */
145 /* Create a new thread of execution. */
146 static inline objc_thread_t
147 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
149 objc_thread_t thread_id;
150 pthread_t new_thread_handle;
152 if (!__gthread_active_p ())
153 return NULL;
155 if (!(pthread_create (&new_thread_handle, pthread_attr_default,
156 (void *) func, arg)))
158 /* ??? May not work! (64bit) */
159 thread_id = *(objc_thread_t *) &new_thread_handle;
160 pthread_detach (&new_thread_handle); /* Fully detach thread. */
162 else
163 thread_id = NULL;
165 return thread_id;
168 /* Set the current thread's priority. */
169 static inline int
170 __gthread_objc_thread_set_priority (int priority)
172 int sys_priority = 0;
174 if (!__gthread_active_p ())
175 return -1;
177 switch (priority)
179 case OBJC_THREAD_INTERACTIVE_PRIORITY:
180 sys_priority = (PRI_FG_MIN_NP + PRI_FG_MAX_NP) / 2;
181 break;
182 default:
183 case OBJC_THREAD_BACKGROUND_PRIORITY:
184 sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
185 break;
186 case OBJC_THREAD_LOW_PRIORITY:
187 sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
188 break;
191 /* Change the priority. */
192 if (pthread_setprio (pthread_self (), sys_priority) >= 0)
193 return 0;
194 else
195 /* Failed */
196 return -1;
199 /* Return the current thread's priority. */
200 static inline int
201 __gthread_objc_thread_get_priority (void)
203 int sys_priority;
205 if (__gthread_active_p ())
207 if ((sys_priority = pthread_getprio (pthread_self ())) >= 0)
209 if (sys_priority >= PRI_FG_MIN_NP
210 && sys_priority <= PRI_FG_MAX_NP)
211 return OBJC_THREAD_INTERACTIVE_PRIORITY;
212 if (sys_priority >= PRI_BG_MIN_NP
213 && sys_priority <= PRI_BG_MAX_NP)
214 return OBJC_THREAD_BACKGROUND_PRIORITY;
215 return OBJC_THREAD_LOW_PRIORITY;
218 /* Failed */
219 return -1;
221 else
222 return OBJC_THREAD_INTERACTIVE_PRIORITY;
225 /* Yield our process time to another thread. */
226 static inline void
227 __gthread_objc_thread_yield (void)
229 if (__gthread_active_p ())
230 pthread_yield ();
233 /* Terminate the current thread. */
234 static inline int
235 __gthread_objc_thread_exit (void)
237 if (__gthread_active_p ())
238 /* exit the thread */
239 pthread_exit (&__objc_thread_exit_status);
241 /* Failed if we reached here */
242 return -1;
245 /* Returns an integer value which uniquely describes a thread. */
246 static inline objc_thread_t
247 __gthread_objc_thread_id (void)
249 if (__gthread_active_p ())
251 pthread_t self = pthread_self ();
253 return (objc_thread_t) pthread_getunique_np (&self);
255 else
256 return (objc_thread_t) 1;
259 /* Sets the thread's local storage pointer. */
260 static inline int
261 __gthread_objc_thread_set_data (void *value)
263 if (__gthread_active_p ())
264 return pthread_setspecific (_objc_thread_storage, value);
265 else
267 thread_local_storage = value;
268 return 0;
272 /* Returns the thread's local storage pointer. */
273 static inline void *
274 __gthread_objc_thread_get_data (void)
276 void *value = NULL;
278 if (__gthread_active_p ())
280 if (!(pthread_getspecific (_objc_thread_storage, &value)))
281 return value;
283 return NULL;
285 else
286 return thread_local_storage;
289 /* Backend mutex functions */
291 /* Allocate a mutex. */
292 static inline int
293 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
295 if (__gthread_active_p ())
297 mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
299 if (pthread_mutex_init ((pthread_mutex_t *) mutex->backend,
300 pthread_mutexattr_default))
302 objc_free (mutex->backend);
303 mutex->backend = NULL;
304 return -1;
308 return 0;
311 /* Deallocate a mutex. */
312 static inline int
313 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
315 if (__gthread_active_p ())
317 if (pthread_mutex_destroy ((pthread_mutex_t *) mutex->backend))
318 return -1;
320 objc_free (mutex->backend);
321 mutex->backend = NULL;
324 return 0;
327 /* Grab a lock on a mutex. */
328 static inline int
329 __gthread_objc_mutex_lock (objc_mutex_t mutex)
331 if (__gthread_active_p ())
332 return pthread_mutex_lock ((pthread_mutex_t *) mutex->backend);
333 else
334 return 0;
337 /* Try to grab a lock on a mutex. */
338 static inline int
339 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
341 if (__gthread_active_p ()
342 && pthread_mutex_trylock ((pthread_mutex_t *) mutex->backend) != 1)
343 return -1;
345 return 0;
348 /* Unlock the mutex */
349 static inline int
350 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
352 if (__gthread_active_p ())
353 return pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend);
354 else
355 return 0;
358 /* Backend condition mutex functions */
360 /* Allocate a condition. */
361 static inline int
362 __gthread_objc_condition_allocate (objc_condition_t condition)
364 if (__gthread_active_p ())
365 /* Unimplemented. */
366 return -1;
367 else
368 return 0;
371 /* Deallocate a condition. */
372 static inline int
373 __gthread_objc_condition_deallocate (objc_condition_t condition)
375 if (__gthread_active_p ())
376 /* Unimplemented. */
377 return -1;
378 else
379 return 0;
382 /* Wait on the condition */
383 static inline int
384 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
386 if (__gthread_active_p ())
387 /* Unimplemented. */
388 return -1;
389 else
390 return 0;
393 /* Wake up all threads waiting on this condition. */
394 static inline int
395 __gthread_objc_condition_broadcast (objc_condition_t condition)
397 if (__gthread_active_p ())
398 /* Unimplemented. */
399 return -1;
400 else
401 return 0;
404 /* Wake up one thread waiting on this condition. */
405 static inline int
406 __gthread_objc_condition_signal (objc_condition_t condition)
408 if (__gthread_active_p ())
409 /* Unimplemented. */
410 return -1;
411 else
412 return 0;
415 #else /* _LIBOBJC */
417 static inline int
418 __gthread_once (__gthread_once_t *once, void (*func) (void))
420 if (__gthread_active_p ())
421 return pthread_once (once, func);
422 else
423 return -1;
426 static inline int
427 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
429 return pthread_keycreate (key, dtor);
432 static inline int
433 __gthread_key_delete (UNUSED (__gthread_key_t key))
435 /* Operation is not supported. */
436 return -1;
439 static inline void *
440 __gthread_getspecific (__gthread_key_t key)
442 void *ptr;
443 if (pthread_getspecific (key, &ptr) == 0)
444 return ptr;
445 else
446 return 0;
449 static inline int
450 __gthread_setspecific (__gthread_key_t key, const void *ptr)
452 return pthread_setspecific (key, (void *) ptr);
455 static inline void
456 __gthread_mutex_init_function (__gthread_mutex_t *mutex)
458 if (__gthread_active_p ())
459 pthread_mutex_init (mutex, pthread_mutexattr_default);
462 static inline int
463 __gthread_mutex_lock (__gthread_mutex_t *mutex)
465 if (__gthread_active_p ())
466 return pthread_mutex_lock (mutex);
467 else
468 return 0;
471 static inline int
472 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
474 if (__gthread_active_p ())
475 return pthread_mutex_trylock (mutex);
476 else
477 return 0;
480 static inline int
481 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
483 if (__gthread_active_p ())
484 return pthread_mutex_unlock (mutex);
485 else
486 return 0;
489 static inline int
490 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
492 if (__gthread_active_p ())
494 pthread_mutexattr_t attr;
495 int r;
497 r = pthread_mutexattr_create (&attr);
498 if (!r)
499 r = pthread_mutexattr_setkind_np (&attr, MUTEX_RECURSIVE_NP);
500 if (!r)
501 r = pthread_mutex_init (mutex, attr);
502 if (!r)
503 r = pthread_mutexattr_delete (&attr);
504 return r;
508 static inline int
509 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
511 return __gthread_mutex_lock (mutex);
514 static inline int
515 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
517 return __gthread_mutex_trylock (mutex);
520 static inline int
521 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
523 return __gthread_mutex_unlock (mutex);
526 #endif /* _LIBOBJC */
528 #undef UNUSED
530 #endif
531 #endif /* ! GCC_GTHR_DCE_H */