Fix __builtin_expect on PowerPCs
[official-gcc.git] / gcc / gthr-dce.h
blob96660a334ef54cf0b475cc6f19ee2128c1518ccd
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 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
51 /* Howto define __GTHREAD_MUTEX_INIT? */
53 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
55 #pragma weak pthread_once
56 #pragma weak pthread_once_init
57 #pragma weak pthread_keycreate
58 #pragma weak pthread_key_delete
59 #pragma weak pthread_getspecific
60 #pragma weak pthread_setspecific
61 #pragma weak pthread_create
63 #pragma weak pthread_mutex_lock
64 #pragma weak pthread_mutex_trylock
65 #pragma weak pthread_mutex_unlock
67 #ifdef _LIBOBJC
68 /* Objective C. */
69 #pragma weak pthread_cond_broadcast
70 #pragma weak pthread_cond_destroy
71 #pragma weak pthread_cond_init
72 #pragma weak pthread_cond_signal
73 #pragma weak pthread_cond_wait
74 #pragma weak pthread_exit
75 #pragma weak pthread_getunique_np
76 #pragma weak pthread_mutex_init
77 #pragma weak pthread_mutex_destroy
78 #pragma weak pthread_self
79 #pragma weak pthread_yield
80 #endif
82 static void *__gthread_active_ptr = (void *) &pthread_create;
84 static inline int
85 __gthread_active_p (void)
87 return __gthread_active_ptr != 0;
90 #else /* not SUPPORTS_WEAK */
92 static inline int
93 __gthread_active_p (void)
95 return 1;
98 #endif /* SUPPORTS_WEAK */
100 #ifdef _LIBOBJC
102 /* Key structure for maintaining thread specific storage */
103 static pthread_key_t _objc_thread_storage;
105 /* Thread local storage for a single thread */
106 static void *thread_local_storage = NULL;
108 /* Backend initialization functions */
110 /* Initialize the threads subsystem. */
111 static inline int
112 __gthread_objc_init_thread_system(void)
114 if (__gthread_active_p ())
115 /* Initialize the thread storage key */
116 return pthread_keycreate (&_objc_thread_storage, NULL);
117 else
118 return -1;
121 /* Close the threads subsystem. */
122 static inline int
123 __gthread_objc_close_thread_system(void)
125 if (__gthread_active_p ())
126 return 0;
127 else
128 return -1;
131 /* Backend thread functions */
133 /* Create a new thread of execution. */
134 static inline objc_thread_t
135 __gthread_objc_thread_detach(void (*func)(void *), void *arg)
137 objc_thread_t thread_id;
138 pthread_t new_thread_handle;
140 if (!__gthread_active_p ())
141 return NULL;
143 if ( !(pthread_create(&new_thread_handle, pthread_attr_default,
144 (void *)func, arg)) )
146 /* ??? May not work! (64bit) */
147 thread_id = *(objc_thread_t *)&new_thread_handle;
148 pthread_detach(&new_thread_handle); /* Fully detach thread. */
150 else
151 thread_id = NULL;
153 return thread_id;
156 /* Set the current thread's priority. */
157 static inline int
158 __gthread_objc_thread_set_priority(int priority)
160 int sys_priority = 0;
162 if (!__gthread_active_p ())
163 return -1;
165 switch (priority)
167 case OBJC_THREAD_INTERACTIVE_PRIORITY:
168 sys_priority = (PRI_FG_MIN_NP + PRI_FG_MAX_NP) / 2;
169 break;
170 default:
171 case OBJC_THREAD_BACKGROUND_PRIORITY:
172 sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
173 break;
174 case OBJC_THREAD_LOW_PRIORITY:
175 sys_priority = (PRI_BG_MIN_NP + PRI_BG_MAX_NP) / 2;
176 break;
179 /* Change the priority. */
180 if (pthread_setprio(pthread_self(), sys_priority) >= 0)
181 return 0;
182 else
183 /* Failed */
184 return -1;
187 /* Return the current thread's priority. */
188 static inline int
189 __gthread_objc_thread_get_priority(void)
191 int sys_priority;
193 if (__gthread_active_p ())
195 if ((sys_priority = pthread_getprio(pthread_self())) >= 0)
197 if (sys_priority >= PRI_FG_MIN_NP
198 && sys_priority <= PRI_FG_MAX_NP)
199 return OBJC_THREAD_INTERACTIVE_PRIORITY;
200 if (sys_priority >= PRI_BG_MIN_NP
201 && sys_priority <= PRI_BG_MAX_NP)
202 return OBJC_THREAD_BACKGROUND_PRIORITY;
203 return OBJC_THREAD_LOW_PRIORITY;
206 /* Failed */
207 return -1;
209 else
210 return OBJC_THREAD_INTERACTIVE_PRIORITY;
213 /* Yield our process time to another thread. */
214 static inline void
215 __gthread_objc_thread_yield(void)
217 if (__gthread_active_p ())
218 pthread_yield();
221 /* Terminate the current thread. */
222 static inline int
223 __gthread_objc_thread_exit(void)
225 if (__gthread_active_p ())
226 /* exit the thread */
227 pthread_exit(&__objc_thread_exit_status);
229 /* Failed if we reached here */
230 return -1;
233 /* Returns an integer value which uniquely describes a thread. */
234 static inline objc_thread_t
235 __gthread_objc_thread_id(void)
237 if (__gthread_active_p ())
239 pthread_t self = pthread_self();
241 return (objc_thread_t) pthread_getunique_np (&self);
243 else
244 return (objc_thread_t)1;
247 /* Sets the thread's local storage pointer. */
248 static inline int
249 __gthread_objc_thread_set_data(void *value)
251 if (__gthread_active_p ())
252 return pthread_setspecific(_objc_thread_storage, value);
253 else
255 thread_local_storage = value;
256 return 0;
260 /* Returns the thread's local storage pointer. */
261 static inline void *
262 __gthread_objc_thread_get_data(void)
264 void *value = NULL;
266 if (__gthread_active_p ())
268 if ( !(pthread_getspecific(_objc_thread_storage, &value)) )
269 return value;
271 return NULL;
273 else
274 return thread_local_storage;
277 /* Backend mutex functions */
279 /* Allocate a mutex. */
280 static inline int
281 __gthread_objc_mutex_allocate(objc_mutex_t mutex)
283 if (__gthread_active_p ())
285 mutex->backend = objc_malloc(sizeof(pthread_mutex_t));
287 if (pthread_mutex_init((pthread_mutex_t *)mutex->backend,
288 pthread_mutexattr_default))
290 objc_free(mutex->backend);
291 mutex->backend = NULL;
292 return -1;
296 return 0;
299 /* Deallocate a mutex. */
300 static inline int
301 __gthread_objc_mutex_deallocate(objc_mutex_t mutex)
303 if (__gthread_active_p ())
305 if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
306 return -1;
308 objc_free(mutex->backend);
309 mutex->backend = NULL;
312 return 0;
315 /* Grab a lock on a mutex. */
316 static inline int
317 __gthread_objc_mutex_lock(objc_mutex_t mutex)
319 if (__gthread_active_p ())
320 return pthread_mutex_lock((pthread_mutex_t *)mutex->backend);
321 else
322 return 0;
325 /* Try to grab a lock on a mutex. */
326 static inline int
327 __gthread_objc_mutex_trylock(objc_mutex_t mutex)
329 if (__gthread_active_p ()
330 && pthread_mutex_trylock((pthread_mutex_t *)mutex->backend) != 1)
331 return -1;
333 return 0;
336 /* Unlock the mutex */
337 static inline int
338 __gthread_objc_mutex_unlock(objc_mutex_t mutex)
340 if (__gthread_active_p ())
341 return pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
342 else
343 return 0;
346 /* Backend condition mutex functions */
348 /* Allocate a condition. */
349 static inline int
350 __gthread_objc_condition_allocate(objc_condition_t condition)
352 if (__gthread_active_p ())
353 /* Unimplemented. */
354 return -1;
355 else
356 return 0;
359 /* Deallocate a condition. */
360 static inline int
361 __gthread_objc_condition_deallocate(objc_condition_t condition)
363 if (__gthread_active_p ())
364 /* Unimplemented. */
365 return -1;
366 else
367 return 0;
370 /* Wait on the condition */
371 static inline int
372 __gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
374 if (__gthread_active_p ())
375 /* Unimplemented. */
376 return -1;
377 else
378 return 0;
381 /* Wake up all threads waiting on this condition. */
382 static inline int
383 __gthread_objc_condition_broadcast(objc_condition_t condition)
385 if (__gthread_active_p ())
386 /* Unimplemented. */
387 return -1;
388 else
389 return 0;
392 /* Wake up one thread waiting on this condition. */
393 static inline int
394 __gthread_objc_condition_signal(objc_condition_t condition)
396 if (__gthread_active_p ())
397 /* Unimplemented. */
398 return -1;
399 else
400 return 0;
403 #else /* _LIBOBJC */
405 static inline int
406 __gthread_once (__gthread_once_t *once, void (*func) (void))
408 if (__gthread_active_p ())
409 return pthread_once (once, func);
410 else
411 return -1;
414 static inline int
415 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
417 return pthread_keycreate (key, dtor);
420 static inline int
421 __gthread_key_dtor (UNUSED (__gthread_key_t key), UNUSED (void *ptr))
423 /* Nothing needed. */
424 return 0;
427 #if defined (__PTHREAD_LIBRARY_VERSION_1) && __PTHREAD_LIBRARY_VERSION_1 >= 1
428 static inline int
429 __gthread_key_delete (__gthread_key_t key)
431 return pthread_key_delete (key);
433 #else
434 static inline int
435 __gthread_key_delete (UNUSED (__gthread_key_t key))
437 /* Operation is not supported. */
438 return -1;
440 #endif
442 static inline void *
443 __gthread_getspecific (__gthread_key_t key)
445 void *ptr;
446 if (pthread_getspecific (key, &ptr) == 0)
447 return ptr;
448 else
449 return 0;
452 static inline int
453 __gthread_setspecific (__gthread_key_t key, const void *ptr)
455 return pthread_setspecific (key, (void *) ptr);
458 static inline int
459 __gthread_mutex_lock (__gthread_mutex_t *mutex)
461 if (__gthread_active_p ())
462 return pthread_mutex_lock (mutex);
463 else
464 return 0;
467 static inline int
468 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
470 if (__gthread_active_p ())
471 return pthread_mutex_trylock (mutex);
472 else
473 return 0;
476 static inline int
477 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
479 if (__gthread_active_p ())
480 return pthread_mutex_unlock (mutex);
481 else
482 return 0;
485 #endif /* _LIBOBJC */
487 #undef UNUSED
489 #endif /* ! GCC_GTHR_DCE_H */