OSDN Git Service

2005-09-27 Uros Bizjak <uros@kss-loka.si>
[pf3gnuchains/gcc-fork.git] / gcc / gthr-dce.h
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.
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, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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 /* 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.  */
41
42 #define __GTHREADS 1
43
44 #include <pthread.h>
45
46 #ifdef __cplusplus
47 #define UNUSED(x) x
48 #else
49 #define UNUSED(x) x __attribute__((unused))
50 #endif
51
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;
56
57 #define __GTHREAD_ONCE_INIT pthread_once_init
58
59 #define __GTHREAD_MUTEX_INIT_FUNCTION __gthread_mutex_init_function
60 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
61
62 #define __GTHREAD_MUTEX_INIT_DEFAULT pthread_once_init
63
64 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
65
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
80
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
94
95 static inline int
96 __gthread_active_p (void)
97 {
98   static void *const __gthread_active_ptr = (void *) &pthread_create;
99   return __gthread_active_ptr != 0;
100 }
101
102 #else /* not SUPPORTS_WEAK */
103
104 static inline int
105 __gthread_active_p (void)
106 {
107   return 1;
108 }
109
110 #endif /* SUPPORTS_WEAK */
111
112 #ifdef _LIBOBJC
113
114 /* Key structure for maintaining thread specific storage */
115 static pthread_key_t _objc_thread_storage;
116
117 /* Thread local storage for a single thread */
118 static void *thread_local_storage = NULL;
119
120 /* Backend initialization functions */
121
122 /* Initialize the threads subsystem.  */
123 static inline int
124 __gthread_objc_init_thread_system (void)
125 {
126   if (__gthread_active_p ())
127     /* Initialize the thread storage key.  */
128     return pthread_keycreate (&_objc_thread_storage, NULL);
129   else
130     return -1;
131 }
132
133 /* Close the threads subsystem.  */
134 static inline int
135 __gthread_objc_close_thread_system (void)
136 {
137   if (__gthread_active_p ())
138     return 0;
139   else
140     return -1;
141 }
142
143 /* Backend thread functions */
144
145 /* Create a new thread of execution.  */
146 static inline objc_thread_t
147 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
148 {
149   objc_thread_t thread_id;
150   pthread_t new_thread_handle;
151
152   if (!__gthread_active_p ())
153     return NULL;
154
155   if (!(pthread_create (&new_thread_handle, pthread_attr_default,
156                         (void *) func, arg)))
157     {
158       /* ??? May not work! (64bit) */
159       thread_id = *(objc_thread_t *) &new_thread_handle;
160       pthread_detach (&new_thread_handle); /* Fully detach thread.  */
161     }
162   else
163     thread_id = NULL;
164
165   return thread_id;
166 }
167
168 /* Set the current thread's priority.  */
169 static inline int
170 __gthread_objc_thread_set_priority (int priority)
171 {
172   int sys_priority = 0;
173
174   if (!__gthread_active_p ())
175     return -1;
176
177   switch (priority)
178     {
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;
189     }
190
191   /* Change the priority.  */
192   if (pthread_setprio (pthread_self (), sys_priority) >= 0)
193     return 0;
194   else
195     /* Failed */
196     return -1;
197 }
198
199 /* Return the current thread's priority.  */
200 static inline int
201 __gthread_objc_thread_get_priority (void)
202 {
203   int sys_priority;
204
205   if (__gthread_active_p ())
206     {
207       if ((sys_priority = pthread_getprio (pthread_self ())) >= 0)
208         {
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;
216         }
217
218       /* Failed */
219       return -1;
220     }
221   else
222     return OBJC_THREAD_INTERACTIVE_PRIORITY;
223 }
224
225 /* Yield our process time to another thread.  */
226 static inline void
227 __gthread_objc_thread_yield (void)
228 {
229   if (__gthread_active_p ())
230     pthread_yield ();
231 }
232
233 /* Terminate the current thread.  */
234 static inline int
235 __gthread_objc_thread_exit (void)
236 {
237   if (__gthread_active_p ())
238     /* exit the thread */
239     pthread_exit (&__objc_thread_exit_status);
240
241   /* Failed if we reached here */
242   return -1;
243 }
244
245 /* Returns an integer value which uniquely describes a thread.  */
246 static inline objc_thread_t
247 __gthread_objc_thread_id (void)
248 {
249   if (__gthread_active_p ())
250     {
251       pthread_t self = pthread_self ();
252
253       return (objc_thread_t) pthread_getunique_np (&self);
254     }
255   else
256     return (objc_thread_t) 1;
257 }
258
259 /* Sets the thread's local storage pointer.  */
260 static inline int
261 __gthread_objc_thread_set_data (void *value)
262 {
263   if (__gthread_active_p ())
264     return pthread_setspecific (_objc_thread_storage, value);
265   else
266     {
267       thread_local_storage = value;
268       return 0;
269     }
270 }
271
272 /* Returns the thread's local storage pointer.  */
273 static inline void *
274 __gthread_objc_thread_get_data (void)
275 {
276   void *value = NULL;
277
278   if (__gthread_active_p ())
279     {
280       if (!(pthread_getspecific (_objc_thread_storage, &value)))
281         return value;
282
283       return NULL;
284     }
285   else
286     return thread_local_storage;
287 }
288
289 /* Backend mutex functions */
290
291 /* Allocate a mutex.  */
292 static inline int
293 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
294 {
295   if (__gthread_active_p ())
296     {
297       mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
298
299       if (pthread_mutex_init ((pthread_mutex_t *) mutex->backend,
300                               pthread_mutexattr_default))
301         {
302           objc_free (mutex->backend);
303           mutex->backend = NULL;
304           return -1;
305         }
306     }
307
308   return 0;
309 }
310
311 /* Deallocate a mutex.  */
312 static inline int
313 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
314 {
315   if (__gthread_active_p ())
316     {
317       if (pthread_mutex_destroy ((pthread_mutex_t *) mutex->backend))
318         return -1;
319
320       objc_free (mutex->backend);
321       mutex->backend = NULL;
322     }
323
324   return 0;
325 }
326
327 /* Grab a lock on a mutex.  */
328 static inline int
329 __gthread_objc_mutex_lock (objc_mutex_t mutex)
330 {
331   if (__gthread_active_p ())
332     return pthread_mutex_lock ((pthread_mutex_t *) mutex->backend);
333   else
334     return 0;
335 }
336
337 /* Try to grab a lock on a mutex.  */
338 static inline int
339 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
340 {
341   if (__gthread_active_p ()
342       && pthread_mutex_trylock ((pthread_mutex_t *) mutex->backend) != 1)
343     return -1;
344
345   return 0;
346 }
347
348 /* Unlock the mutex */
349 static inline int
350 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
351 {
352   if (__gthread_active_p ())
353     return pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend);
354   else
355     return 0;
356 }
357
358 /* Backend condition mutex functions */
359
360 /* Allocate a condition.  */
361 static inline int
362 __gthread_objc_condition_allocate (objc_condition_t condition)
363 {
364   if (__gthread_active_p ())
365     /* Unimplemented.  */
366     return -1;
367   else
368     return 0;
369 }
370
371 /* Deallocate a condition.  */
372 static inline int
373 __gthread_objc_condition_deallocate (objc_condition_t condition)
374 {
375   if (__gthread_active_p ())
376     /* Unimplemented.  */
377     return -1;
378   else
379     return 0;
380 }
381
382 /* Wait on the condition */
383 static inline int
384 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
385 {
386   if (__gthread_active_p ())
387     /* Unimplemented.  */
388     return -1;
389   else
390     return 0;
391 }
392
393 /* Wake up all threads waiting on this condition.  */
394 static inline int
395 __gthread_objc_condition_broadcast (objc_condition_t condition)
396 {
397   if (__gthread_active_p ())
398     /* Unimplemented.  */
399     return -1;
400   else
401     return 0;
402 }
403
404 /* Wake up one thread waiting on this condition.  */
405 static inline int
406 __gthread_objc_condition_signal (objc_condition_t condition)
407 {
408   if (__gthread_active_p ())
409     /* Unimplemented.  */
410     return -1;
411   else
412     return 0;
413 }
414
415 #else /* _LIBOBJC */
416
417 static inline int
418 __gthread_once (__gthread_once_t *once, void (*func) (void))
419 {
420   if (__gthread_active_p ())
421     return pthread_once (once, func);
422   else
423     return -1;
424 }
425
426 static inline int
427 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
428 {
429   return pthread_keycreate (key, dtor);
430 }
431
432 static inline int
433 __gthread_key_delete (UNUSED (__gthread_key_t key))
434 {
435   /* Operation is not supported.  */
436   return -1;
437 }
438
439 static inline void *
440 __gthread_getspecific (__gthread_key_t key)
441 {
442   void *ptr;
443   if (pthread_getspecific (key, &ptr) == 0)
444     return ptr;
445   else
446     return 0;
447 }
448
449 static inline int
450 __gthread_setspecific (__gthread_key_t key, const void *ptr)
451 {
452   return pthread_setspecific (key, (void *) ptr);
453 }
454
455 static inline void
456 __gthread_mutex_init_function (__gthread_mutex_t *mutex)
457 {
458   if (__gthread_active_p ())
459     pthread_mutex_init (mutex, pthread_mutexattr_default);
460 }
461
462 static inline int
463 __gthread_mutex_lock (__gthread_mutex_t *mutex)
464 {
465   if (__gthread_active_p ())
466     return pthread_mutex_lock (mutex);
467   else
468     return 0;
469 }
470
471 static inline int
472 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
473 {
474   if (__gthread_active_p ())
475     return pthread_mutex_trylock (mutex);
476   else
477     return 0;
478 }
479
480 static inline int
481 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
482 {
483   if (__gthread_active_p ())
484     return pthread_mutex_unlock (mutex);
485   else
486     return 0;
487 }
488
489 static inline int
490 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
491 {
492   if (__gthread_active_p ())
493     {
494       pthread_mutexattr_t attr;
495       int r;
496
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;
505     }
506 }
507
508 static inline int
509 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
510 {
511   return __gthread_mutex_lock (mutex);
512 }
513
514 static inline int
515 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
516 {
517   return __gthread_mutex_trylock (mutex);
518 }
519
520 static inline int
521 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
522 {
523   return __gthread_mutex_unlock (mutex);
524 }
525
526 #endif /* _LIBOBJC */
527
528 #undef UNUSED
529
530 #endif
531 #endif /* ! GCC_GTHR_DCE_H */