OSDN Git Service

* lang-specs.h ("@objective-c"): Use cc1obj when -E is used so
[pf3gnuchains/gcc-fork.git] / gcc / gthr-posix.h
1 /* Threads compatibility routines for libgcc2 and libobjc.  */
2 /* Compile this one with gcc.  */
3 /* Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA.  */
22
23 /* As a special exception, if you link this library with other files,
24    some of which are compiled with GCC, to produce an executable,
25    this library does not by itself cause the resulting executable
26    to be covered by the GNU General Public License.
27    This exception does not however invalidate any other reasons why
28    the executable file might be covered by the GNU General Public License.  */
29
30 #ifndef GCC_GTHR_POSIX_H
31 #define GCC_GTHR_POSIX_H
32
33 /* POSIX threads specific definitions.
34    Easy, since the interface is just one-to-one mapping.  */
35
36 #define __GTHREADS 1
37
38 /* Some implementations of <pthread.h> require this to be defined.  */
39 #ifndef _REENTRANT
40 #define _REENTRANT 1
41 #endif
42
43 #include <pthread.h>
44 #include <unistd.h>
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 typedef pthread_mutex_t __gthread_recursive_mutex_t;
50
51 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
52 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
53 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
54 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
55 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
56 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
57 #else
58 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
59 #endif
60
61 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
62
63 #pragma weak pthread_once
64 #pragma weak pthread_key_create
65 #pragma weak pthread_key_delete
66 #pragma weak pthread_getspecific
67 #pragma weak pthread_setspecific
68 #pragma weak pthread_create
69
70 #pragma weak pthread_mutex_lock
71 #pragma weak pthread_mutex_trylock
72 #pragma weak pthread_mutex_unlock
73 #pragma weak pthread_mutexattr_init
74 #pragma weak pthread_mutexattr_settype
75 #pragma weak pthread_mutexattr_destroy
76
77 #pragma weak pthread_mutex_init
78
79 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
80 /* Objective-C.  */
81 #pragma weak pthread_cond_broadcast
82 #pragma weak pthread_cond_destroy
83 #pragma weak pthread_cond_init
84 #pragma weak pthread_cond_signal
85 #pragma weak pthread_cond_wait
86 #pragma weak pthread_exit
87 #pragma weak pthread_mutex_destroy
88 #pragma weak pthread_self
89 #ifdef _POSIX_PRIORITY_SCHEDULING
90 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
91 #pragma weak sched_get_priority_max
92 #pragma weak sched_get_priority_min
93 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
94 #endif /* _POSIX_PRIORITY_SCHEDULING */
95 #pragma weak sched_yield
96 #pragma weak pthread_attr_destroy
97 #pragma weak pthread_attr_init
98 #pragma weak pthread_attr_setdetachstate
99 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
100 #pragma weak pthread_getschedparam
101 #pragma weak pthread_setschedparam
102 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
103 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
104
105 static inline int
106 __gthread_active_p (void)
107 {
108   static void *const __gthread_active_ptr 
109     = __extension__ (void *) &pthread_create;
110   return __gthread_active_ptr != 0;
111 }
112
113 #else /* not SUPPORTS_WEAK */
114
115 static inline int
116 __gthread_active_p (void)
117 {
118   return 1;
119 }
120
121 #endif /* SUPPORTS_WEAK */
122
123 #ifdef _LIBOBJC
124
125 /* This is the config.h file in libobjc/ */
126 #include <config.h>
127
128 #ifdef HAVE_SCHED_H
129 # include <sched.h>
130 #endif
131
132 /* Key structure for maintaining thread specific storage */
133 static pthread_key_t _objc_thread_storage;
134 static pthread_attr_t _objc_thread_attribs;
135
136 /* Thread local storage for a single thread */
137 static void *thread_local_storage = NULL;
138
139 /* Backend initialization functions */
140
141 /* Initialize the threads subsystem.  */
142 static inline int
143 __gthread_objc_init_thread_system (void)
144 {
145   if (__gthread_active_p ())
146     {
147       /* Initialize the thread storage key.  */
148       if (pthread_key_create (&_objc_thread_storage, NULL) == 0)
149         {
150           /* The normal default detach state for threads is
151            * PTHREAD_CREATE_JOINABLE which causes threads to not die
152            * when you think they should.  */
153           if (pthread_attr_init (&_objc_thread_attribs) == 0
154               && pthread_attr_setdetachstate (&_objc_thread_attribs,
155                                               PTHREAD_CREATE_DETACHED) == 0)
156             return 0;
157         }
158     }
159
160   return -1;
161 }
162
163 /* Close the threads subsystem.  */
164 static inline int
165 __gthread_objc_close_thread_system (void)
166 {
167   if (__gthread_active_p ()
168       && pthread_key_delete (_objc_thread_storage) == 0
169       && pthread_attr_destroy (&_objc_thread_attribs) == 0)
170     return 0;
171
172   return -1;
173 }
174
175 /* Backend thread functions */
176
177 /* Create a new thread of execution.  */
178 static inline objc_thread_t
179 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
180 {
181   objc_thread_t thread_id;
182   pthread_t new_thread_handle;
183
184   if (!__gthread_active_p ())
185     return NULL;
186
187   if (!(pthread_create (&new_thread_handle, NULL, (void *) func, arg)))
188     thread_id = (objc_thread_t) new_thread_handle;
189   else
190     thread_id = NULL;
191
192   return thread_id;
193 }
194
195 /* Set the current thread's priority.  */
196 static inline int
197 __gthread_objc_thread_set_priority (int priority)
198 {
199   if (!__gthread_active_p ())
200     return -1;
201   else
202     {
203 #ifdef _POSIX_PRIORITY_SCHEDULING
204 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
205       pthread_t thread_id = pthread_self ();
206       int policy;
207       struct sched_param params;
208       int priority_min, priority_max;
209
210       if (pthread_getschedparam (thread_id, &policy, &params) == 0)
211         {
212           if ((priority_max = sched_get_priority_max (policy)) == -1)
213             return -1;
214
215           if ((priority_min = sched_get_priority_min (policy)) == -1)
216             return -1;
217
218           if (priority > priority_max)
219             priority = priority_max;
220           else if (priority < priority_min)
221             priority = priority_min;
222           params.sched_priority = priority;
223
224           /*
225            * The solaris 7 and several other man pages incorrectly state that
226            * this should be a pointer to policy but pthread.h is universally
227            * at odds with this.
228            */
229           if (pthread_setschedparam (thread_id, policy, &params) == 0)
230             return 0;
231         }
232 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
233 #endif /* _POSIX_PRIORITY_SCHEDULING */
234       return -1;
235     }
236 }
237
238 /* Return the current thread's priority.  */
239 static inline int
240 __gthread_objc_thread_get_priority (void)
241 {
242 #ifdef _POSIX_PRIORITY_SCHEDULING
243 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
244   if (__gthread_active_p ())
245     {
246       int policy;
247       struct sched_param params;
248
249       if (pthread_getschedparam (pthread_self (), &policy, &params) == 0)
250         return params.sched_priority;
251       else
252         return -1;
253     }
254   else
255 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
256 #endif /* _POSIX_PRIORITY_SCHEDULING */
257     return OBJC_THREAD_INTERACTIVE_PRIORITY;
258 }
259
260 /* Yield our process time to another thread.  */
261 static inline void
262 __gthread_objc_thread_yield (void)
263 {
264   if (__gthread_active_p ())
265     sched_yield ();
266 }
267
268 /* Terminate the current thread.  */
269 static inline int
270 __gthread_objc_thread_exit (void)
271 {
272   if (__gthread_active_p ())
273     /* exit the thread */
274     pthread_exit (&__objc_thread_exit_status);
275
276   /* Failed if we reached here */
277   return -1;
278 }
279
280 /* Returns an integer value which uniquely describes a thread.  */
281 static inline objc_thread_t
282 __gthread_objc_thread_id (void)
283 {
284   if (__gthread_active_p ())
285     return (objc_thread_t) pthread_self ();
286   else
287     return (objc_thread_t) 1;
288 }
289
290 /* Sets the thread's local storage pointer.  */
291 static inline int
292 __gthread_objc_thread_set_data (void *value)
293 {
294   if (__gthread_active_p ())
295     return pthread_setspecific (_objc_thread_storage, value);
296   else
297     {
298       thread_local_storage = value;
299       return 0;
300     }
301 }
302
303 /* Returns the thread's local storage pointer.  */
304 static inline void *
305 __gthread_objc_thread_get_data (void)
306 {
307   if (__gthread_active_p ())
308     return pthread_getspecific (_objc_thread_storage);
309   else
310     return thread_local_storage;
311 }
312
313 /* Backend mutex functions */
314
315 /* Allocate a mutex.  */
316 static inline int
317 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
318 {
319   if (__gthread_active_p ())
320     {
321       mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
322
323       if (pthread_mutex_init ((pthread_mutex_t *) mutex->backend, NULL))
324         {
325           objc_free (mutex->backend);
326           mutex->backend = NULL;
327           return -1;
328         }
329     }
330
331   return 0;
332 }
333
334 /* Deallocate a mutex.  */
335 static inline int
336 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
337 {
338   if (__gthread_active_p ())
339     {
340       int count;
341
342       /*
343        * Posix Threads specifically require that the thread be unlocked
344        * for pthread_mutex_destroy to work.
345        */
346
347       do
348         {
349           count = pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend);
350           if (count < 0)
351             return -1;
352         }
353       while (count);
354
355       if (pthread_mutex_destroy ((pthread_mutex_t *) mutex->backend))
356         return -1;
357
358       objc_free (mutex->backend);
359       mutex->backend = NULL;
360     }
361   return 0;
362 }
363
364 /* Grab a lock on a mutex.  */
365 static inline int
366 __gthread_objc_mutex_lock (objc_mutex_t mutex)
367 {
368   if (__gthread_active_p ()
369       && pthread_mutex_lock ((pthread_mutex_t *) mutex->backend) != 0)
370     {
371       return -1;
372     }
373
374   return 0;
375 }
376
377 /* Try to grab a lock on a mutex.  */
378 static inline int
379 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
380 {
381   if (__gthread_active_p ()
382       && pthread_mutex_trylock ((pthread_mutex_t *) mutex->backend) != 0)
383     {
384       return -1;
385     }
386
387   return 0;
388 }
389
390 /* Unlock the mutex */
391 static inline int
392 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
393 {
394   if (__gthread_active_p ()
395       && pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend) != 0)
396     {
397       return -1;
398     }
399
400   return 0;
401 }
402
403 /* Backend condition mutex functions */
404
405 /* Allocate a condition.  */
406 static inline int
407 __gthread_objc_condition_allocate (objc_condition_t condition)
408 {
409   if (__gthread_active_p ())
410     {
411       condition->backend = objc_malloc (sizeof (pthread_cond_t));
412
413       if (pthread_cond_init ((pthread_cond_t *) condition->backend, NULL))
414         {
415           objc_free (condition->backend);
416           condition->backend = NULL;
417           return -1;
418         }
419     }
420
421   return 0;
422 }
423
424 /* Deallocate a condition.  */
425 static inline int
426 __gthread_objc_condition_deallocate (objc_condition_t condition)
427 {
428   if (__gthread_active_p ())
429     {
430       if (pthread_cond_destroy ((pthread_cond_t *) condition->backend))
431         return -1;
432
433       objc_free (condition->backend);
434       condition->backend = NULL;
435     }
436   return 0;
437 }
438
439 /* Wait on the condition */
440 static inline int
441 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
442 {
443   if (__gthread_active_p ())
444     return pthread_cond_wait ((pthread_cond_t *) condition->backend,
445                               (pthread_mutex_t *) mutex->backend);
446   else
447     return 0;
448 }
449
450 /* Wake up all threads waiting on this condition.  */
451 static inline int
452 __gthread_objc_condition_broadcast (objc_condition_t condition)
453 {
454   if (__gthread_active_p ())
455     return pthread_cond_broadcast ((pthread_cond_t *) condition->backend);
456   else
457     return 0;
458 }
459
460 /* Wake up one thread waiting on this condition.  */
461 static inline int
462 __gthread_objc_condition_signal (objc_condition_t condition)
463 {
464   if (__gthread_active_p ())
465     return pthread_cond_signal ((pthread_cond_t *) condition->backend);
466   else
467     return 0;
468 }
469
470 #else /* _LIBOBJC */
471
472 static inline int
473 __gthread_once (__gthread_once_t *once, void (*func) (void))
474 {
475   if (__gthread_active_p ())
476     return pthread_once (once, func);
477   else
478     return -1;
479 }
480
481 static inline int
482 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
483 {
484   return pthread_key_create (key, dtor);
485 }
486
487 static inline int
488 __gthread_key_delete (__gthread_key_t key)
489 {
490   return pthread_key_delete (key);
491 }
492
493 static inline void *
494 __gthread_getspecific (__gthread_key_t key)
495 {
496   return pthread_getspecific (key);
497 }
498
499 static inline int
500 __gthread_setspecific (__gthread_key_t key, const void *ptr)
501 {
502   return pthread_setspecific (key, ptr);
503 }
504
505 static inline int
506 __gthread_mutex_lock (__gthread_mutex_t *mutex)
507 {
508   if (__gthread_active_p ())
509     return pthread_mutex_lock (mutex);
510   else
511     return 0;
512 }
513
514 static inline int
515 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
516 {
517   if (__gthread_active_p ())
518     return pthread_mutex_trylock (mutex);
519   else
520     return 0;
521 }
522
523 static inline int
524 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
525 {
526   if (__gthread_active_p ())
527     return pthread_mutex_unlock (mutex);
528   else
529     return 0;
530 }
531
532 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
533 static inline int
534 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
535 {
536   if (__gthread_active_p ())
537     {
538       pthread_mutexattr_t attr;
539       int r;
540
541       r = pthread_mutexattr_init (&attr);
542       if (!r)
543         r = pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
544       if (!r)
545         r = pthread_mutex_init (mutex, &attr);
546       if (!r)
547         r = pthread_mutexattr_destroy (&attr);
548       return r;
549     }
550 }
551 #endif
552
553 static inline int
554 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
555 {
556   return __gthread_mutex_lock (mutex);
557 }
558
559 static inline int
560 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
561 {
562   return __gthread_mutex_trylock (mutex);
563 }
564
565 static inline int
566 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
567 {
568   return __gthread_mutex_unlock (mutex);
569 }
570
571 #endif /* _LIBOBJC */
572
573 #endif /* ! GCC_GTHR_POSIX_H */