* Makefile.in (rtlanal.o): Depend on $(TM_P_H).
[official-gcc.git] / gcc / gthr-dce.h
blob25eab0e5693a7a445a0ca04ee8ebe37d13c4e94b
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001 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 /* DCE threads interface.
33 DCE threads are based on POSIX threads draft 4, and many things
34 have changed since then. */
36 #define __GTHREADS 1
38 #include <pthread.h>
40 #ifdef __cplusplus
41 #define UNUSED(x) x
42 #else
43 #define UNUSED(x) x __attribute__((unused))
44 #endif
46 typedef pthread_key_t __gthread_key_t;
47 typedef pthread_once_t __gthread_once_t;
48 typedef pthread_mutex_t __gthread_mutex_t;
50 #define __GTHREAD_ONCE_INIT pthread_once_init
52 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
54 #define __GTHREAD_MUTEX_INIT_DEFAULT pthread_once_init
56 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
58 #pragma weak pthread_once
59 #pragma weak pthread_once_init
60 #pragma weak pthread_keycreate
61 #pragma weak pthread_key_delete
62 #pragma weak pthread_getspecific
63 #pragma weak pthread_setspecific
64 #pragma weak pthread_create
65 #pragma weak pthread_mutex_init
66 #pragma weak pthread_mutex_lock
67 #pragma weak pthread_mutex_trylock
68 #pragma weak pthread_mutex_unlock
70 #ifdef _LIBOBJC
71 /* Objective C. */
72 #pragma weak pthread_cond_broadcast
73 #pragma weak pthread_cond_destroy
74 #pragma weak pthread_cond_init
75 #pragma weak pthread_cond_signal
76 #pragma weak pthread_cond_wait
77 #pragma weak pthread_exit
78 #pragma weak pthread_getunique_np
79 #pragma weak pthread_mutex_destroy
80 #pragma weak pthread_self
81 #pragma weak pthread_yield
82 #endif
84 static void *__gthread_active_ptr = (void *) &pthread_create;
86 static inline int
87 __gthread_active_p (void)
89 return __gthread_active_ptr != 0;
92 #else /* not SUPPORTS_WEAK */
94 static inline int
95 __gthread_active_p (void)
97 return 1;
100 #endif /* SUPPORTS_WEAK */
102 #ifdef _LIBOBJC
104 /* Key structure for maintaining thread specific storage */
105 static pthread_key_t _objc_thread_storage;
107 /* Thread local storage for a single thread */
108 static void *thread_local_storage = NULL;
110 /* Backend initialization functions */
112 /* Initialize the threads subsystem. */
113 static inline int
114 __gthread_objc_init_thread_system(void)
116 if (__gthread_active_p ())
117 /* Initialize the thread storage key */
118 return pthread_keycreate (&_objc_thread_storage, NULL);
119 else
120 return -1;
123 /* Close the threads subsystem. */
124 static inline int
125 __gthread_objc_close_thread_system(void)
127 if (__gthread_active_p ())
128 return 0;
129 else
130 return -1;
133 /* Backend thread functions */
135 /* Create a new thread of execution. */
136 static inline objc_thread_t
137 __gthread_objc_thread_detach(void (*func)(void *), void *arg)
139 objc_thread_t thread_id;
140 pthread_t new_thread_handle;
142 if (!__gthread_active_p ())
143 return NULL;
145 if ( !(pthread_create(&new_thread_handle, pthread_attr_default,
146 (void *)func, arg)) )
148 /* ??? May not work! (64bit) */
149 thread_id = *(objc_thread_t *)&new_thread_handle;
150 pthread_detach(&new_thread_handle); /* Fully detach thread. */
152 else
153 thread_id = NULL;
155 return thread_id;
158 /* Set the current thread's priority. */
159 static inline int
160 __gthread_objc_thread_set_priority(int priority)
162 int sys_priority = 0;
164 if (!__gthread_active_p ())
165 return -1;
167 switch (priority)
169 case OBJC_THREAD_INTERACTIVE_PRIORITY:
170 sys_priority = (PRI_FG_MIN_NP + PRI_FG_MAX_NP) / 2;
171 break;
172 default:
173 case OBJC_THREAD_BACKGROUND_PRIORITY:
174 sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
175 break;
176 case OBJC_THREAD_LOW_PRIORITY:
177 sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
178 break;
181 /* Change the priority. */
182 if (pthread_setprio(pthread_self(), sys_priority) >= 0)
183 return 0;
184 else
185 /* Failed */
186 return -1;
189 /* Return the current thread's priority. */
190 static inline int
191 __gthread_objc_thread_get_priority(void)
193 int sys_priority;
195 if (__gthread_active_p ())
197 if ((sys_priority = pthread_getprio(pthread_self())) >= 0)
199 if (sys_priority >= PRI_FG_MIN_NP
200 && sys_priority <= PRI_FG_MAX_NP)
201 return OBJC_THREAD_INTERACTIVE_PRIORITY;
202 if (sys_priority >= PRI_BG_MIN_NP
203 && sys_priority <= PRI_BG_MAX_NP)
204 return OBJC_THREAD_BACKGROUND_PRIORITY;
205 return OBJC_THREAD_LOW_PRIORITY;
208 /* Failed */
209 return -1;
211 else
212 return OBJC_THREAD_INTERACTIVE_PRIORITY;
215 /* Yield our process time to another thread. */
216 static inline void
217 __gthread_objc_thread_yield(void)
219 if (__gthread_active_p ())
220 pthread_yield();
223 /* Terminate the current thread. */
224 static inline int
225 __gthread_objc_thread_exit(void)
227 if (__gthread_active_p ())
228 /* exit the thread */
229 pthread_exit(&__objc_thread_exit_status);
231 /* Failed if we reached here */
232 return -1;
235 /* Returns an integer value which uniquely describes a thread. */
236 static inline objc_thread_t
237 __gthread_objc_thread_id(void)
239 if (__gthread_active_p ())
241 pthread_t self = pthread_self();
243 return (objc_thread_t) pthread_getunique_np (&self);
245 else
246 return (objc_thread_t)1;
249 /* Sets the thread's local storage pointer. */
250 static inline int
251 __gthread_objc_thread_set_data(void *value)
253 if (__gthread_active_p ())
254 return pthread_setspecific(_objc_thread_storage, value);
255 else
257 thread_local_storage = value;
258 return 0;
262 /* Returns the thread's local storage pointer. */
263 static inline void *
264 __gthread_objc_thread_get_data(void)
266 void *value = NULL;
268 if (__gthread_active_p ())
270 if ( !(pthread_getspecific(_objc_thread_storage, &value)) )
271 return value;
273 return NULL;
275 else
276 return thread_local_storage;
279 /* Backend mutex functions */
281 /* Allocate a mutex. */
282 static inline int
283 __gthread_objc_mutex_allocate(objc_mutex_t mutex)
285 if (__gthread_active_p ())
287 mutex->backend = objc_malloc(sizeof(pthread_mutex_t));
289 if (pthread_mutex_init((pthread_mutex_t *)mutex->backend,
290 pthread_mutexattr_default))
292 objc_free(mutex->backend);
293 mutex->backend = NULL;
294 return -1;
298 return 0;
301 /* Deallocate a mutex. */
302 static inline int
303 __gthread_objc_mutex_deallocate(objc_mutex_t mutex)
305 if (__gthread_active_p ())
307 if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
308 return -1;
310 objc_free(mutex->backend);
311 mutex->backend = NULL;
314 return 0;
317 /* Grab a lock on a mutex. */
318 static inline int
319 __gthread_objc_mutex_lock(objc_mutex_t mutex)
321 if (__gthread_active_p ())
322 return pthread_mutex_lock((pthread_mutex_t *)mutex->backend);
323 else
324 return 0;
327 /* Try to grab a lock on a mutex. */
328 static inline int
329 __gthread_objc_mutex_trylock(objc_mutex_t mutex)
331 if (__gthread_active_p ()
332 && pthread_mutex_trylock((pthread_mutex_t *)mutex->backend) != 1)
333 return -1;
335 return 0;
338 /* Unlock the mutex */
339 static inline int
340 __gthread_objc_mutex_unlock(objc_mutex_t mutex)
342 if (__gthread_active_p ())
343 return pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
344 else
345 return 0;
348 /* Backend condition mutex functions */
350 /* Allocate a condition. */
351 static inline int
352 __gthread_objc_condition_allocate(objc_condition_t condition)
354 if (__gthread_active_p ())
355 /* Unimplemented. */
356 return -1;
357 else
358 return 0;
361 /* Deallocate a condition. */
362 static inline int
363 __gthread_objc_condition_deallocate(objc_condition_t condition)
365 if (__gthread_active_p ())
366 /* Unimplemented. */
367 return -1;
368 else
369 return 0;
372 /* Wait on the condition */
373 static inline int
374 __gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
376 if (__gthread_active_p ())
377 /* Unimplemented. */
378 return -1;
379 else
380 return 0;
383 /* Wake up all threads waiting on this condition. */
384 static inline int
385 __gthread_objc_condition_broadcast(objc_condition_t condition)
387 if (__gthread_active_p ())
388 /* Unimplemented. */
389 return -1;
390 else
391 return 0;
394 /* Wake up one thread waiting on this condition. */
395 static inline int
396 __gthread_objc_condition_signal(objc_condition_t condition)
398 if (__gthread_active_p ())
399 /* Unimplemented. */
400 return -1;
401 else
402 return 0;
405 #else /* _LIBOBJC */
407 static inline int
408 __gthread_once (__gthread_once_t *once, void (*func) (void))
410 if (__gthread_active_p ())
411 return pthread_once (once, func);
412 else
413 return -1;
416 static inline int
417 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
419 return pthread_keycreate (key, dtor);
422 static inline int
423 __gthread_key_dtor (UNUSED (__gthread_key_t key), UNUSED (void *ptr))
425 /* Nothing needed. */
426 return 0;
429 static inline int
430 __gthread_key_delete (UNUSED (__gthread_key_t key))
432 /* Operation is not supported. */
433 return -1;
436 static inline void *
437 __gthread_getspecific (__gthread_key_t key)
439 void *ptr;
440 if (pthread_getspecific (key, &ptr) == 0)
441 return ptr;
442 else
443 return 0;
446 static inline int
447 __gthread_setspecific (__gthread_key_t key, const void *ptr)
449 return pthread_setspecific (key, (void *) ptr);
452 static inline void
453 __gthread_mutex_init_function (__gthread_mutex_t *mutex)
455 if (__gthread_active_p ())
456 pthread_mutex_init (mutex, pthread_mutexattr_default);
459 static inline int
460 __gthread_mutex_lock (__gthread_mutex_t *mutex)
462 if (__gthread_active_p ())
463 return pthread_mutex_lock (mutex);
464 else
465 return 0;
468 static inline int
469 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
471 if (__gthread_active_p ())
472 return pthread_mutex_trylock (mutex);
473 else
474 return 0;
477 static inline int
478 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
480 if (__gthread_active_p ())
481 return pthread_mutex_unlock (mutex);
482 else
483 return 0;
486 #endif /* _LIBOBJC */
488 #undef UNUSED
490 #endif /* ! GCC_GTHR_DCE_H */