OSDN Git Service

* gthr-posix.h [LIBOBJC]: Add weak definitions of
[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 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
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.
11
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.
16
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.  */
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_POSIX_H
30 #define GCC_GTHR_POSIX_H
31
32 /* POSIX threads specific definitions.
33    Easy, since the interface is just one-to-one mapping. */
34
35 #define __GTHREADS 1
36
37 #include <pthread.h>
38
39 typedef pthread_key_t __gthread_key_t;
40 typedef pthread_once_t __gthread_once_t;
41 typedef pthread_mutex_t __gthread_mutex_t;
42
43 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
44 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
45
46 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
47
48 #pragma weak pthread_once
49 #pragma weak pthread_key_create
50 #pragma weak pthread_key_delete
51 #pragma weak pthread_getspecific
52 #pragma weak pthread_setspecific
53 #pragma weak pthread_create
54
55 #pragma weak pthread_mutex_lock 
56 #pragma weak pthread_mutex_trylock 
57 #pragma weak pthread_mutex_unlock 
58
59 #ifdef _LIBOBJC
60 /* Objective C. */
61 #pragma weak pthread_cond_broadcast
62 #pragma weak pthread_cond_destroy
63 #pragma weak pthread_cond_init
64 #pragma weak pthread_cond_signal
65 #pragma weak pthread_cond_wait
66 #pragma weak pthread_exit
67 #pragma weak pthread_mutex_init
68 #pragma weak pthread_mutex_destroy
69 #pragma weak pthread_self
70 #pragma weak sched_get_priority_max
71 #pragma weak sched_get_priority_min
72 #pragma weak sched_yield
73 #pragma weak pthread_attr_destroy
74 #pragma weak pthread_attr_init
75 #pragma weak pthread_attr_setdetachstate
76 #pragma weak pthread_getschedparam
77 #pragma weak pthread_setschedparam
78 #endif
79
80 static void *__gthread_active_ptr = (void *) &pthread_create;
81
82 static inline int
83 __gthread_active_p (void)
84 {
85   return __gthread_active_ptr != 0;
86 }
87
88 #else /* not SUPPORTS_WEAK */
89
90 static inline int
91 __gthread_active_p (void)
92 {
93   return 1;
94 }
95
96 #endif /* SUPPORTS_WEAK */
97
98 #ifdef _LIBOBJC
99
100 /* This is the config.h file in libobjc/ */
101 #include <config.h>
102
103 #ifdef HAVE_SCHED_H
104 # include <sched.h>
105 #endif
106
107 /* Key structure for maintaining thread specific storage */
108 static pthread_key_t _objc_thread_storage;
109 static pthread_attr_t _objc_thread_attribs;
110
111 /* Thread local storage for a single thread */
112 static void *thread_local_storage = NULL;
113
114 /* Backend initialization functions */
115
116 /* Initialize the threads subsystem. */
117 static inline int
118 __gthread_objc_init_thread_system(void)
119 {
120   if (__gthread_active_p ())
121     {
122       /* Initialize the thread storage key */
123       if (pthread_key_create(&_objc_thread_storage, NULL) == 0)
124         {
125           /* The normal default detach state for threads is
126            * PTHREAD_CREATE_JOINABLE which causes threads to not die
127            * when you think they should.  */
128           if (pthread_attr_init(&_objc_thread_attribs) == 0
129               && pthread_attr_setdetachstate(&_objc_thread_attribs, 
130                                              PTHREAD_CREATE_DETACHED) == 0)
131             return 0;
132         }
133     }
134
135   return -1;
136 }
137
138 /* Close the threads subsystem. */
139 static inline int
140 __gthread_objc_close_thread_system(void)
141 {
142   if (__gthread_active_p ()
143       && pthread_key_delete(_objc_thread_storage) == 0
144       && pthread_attr_destroy(&_objc_thread_attribs) == 0)
145     return 0;
146
147   return -1;
148 }
149
150 /* Backend thread functions */
151
152 /* Create a new thread of execution. */
153 static inline objc_thread_t
154 __gthread_objc_thread_detach(void (*func)(void *), void *arg)
155 {
156   objc_thread_t thread_id;
157   pthread_t new_thread_handle;
158
159   if (!__gthread_active_p ())
160     return NULL;
161  
162   if ( !(pthread_create(&new_thread_handle, NULL, (void *)func, arg)) )
163     thread_id = (objc_thread_t) new_thread_handle;
164   else
165     thread_id = NULL;
166   
167   return thread_id;
168 }
169
170 /* Set the current thread's priority. */
171 static inline int
172 __gthread_objc_thread_set_priority(int priority)
173 {
174   if (!__gthread_active_p())
175     return -1;
176   else {
177     pthread_t thread_id = pthread_self();
178     int policy;
179     struct sched_param params;
180     int priority_min, priority_max;
181
182     if (pthread_getschedparam(thread_id, &policy, &params) == 0)
183       {
184         if ((priority_max = sched_get_priority_max(policy)) != 0)
185           return -1;
186
187         if ((priority_min = sched_get_priority_min(policy)) != 0)
188           return -1;
189
190         if (priority > priority_max)
191           priority = priority_max;
192         else if (priority < priority_min)
193           priority = priority_min;
194         params.sched_priority = priority;
195
196         /*
197          * The solaris 7 and several other man pages incorrectly state that
198          * this should be a pointer to policy but pthread.h is universally
199          * at odds with this.
200          */
201         if (pthread_setschedparam(thread_id, policy, &params) == 0)
202           return 0;
203       }
204     return -1;
205   }
206 }
207
208 /* Return the current thread's priority. */
209 static inline int
210 __gthread_objc_thread_get_priority(void)
211 {
212   if (__gthread_active_p ())
213     {
214       int policy;
215       struct sched_param params;
216
217       if (pthread_getschedparam(pthread_self(), &policy, &params) == 0)
218         return params.sched_priority;
219       else
220         return -1;
221     }
222   else
223     return OBJC_THREAD_INTERACTIVE_PRIORITY;
224 }
225
226 /* Yield our process time to another thread. */
227 static inline void
228 __gthread_objc_thread_yield(void)
229 {
230   if (__gthread_active_p ())
231     sched_yield();
232 }
233
234 /* Terminate the current thread. */
235 static inline int
236 __gthread_objc_thread_exit(void)
237 {
238   if (__gthread_active_p ())
239     /* exit the thread */
240     pthread_exit(&__objc_thread_exit_status);
241
242   /* Failed if we reached here */
243   return -1;
244 }
245
246 /* Returns an integer value which uniquely describes a thread. */
247 static inline objc_thread_t
248 __gthread_objc_thread_id(void)
249 {
250   if (__gthread_active_p ())
251     return (objc_thread_t) pthread_self();
252   else
253     return (objc_thread_t) 1;
254 }
255
256 /* Sets the thread's local storage pointer. */
257 static inline int
258 __gthread_objc_thread_set_data(void *value)
259 {
260   if (__gthread_active_p ())
261     return pthread_setspecific(_objc_thread_storage, value);
262   else
263     {
264       thread_local_storage = value;
265       return 0;
266     }
267 }
268
269 /* Returns the thread's local storage pointer. */
270 static inline void *
271 __gthread_objc_thread_get_data(void)
272 {
273   if (__gthread_active_p ())
274     return pthread_getspecific(_objc_thread_storage);
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, NULL))
290         {
291           objc_free(mutex->backend);
292           mutex->backend = NULL;
293           return -1;
294         }
295     }
296
297   return 0;
298 }
299
300 /* Deallocate a mutex. */
301 static inline int
302 __gthread_objc_mutex_deallocate(objc_mutex_t mutex)
303 {
304   if (__gthread_active_p ())
305     {
306       int count;
307
308       /*
309        * Posix Threads specifically require that the thread be unlocked
310        * for pthread_mutex_destroy to work.
311        */
312
313       do
314         {
315           count = pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
316           if (count < 0)
317             return -1;
318         }
319       while (count);
320
321       if (pthread_mutex_destroy((pthread_mutex_t *)mutex->backend))
322         return -1;
323
324       objc_free(mutex->backend);
325       mutex->backend = NULL;
326     }
327   return 0;
328 }
329
330 /* Grab a lock on a mutex. */
331 static inline int
332 __gthread_objc_mutex_lock(objc_mutex_t mutex)
333 {
334   if (__gthread_active_p ())
335     return pthread_mutex_lock((pthread_mutex_t *)mutex->backend);
336   else
337     return 0;
338 }
339
340 /* Try to grab a lock on a mutex. */
341 static inline int
342 __gthread_objc_mutex_trylock(objc_mutex_t mutex)
343 {
344   if (__gthread_active_p ())
345     return pthread_mutex_trylock((pthread_mutex_t *)mutex->backend);
346   else
347     return 0;
348 }
349
350 /* Unlock the mutex */
351 static inline int
352 __gthread_objc_mutex_unlock(objc_mutex_t mutex)
353 {
354   if (__gthread_active_p ())
355     return pthread_mutex_unlock((pthread_mutex_t *)mutex->backend);
356   else
357     return 0;
358 }
359
360 /* Backend condition mutex functions */
361
362 /* Allocate a condition. */
363 static inline int
364 __gthread_objc_condition_allocate(objc_condition_t condition)
365 {
366   if (__gthread_active_p ())
367     {
368       condition->backend = objc_malloc(sizeof(pthread_cond_t));
369
370       if (pthread_cond_init((pthread_cond_t *)condition->backend, NULL))
371         {
372           objc_free(condition->backend);
373           condition->backend = NULL;
374           return -1;
375         }
376     }
377
378   return 0;
379 }
380
381 /* Deallocate a condition. */
382 static inline int
383 __gthread_objc_condition_deallocate(objc_condition_t condition)
384 {
385   if (__gthread_active_p ())
386     {
387       if (pthread_cond_destroy((pthread_cond_t *)condition->backend))
388         return -1;
389
390       objc_free(condition->backend);
391       condition->backend = NULL;
392     }
393   return 0;
394 }
395
396 /* Wait on the condition */
397 static inline int
398 __gthread_objc_condition_wait(objc_condition_t condition, objc_mutex_t mutex)
399 {
400   if (__gthread_active_p ())
401     return pthread_cond_wait((pthread_cond_t *)condition->backend,
402                            (pthread_mutex_t *)mutex->backend);
403   else
404     return 0;
405 }
406
407 /* Wake up all threads waiting on this condition. */
408 static inline int
409 __gthread_objc_condition_broadcast(objc_condition_t condition)
410 {
411   if (__gthread_active_p ())
412     return pthread_cond_broadcast((pthread_cond_t *)condition->backend);
413   else
414     return 0;
415 }
416
417 /* Wake up one thread waiting on this condition. */
418 static inline int
419 __gthread_objc_condition_signal(objc_condition_t condition)
420 {
421   if (__gthread_active_p ())
422     return pthread_cond_signal((pthread_cond_t *)condition->backend);
423   else
424     return 0;
425 }
426
427 #else /* _LIBOBJC */
428
429 static inline int
430 __gthread_once (__gthread_once_t *once, void (*func) (void))
431 {
432   if (__gthread_active_p ())
433     return pthread_once (once, func);
434   else
435     return -1;
436 }
437
438 static inline int
439 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
440 {
441   return pthread_key_create (key, dtor);
442 }
443
444 static inline int
445 __gthread_key_dtor (__gthread_key_t key, void *ptr)
446 {
447   /* Just reset the key value to zero. */
448   if (ptr)
449     return pthread_setspecific (key, 0);
450   else
451     return 0;
452 }
453
454 static inline int
455 __gthread_key_delete (__gthread_key_t key)
456 {
457   return pthread_key_delete (key);
458 }
459
460 static inline void *
461 __gthread_getspecific (__gthread_key_t key)
462 {
463   return pthread_getspecific (key);
464 }
465
466 static inline int
467 __gthread_setspecific (__gthread_key_t key, const void *ptr)
468 {
469   return pthread_setspecific (key, ptr);
470 }
471
472 static inline int
473 __gthread_mutex_lock (__gthread_mutex_t *mutex)
474 {
475   if (__gthread_active_p ())
476     return pthread_mutex_lock (mutex);
477   else
478     return 0;
479 }
480
481 static inline int
482 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
483 {
484   if (__gthread_active_p ())
485     return pthread_mutex_trylock (mutex);
486   else
487     return 0;
488 }
489
490 static inline int
491 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
492 {
493   if (__gthread_active_p ())
494     return pthread_mutex_unlock (mutex);
495   else
496     return 0;
497 }
498
499 #endif /* _LIBOBJC */
500
501 #endif /* ! GCC_GTHR_POSIX_H */