]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gthr-dce.h
Makefile.in, [...]: replace "GNU CC" with "GCC".
[thirdparty/gcc.git] / gcc / gthr-dce.h
1
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1997, 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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. */
21
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. */
28
29 #ifndef GCC_GTHR_DCE_H
30 #define GCC_GTHR_DCE_H
31
32 /* DCE threads interface.
33 DCE threads are based on POSIX threads draft 4, and many things
34 have changed since then. */
35
36 #define __GTHREADS 1
37
38 #include <pthread.h>
39
40 #ifdef __cplusplus
41 #define UNUSED(x) x
42 #else
43 #define UNUSED(x) x __attribute__((unused))
44 #endif
45
46 typedef pthread_key_t __gthread_key_t;
47 typedef pthread_once_t __gthread_once_t;
48 typedef pthread_mutex_t __gthread_mutex_t;
49
50 #define __GTHREAD_ONCE_INIT pthread_once_init
51
52 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
53
54 #define __GTHREAD_MUTEX_INIT_DEFAULT pthread_once_init
55
56 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
57
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
69
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
83
84 static void *__gthread_active_ptr = (void *) &pthread_create;
85
86 static inline int
87 __gthread_active_p (void)
88 {
89 return __gthread_active_ptr != 0;
90 }
91
92 #else /* not SUPPORTS_WEAK */
93
94 static inline int
95 __gthread_active_p (void)
96 {
97 return 1;
98 }
99
100 #endif /* SUPPORTS_WEAK */
101
102 #ifdef _LIBOBJC
103
104 /* Key structure for maintaining thread specific storage */
105 static pthread_key_t _objc_thread_storage;
106
107 /* Thread local storage for a single thread */
108 static void *thread_local_storage = NULL;
109
110 /* Backend initialization functions */
111
112 /* Initialize the threads subsystem. */
113 static inline int
114 __gthread_objc_init_thread_system(void)
115 {
116 if (__gthread_active_p ())
117 /* Initialize the thread storage key */
118 return pthread_keycreate (&_objc_thread_storage, NULL);
119 else
120 return -1;
121 }
122
123 /* Close the threads subsystem. */
124 static inline int
125 __gthread_objc_close_thread_system(void)
126 {
127 if (__gthread_active_p ())
128 return 0;
129 else
130 return -1;
131 }
132
133 /* Backend thread functions */
134
135 /* Create a new thread of execution. */
136 static inline objc_thread_t
137 __gthread_objc_thread_detach(void (*func)(void *), void *arg)
138 {
139 objc_thread_t thread_id;
140 pthread_t new_thread_handle;
141
142 if (!__gthread_active_p ())
143 return NULL;
144
145 if ( !(pthread_create(&new_thread_handle, pthread_attr_default,
146 (void *)func, arg)) )
147 {
148 /* ??? May not work! (64bit) */
149 thread_id = *(objc_thread_t *)&new_thread_handle;
150 pthread_detach(&new_thread_handle); /* Fully detach thread. */
151 }
152 else
153 thread_id = NULL;
154
155 return thread_id;
156 }
157
158 /* Set the current thread's priority. */
159 static inline int
160 __gthread_objc_thread_set_priority(int priority)
161 {
162 int sys_priority = 0;
163
164 if (!__gthread_active_p ())
165 return -1;
166
167 switch (priority)
168 {
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;
179 }
180
181 /* Change the priority. */
182 if (pthread_setprio(pthread_self(), sys_priority) >= 0)
183 return 0;
184 else
185 /* Failed */
186 return -1;
187 }
188
189 /* Return the current thread's priority. */
190 static inline int
191 __gthread_objc_thread_get_priority(void)
192 {
193 int sys_priority;
194
195 if (__gthread_active_p ())
196 {
197 if ((sys_priority = pthread_getprio(pthread_self())) >= 0)
198 {
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;
206 }
207
208 /* Failed */
209 return -1;
210 }
211 else
212 return OBJC_THREAD_INTERACTIVE_PRIORITY;
213 }
214
215 /* Yield our process time to another thread. */
216 static inline void
217 __gthread_objc_thread_yield(void)
218 {
219 if (__gthread_active_p ())
220 pthread_yield();
221 }
222
223 /* Terminate the current thread. */
224 static inline int
225 __gthread_objc_thread_exit(void)
226 {
227 if (__gthread_active_p ())
228 /* exit the thread */
229 pthread_exit(&__objc_thread_exit_status);
230
231 /* Failed if we reached here */
232 return -1;
233 }
234
235 /* Returns an integer value which uniquely describes a thread. */
236 static inline objc_thread_t
237 __gthread_objc_thread_id(void)
238 {
239 if (__gthread_active_p ())
240 {
241 pthread_t self = pthread_self();
242
243 return (objc_thread_t) pthread_getunique_np (&self);
244 }
245 else
246 return (objc_thread_t)1;
247 }
248
249 /* Sets the thread's local storage pointer. */
250 static inline int
251 __gthread_objc_thread_set_data(void *value)
252 {
253 if (__gthread_active_p ())
254 return pthread_setspecific(_objc_thread_storage, value);
255 else
256 {
257 thread_local_storage = value;
258 return 0;
259 }
260 }
261
262 /* Returns the thread's local storage pointer. */
263 static inline void *
264 __gthread_objc_thread_get_data(void)
265 {
266 void *value = NULL;
267
268 if (__gthread_active_p ())
269 {
270 if ( !(pthread_getspecific(_objc_thread_storage, &value)) )
271 return value;
272
273 return NULL;
274 }
275 else
276 return thread_local_storage;
277 }
278
279 /* Backend mutex functions */
280
281 /* Allocate a mutex. */
282 static inline int
283 __gthread_objc_mutex_allocate(objc_mutex_t mutex)
284 {
285 if (__gthread_active_p ())
286 {
287 mutex->backend = objc_malloc(sizeof(pthread_mutex_t));
288
289 if (pthread_mutex_init((pthread_mutex_t *)mutex->backend,
290 pthread_mutexattr_default))
291 {
292 objc_free(mutex->backend);
293 mutex->backend = NULL;
294 return -1;
295 }
296 }
297
298 return 0;
299 }
300
301 /* Deallocate a mutex. */
302 static inline int
303 __gthread_objc_mutex_deallocate(objc_mutex_t mutex)
304 {
305 if (__gthread_active_p ())
306 {
307 if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
308 return -1;
309
310 objc_free(mutex->backend);
311 mutex->backend = NULL;
312 }
313
314 return 0;
315 }
316
317 /* Grab a lock on a mutex. */
318 static inline int
319 __gthread_objc_mutex_lock(objc_mutex_t mutex)
320 {
321 if (__gthread_active_p ())
322 return pthread_mutex_lock((pthread_mutex_t *)mutex->backend);
323 else
324 return 0;
325 }
326
327 /* Try to grab a lock on a mutex. */
328 static inline int
329 __gthread_objc_mutex_trylock(objc_mutex_t mutex)
330 {
331 if (__gthread_active_p ()
332 && pthread_mutex_trylock((pthread_mutex_t *)mutex->backend) != 1)
333 return -1;
334
335 return 0;
336 }
337
338 /* Unlock the mutex */
339 static inline int
340 __gthread_objc_mutex_unlock(objc_mutex_t mutex)
341 {
342 if (__gthread_active_p ())
343 return pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
344 else
345 return 0;
346 }
347
348 /* Backend condition mutex functions */
349
350 /* Allocate a condition. */
351 static inline int
352 __gthread_objc_condition_allocate(objc_condition_t condition)
353 {
354 if (__gthread_active_p ())
355 /* Unimplemented. */
356 return -1;
357 else
358 return 0;
359 }
360
361 /* Deallocate a condition. */
362 static inline int
363 __gthread_objc_condition_deallocate(objc_condition_t condition)
364 {
365 if (__gthread_active_p ())
366 /* Unimplemented. */
367 return -1;
368 else
369 return 0;
370 }
371
372 /* Wait on the condition */
373 static inline int
374 __gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
375 {
376 if (__gthread_active_p ())
377 /* Unimplemented. */
378 return -1;
379 else
380 return 0;
381 }
382
383 /* Wake up all threads waiting on this condition. */
384 static inline int
385 __gthread_objc_condition_broadcast(objc_condition_t condition)
386 {
387 if (__gthread_active_p ())
388 /* Unimplemented. */
389 return -1;
390 else
391 return 0;
392 }
393
394 /* Wake up one thread waiting on this condition. */
395 static inline int
396 __gthread_objc_condition_signal(objc_condition_t condition)
397 {
398 if (__gthread_active_p ())
399 /* Unimplemented. */
400 return -1;
401 else
402 return 0;
403 }
404
405 #else /* _LIBOBJC */
406
407 static inline int
408 __gthread_once (__gthread_once_t *once, void (*func) (void))
409 {
410 if (__gthread_active_p ())
411 return pthread_once (once, func);
412 else
413 return -1;
414 }
415
416 static inline int
417 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
418 {
419 return pthread_keycreate (key, dtor);
420 }
421
422 static inline int
423 __gthread_key_dtor (UNUSED (__gthread_key_t key), UNUSED (void *ptr))
424 {
425 /* Nothing needed. */
426 return 0;
427 }
428
429 static inline int
430 __gthread_key_delete (UNUSED (__gthread_key_t key))
431 {
432 /* Operation is not supported. */
433 return -1;
434 }
435
436 static inline void *
437 __gthread_getspecific (__gthread_key_t key)
438 {
439 void *ptr;
440 if (pthread_getspecific (key, &ptr) == 0)
441 return ptr;
442 else
443 return 0;
444 }
445
446 static inline int
447 __gthread_setspecific (__gthread_key_t key, const void *ptr)
448 {
449 return pthread_setspecific (key, (void *) ptr);
450 }
451
452 static inline void
453 __gthread_mutex_init_function (__gthread_mutex_t *mutex)
454 {
455 if (__gthread_active_p ())
456 pthread_mutex_init (mutex, pthread_mutexattr_default);
457 }
458
459 static inline int
460 __gthread_mutex_lock (__gthread_mutex_t *mutex)
461 {
462 if (__gthread_active_p ())
463 return pthread_mutex_lock (mutex);
464 else
465 return 0;
466 }
467
468 static inline int
469 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
470 {
471 if (__gthread_active_p ())
472 return pthread_mutex_trylock (mutex);
473 else
474 return 0;
475 }
476
477 static inline int
478 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
479 {
480 if (__gthread_active_p ())
481 return pthread_mutex_unlock (mutex);
482 else
483 return 0;
484 }
485
486 #endif /* _LIBOBJC */
487
488 #undef UNUSED
489
490 #endif /* ! GCC_GTHR_DCE_H */