# gthr-default.h

    00001 /* Threads compatibility routines for libgcc2 and libobjc.  */
    00002 /* Compile this one with gcc.  */
    00003 /* Copyright (C) 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005
    00004    Free Software Foundation, Inc.
    00005
    00006 This file is part of GCC.
    00007
    00008 GCC is free software; you can redistribute it and/or modify it under
    00009 the terms of the GNU General Public License as published by the Free
    00010 Software Foundation; either version 2, or (at your option) any later
    00011 version.
    00012
    00013 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
    00014 WARRANTY; without even the implied warranty of MERCHANTABILITY or
    00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    00016 for more details.
    00017
    00018 You should have received a copy of the GNU General Public License
    00019 along with GCC; see the file COPYING.  If not, write to the Free
    00020 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
    00021 02111-1307, USA.  */
    00022
    00023 /* As a special exception, if you link this library with other files,
    00024    some of which are compiled with GCC, to produce an executable,
    00025    this library does not by itself cause the resulting executable
    00026    to be covered by the GNU General Public License.
    00027    This exception does not however invalidate any other reasons why
    00028    the executable file might be covered by the GNU General Public License.  */
    00029
    00030 #ifndef _GLIBCXX_GCC_GTHR_POSIX_H
    00031 #define _GLIBCXX_GCC_GTHR_POSIX_H
    00032
    00033 /* POSIX threads specific definitions.
    00034    Easy, since the interface is just one-to-one mapping.  */
    00035
    00036 #define __GTHREADS 1
    00037
    00038 /* Some implementations of <pthread.h> require this to be defined.  */
    00039 #ifndef _REENTRANT
    00040 #define _REENTRANT 1
    00041 #endif
    00042
    00043 #include <pthread.h>
    00044 #include <unistd.h>
    00045
    00046 typedef pthread_key_t __gthread_key_t;
    00047 typedef pthread_once_t __gthread_once_t;
    00048 typedef pthread_mutex_t __gthread_mutex_t;
    00049 typedef pthread_mutex_t __gthread_recursive_mutex_t;
    00050
    00051 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
    00052 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
    00053 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
    00054 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
    00055 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
    00056 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
    00057 #else
    00058 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
    00059 #endif
    00060
    00061 #if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
    00062
    00063 #pragma weak pthread_once
    00064 #pragma weak pthread_key_create
    00065 #pragma weak pthread_key_delete
    00066 #pragma weak pthread_getspecific
    00067 #pragma weak pthread_setspecific
    00068 #pragma weak pthread_create
    00069 #pragma weak pthread_cancel
    00070
    00071 #pragma weak pthread_mutex_lock
    00072 #pragma weak pthread_mutex_trylock
    00073 #pragma weak pthread_mutex_unlock
    00074 #pragma weak pthread_mutexattr_init
    00075 #pragma weak pthread_mutexattr_settype
    00076 #pragma weak pthread_mutexattr_destroy
    00077
    00078 #pragma weak pthread_mutex_init
    00079
    00080 #if defined(_LIBOBJC) || defined(_LIBOBJC_WEAK)
    00081 /* Objective-C.  */
    00082 #pragma weak pthread_cond_broadcast
    00083 #pragma weak pthread_cond_destroy
    00084 #pragma weak pthread_cond_init
    00085 #pragma weak pthread_cond_signal
    00086 #pragma weak pthread_cond_wait
    00087 #pragma weak pthread_exit
    00088 #pragma weak pthread_mutex_destroy
    00089 #pragma weak pthread_self
    00090 #ifdef _POSIX_PRIORITY_SCHEDULING
    00091 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
    00092 #pragma weak sched_get_priority_max
    00093 #pragma weak sched_get_priority_min
    00094 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
    00095 #endif /* _POSIX_PRIORITY_SCHEDULING */
    00096 #pragma weak sched_yield
    00097 #pragma weak pthread_attr_destroy
    00098 #pragma weak pthread_attr_init
    00099 #pragma weak pthread_attr_setdetachstate
    00100 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
    00101 #pragma weak pthread_getschedparam
    00102 #pragma weak pthread_setschedparam
    00103 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
    00104 #endif /* _LIBOBJC || _LIBOBJC_WEAK */
    00105
    00106 static inline int
    00107 __gthread_active_p (void)
    00108 {
    00109   static void *const __gthread_active_ptr
    00110     = __extension__ (void *) &pthread_cancel;
    00111   return __gthread_active_ptr != 0;
    00112 }
    00113
    00114 #else /* not __GXX_WEAK__ */
    00115
    00116 static inline int
    00117 __gthread_active_p (void)
    00118 {
    00119   return 1;
    00120 }
    00121
    00122 #endif /* __GXX_WEAK__ */
    00123
    00124 #ifdef _LIBOBJC
    00125
    00126 /* This is the config.h file in libobjc/ */
    00127 #include <config.h>
    00128
    00129 #ifdef HAVE_SCHED_H
    00130 # include <sched.h>
    00131 #endif
    00132
    00133 /* Key structure for maintaining thread specific storage */
    00134 static pthread_key_t _objc_thread_storage;
    00135 static pthread_attr_t _objc_thread_attribs;
    00136
    00137 /* Thread local storage for a single thread */
    00138 static void *thread_local_storage = NULL;
    00139
    00140 /* Backend initialization functions */
    00141
    00142 /* Initialize the threads subsystem.  */
    00143 static inline int
    00144 __gthread_objc_init_thread_system (void)
    00145 {
    00146   if (__gthread_active_p ())
    00147     {
    00148       /* Initialize the thread storage key.  */
    00149       if (pthread_key_create (&_objc_thread_storage, NULL) == 0)
    00150     {
    00151       /* The normal default detach state for threads is
    00152        * PTHREAD_CREATE_JOINABLE which causes threads to not die
    00153        * when you think they should.  */
    00154       if (pthread_attr_init (&_objc_thread_attribs) == 0
    00155           && pthread_attr_setdetachstate (&_objc_thread_attribs,
    00156                           PTHREAD_CREATE_DETACHED) == 0)
    00157         return 0;
    00158     }
    00159     }
    00160
    00161   return -1;
    00162 }
    00163
    00164 /* Close the threads subsystem.  */
    00165 static inline int
    00166 __gthread_objc_close_thread_system (void)
    00167 {
    00168   if (__gthread_active_p ()
    00169       && pthread_key_delete (_objc_thread_storage) == 0
    00170       && pthread_attr_destroy (&_objc_thread_attribs) == 0)
    00171     return 0;
    00172
    00173   return -1;
    00174 }
    00175
    00176 /* Backend thread functions */
    00177
    00178 /* Create a new thread of execution.  */
    00179 static inline objc_thread_t
    00180 __gthread_objc_thread_detach (void (*func)(void *), void *arg)
    00181 {
    00182   objc_thread_t thread_id;
    00183   pthread_t new_thread_handle;
    00184
    00185   if (!__gthread_active_p ())
    00186     return NULL;
    00187
    00188   if (!(pthread_create (&new_thread_handle, NULL, (void *) func, arg)))
    00189     thread_id = (objc_thread_t) new_thread_handle;
    00190   else
    00191     thread_id = NULL;
    00192
    00193   return thread_id;
    00194 }
    00195
    00196 /* Set the current thread's priority.  */
    00197 static inline int
    00198 __gthread_objc_thread_set_priority (int priority)
    00199 {
    00200   if (!__gthread_active_p ())
    00201     return -1;
    00202   else
    00203     {
    00204 #ifdef _POSIX_PRIORITY_SCHEDULING
    00205 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
    00206       pthread_t thread_id = pthread_self ();
    00207       int policy;
    00208       struct sched_param params;
    00209       int priority_min, priority_max;
    00210
    00211       if (pthread_getschedparam (thread_id, &policy, &params) == 0)
    00212     {
    00213       if ((priority_max = sched_get_priority_max (policy)) == -1)
    00214         return -1;
    00215
    00216       if ((priority_min = sched_get_priority_min (policy)) == -1)
    00217         return -1;
    00218
    00219       if (priority > priority_max)
    00220         priority = priority_max;
    00221       else if (priority < priority_min)
    00222         priority = priority_min;
    00223       params.sched_priority = priority;
    00224
    00225       /*
    00226        * The solaris 7 and several other man pages incorrectly state that
    00227        * this should be a pointer to policy but pthread.h is universally
    00228        * at odds with this.
    00229        */
    00230       if (pthread_setschedparam (thread_id, policy, &params) == 0)
    00231         return 0;
    00232     }
    00233 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
    00234 #endif /* _POSIX_PRIORITY_SCHEDULING */
    00235       return -1;
    00236     }
    00237 }
    00238
    00239 /* Return the current thread's priority.  */
    00240 static inline int
    00241 __gthread_objc_thread_get_priority (void)
    00242 {
    00243 #ifdef _POSIX_PRIORITY_SCHEDULING
    00244 #ifdef _POSIX_THREAD_PRIORITY_SCHEDULING
    00245   if (__gthread_active_p ())
    00246     {
    00247       int policy;
    00248       struct sched_param params;
    00249
    00250       if (pthread_getschedparam (pthread_self (), &policy, &params) == 0)
    00251     return params.sched_priority;
    00252       else
    00253     return -1;
    00254     }
    00255   else
    00256 #endif /* _POSIX_THREAD_PRIORITY_SCHEDULING */
    00257 #endif /* _POSIX_PRIORITY_SCHEDULING */
    00258     return OBJC_THREAD_INTERACTIVE_PRIORITY;
    00259 }
    00260
    00261 /* Yield our process time to another thread.  */
    00262 static inline void
    00263 __gthread_objc_thread_yield (void)
    00264 {
    00265   if (__gthread_active_p ())
    00266     sched_yield ();
    00267 }
    00268
    00269 /* Terminate the current thread.  */
    00270 static inline int
    00271 __gthread_objc_thread_exit (void)
    00272 {
    00273   if (__gthread_active_p ())
    00274     /* exit the thread */
    00275     pthread_exit (&__objc_thread_exit_status);
    00276
    00277   /* Failed if we reached here */
    00278   return -1;
    00279 }
    00280
    00281 /* Returns an integer value which uniquely describes a thread.  */
    00282 static inline objc_thread_t
    00283 __gthread_objc_thread_id (void)
    00284 {
    00285   if (__gthread_active_p ())
    00286     return (objc_thread_t) pthread_self ();
    00287   else
    00288     return (objc_thread_t) 1;
    00289 }
    00290
    00291 /* Sets the thread's local storage pointer.  */
    00292 static inline int
    00293 __gthread_objc_thread_set_data (void *value)
    00294 {
    00295   if (__gthread_active_p ())
    00296     return pthread_setspecific (_objc_thread_storage, value);
    00297   else
    00298     {
    00299       thread_local_storage = value;
    00300       return 0;
    00301     }
    00302 }
    00303
    00304 /* Returns the thread's local storage pointer.  */
    00305 static inline void *
    00306 __gthread_objc_thread_get_data (void)
    00307 {
    00308   if (__gthread_active_p ())
    00309     return pthread_getspecific (_objc_thread_storage);
    00310   else
    00311     return thread_local_storage;
    00312 }
    00313
    00314 /* Backend mutex functions */
    00315
    00316 /* Allocate a mutex.  */
    00317 static inline int
    00318 __gthread_objc_mutex_allocate (objc_mutex_t mutex)
    00319 {
    00320   if (__gthread_active_p ())
    00321     {
    00322       mutex->backend = objc_malloc (sizeof (pthread_mutex_t));
    00323
    00324       if (pthread_mutex_init ((pthread_mutex_t *) mutex->backend, NULL))
    00325     {
    00326       objc_free (mutex->backend);
    00327       mutex->backend = NULL;
    00328       return -1;
    00329     }
    00330     }
    00331
    00332   return 0;
    00333 }
    00334
    00335 /* Deallocate a mutex.  */
    00336 static inline int
    00337 __gthread_objc_mutex_deallocate (objc_mutex_t mutex)
    00338 {
    00339   if (__gthread_active_p ())
    00340     {
    00341       int count;
    00342
    00343       /*
    00344        * Posix Threads specifically require that the thread be unlocked
    00345        * for pthread_mutex_destroy to work.
    00346        */
    00347
    00348       do
    00349     {
    00350       count = pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend);
    00351       if (count < 0)
    00352         return -1;
    00353     }
    00354       while (count);
    00355
    00356       if (pthread_mutex_destroy ((pthread_mutex_t *) mutex->backend))
    00357     return -1;
    00358
    00359       objc_free (mutex->backend);
    00360       mutex->backend = NULL;
    00361     }
    00362   return 0;
    00363 }
    00364
    00365 /* Grab a lock on a mutex.  */
    00366 static inline int
    00367 __gthread_objc_mutex_lock (objc_mutex_t mutex)
    00368 {
    00369   if (__gthread_active_p ()
    00370       && pthread_mutex_lock ((pthread_mutex_t *) mutex->backend) != 0)
    00371     {
    00372       return -1;
    00373     }
    00374
    00375   return 0;
    00376 }
    00377
    00378 /* Try to grab a lock on a mutex.  */
    00379 static inline int
    00380 __gthread_objc_mutex_trylock (objc_mutex_t mutex)
    00381 {
    00382   if (__gthread_active_p ()
    00383       && pthread_mutex_trylock ((pthread_mutex_t *) mutex->backend) != 0)
    00384     {
    00385       return -1;
    00386     }
    00387
    00388   return 0;
    00389 }
    00390
    00391 /* Unlock the mutex */
    00392 static inline int
    00393 __gthread_objc_mutex_unlock (objc_mutex_t mutex)
    00394 {
    00395   if (__gthread_active_p ()
    00396       && pthread_mutex_unlock ((pthread_mutex_t *) mutex->backend) != 0)
    00397     {
    00398       return -1;
    00399     }
    00400
    00401   return 0;
    00402 }
    00403
    00404 /* Backend condition mutex functions */
    00405
    00406 /* Allocate a condition.  */
    00407 static inline int
    00408 __gthread_objc_condition_allocate (objc_condition_t condition)
    00409 {
    00410   if (__gthread_active_p ())
    00411     {
    00412       condition->backend = objc_malloc (sizeof (pthread_cond_t));
    00413
    00414       if (pthread_cond_init ((pthread_cond_t *) condition->backend, NULL))
    00415     {
    00416       objc_free (condition->backend);
    00417       condition->backend = NULL;
    00418       return -1;
    00419     }
    00420     }
    00421
    00422   return 0;
    00423 }
    00424
    00425 /* Deallocate a condition.  */
    00426 static inline int
    00427 __gthread_objc_condition_deallocate (objc_condition_t condition)
    00428 {
    00429   if (__gthread_active_p ())
    00430     {
    00431       if (pthread_cond_destroy ((pthread_cond_t *) condition->backend))
    00432     return -1;
    00433
    00434       objc_free (condition->backend);
    00435       condition->backend = NULL;
    00436     }
    00437   return 0;
    00438 }
    00439
    00440 /* Wait on the condition */
    00441 static inline int
    00442 __gthread_objc_condition_wait (objc_condition_t condition, objc_mutex_t mutex)
    00443 {
    00444   if (__gthread_active_p ())
    00445     return pthread_cond_wait ((pthread_cond_t *) condition->backend,
    00446                   (pthread_mutex_t *) mutex->backend);
    00447   else
    00448     return 0;
    00449 }
    00450
    00451 /* Wake up all threads waiting on this condition.  */
    00452 static inline int
    00453 __gthread_objc_condition_broadcast (objc_condition_t condition)
    00454 {
    00455   if (__gthread_active_p ())
    00456     return pthread_cond_broadcast ((pthread_cond_t *) condition->backend);
    00457   else
    00458     return 0;
    00459 }
    00460
    00461 /* Wake up one thread waiting on this condition.  */
    00462 static inline int
    00463 __gthread_objc_condition_signal (objc_condition_t condition)
    00464 {
    00465   if (__gthread_active_p ())
    00466     return pthread_cond_signal ((pthread_cond_t *) condition->backend);
    00467   else
    00468     return 0;
    00469 }
    00470
    00471 #else /* _LIBOBJC */
    00472
    00473 static inline int
    00474 __gthread_once (__gthread_once_t *once, void (*func) (void))
    00475 {
    00476   if (__gthread_active_p ())
    00477     return pthread_once (once, func);
    00478   else
    00479     return -1;
    00480 }
    00481
    00482 static inline int
    00483 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
    00484 {
    00485   return pthread_key_create (key, dtor);
    00486 }
    00487
    00488 static inline int
    00489 __gthread_key_delete (__gthread_key_t key)
    00490 {
    00491   return pthread_key_delete (key);
    00492 }
    00493
    00494 static inline void *
    00495 __gthread_getspecific (__gthread_key_t key)
    00496 {
    00497   return pthread_getspecific (key);
    00498 }
    00499
    00500 static inline int
    00501 __gthread_setspecific (__gthread_key_t key, const void *ptr)
    00502 {
    00503   return pthread_setspecific (key, ptr);
    00504 }
    00505
    00506 static inline int
    00507 __gthread_mutex_lock (__gthread_mutex_t *mutex)
    00508 {
    00509   if (__gthread_active_p ())
    00510     return pthread_mutex_lock (mutex);
    00511   else
    00512     return 0;
    00513 }
    00514
    00515 static inline int
    00516 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
    00517 {
    00518   if (__gthread_active_p ())
    00519     return pthread_mutex_trylock (mutex);
    00520   else
    00521     return 0;
    00522 }
    00523
    00524 static inline int
    00525 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
    00526 {
    00527   if (__gthread_active_p ())
    00528     return pthread_mutex_unlock (mutex);
    00529   else
    00530     return 0;
    00531 }
    00532
    00533 #ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
    00534 static inline int
    00535 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
    00536 {
    00537   if (__gthread_active_p ())
    00538     {
    00539       pthread_mutexattr_t attr;
    00540       int r;
    00541
    00542       r = pthread_mutexattr_init (&attr);
    00543       if (!r)
    00544     r = pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
    00545       if (!r)
    00546     r = pthread_mutex_init (mutex, &attr);
    00547       if (!r)
    00548     r = pthread_mutexattr_destroy (&attr);
    00549       return r;
    00550     }
    00551 }
    00552 #endif
    00553
    00554 static inline int
    00555 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
    00556 {
    00557   return __gthread_mutex_lock (mutex);
    00558 }
    00559
    00560 static inline int
    00561 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
    00562 {
    00563   return __gthread_mutex_trylock (mutex);
    00564 }
    00565
    00566 static inline int
    00567 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
    00568 {
    00569   return __gthread_mutex_unlock (mutex);
    00570 }
    00571
    00572 #endif /* _LIBOBJC */
    00573
    00574 #endif /* ! _GLIBCXX_GCC_GTHR_POSIX_H */