libstdc++
gthr-tpf.h
00001 /* Threads compatibility routines for libgcc2 and libobjc.
00002    Compile this one with gcc.
00003    Copyright (C) 2004, 2005, 2008, 2009 Free Software Foundation, Inc.
00004 
00005 This file is part of GCC.
00006 
00007 GCC is free software; you can redistribute it and/or modify it under
00008 the terms of the GNU General Public License as published by the Free
00009 Software Foundation; either version 3, or (at your option) any later
00010 version.
00011 
00012 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
00013 WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 Under Section 7 of GPL version 3, you are granted additional
00018 permissions described in the GCC Runtime Library Exception, version
00019 3.1, as published by the Free Software Foundation.
00020 
00021 You should have received a copy of the GNU General Public License and
00022 a copy of the GCC Runtime Library Exception along with this program;
00023 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00024 <http://www.gnu.org/licenses/>.  */
00025 
00026 /* TPF needs its own version of gthr-*.h because TPF always links to
00027    the thread library.  However, for performance reasons we still do not
00028    want to issue thread api calls unless a check is made to see that we
00029    are running as a thread.  */
00030 
00031 #ifndef _GLIBCXX_GCC_GTHR_TPF_H
00032 #define _GLIBCXX_GCC_GTHR_TPF_H
00033 
00034 /* POSIX threads specific definitions.
00035    Easy, since the interface is just one-to-one mapping.  */
00036 
00037 #define __GTHREADS 1
00038 
00039 /* Some implementations of <pthread.h> require this to be defined.  */
00040 #ifndef _REENTRANT
00041 #define _REENTRANT 1
00042 #endif
00043 
00044 #include <pthread.h>
00045 #include <unistd.h>
00046 
00047 typedef pthread_key_t __gthread_key_t;
00048 typedef pthread_once_t __gthread_once_t;
00049 typedef pthread_mutex_t __gthread_mutex_t;
00050 typedef pthread_mutex_t __gthread_recursive_mutex_t;
00051 
00052 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
00053 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
00054 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
00055 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
00056 #endif
00057 
00058 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
00059 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
00060 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
00061 
00062 #define NOTATHREAD   00
00063 #define ECBBASEPTR (unsigned long int) *(unsigned int *)0x00000514u
00064 #define ECBPG2PTR  ECBBASEPTR + 0x1000
00065 #define CE2THRCPTR *((unsigned char *)(ECBPG2PTR + 16))
00066 #define __tpf_pthread_active() (CE2THRCPTR != NOTATHREAD)
00067 
00068 #if __GXX_WEAK__ && _GLIBCXX_GTHREAD_USE_WEAK
00069 # define __gthrw(name) \
00070   static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)));
00071 # define __gthrw_(name) __gthrw_ ## name
00072 #else
00073 # define __gthrw(name)
00074 # define __gthrw_(name) name
00075 #endif
00076 
00077 __gthrw(pthread_once)
00078 __gthrw(pthread_key_create)
00079 __gthrw(pthread_key_delete)
00080 __gthrw(pthread_getspecific)
00081 __gthrw(pthread_setspecific)
00082 __gthrw(pthread_create)
00083 
00084 __gthrw(pthread_mutex_lock)
00085 __gthrw(pthread_mutex_trylock)
00086 __gthrw(pthread_mutex_unlock)
00087 __gthrw(pthread_mutexattr_init)
00088 __gthrw(pthread_mutexattr_settype)
00089 __gthrw(pthread_mutexattr_destroy)
00090 __gthrw(pthread_mutex_init)
00091 __gthrw(pthread_mutex_destroy)
00092 
00093 static inline int
00094 __gthread_active_p (void)
00095 {
00096   return 1;
00097 }
00098 
00099 static inline int
00100 __gthread_once (__gthread_once_t *__once, void (*__func) (void))
00101 {
00102   if (__tpf_pthread_active ())
00103     return __gthrw_(pthread_once) (__once, __func);
00104   else
00105     return -1;
00106 }
00107 
00108 static inline int
00109 __gthread_key_create (__gthread_key_t *__key, void (*__dtor) (void *))
00110 {
00111   if (__tpf_pthread_active ())
00112     return __gthrw_(pthread_key_create) (__key, __dtor);
00113   else
00114     return -1;
00115 }
00116 
00117 static inline int
00118 __gthread_key_delete (__gthread_key_t __key)
00119 {
00120   if (__tpf_pthread_active ())
00121     return __gthrw_(pthread_key_delete) (__key);
00122   else
00123     return -1;
00124 }
00125 
00126 static inline void *
00127 __gthread_getspecific (__gthread_key_t __key)
00128 {
00129   if (__tpf_pthread_active ())
00130     return __gthrw_(pthread_getspecific) (__key);
00131   else
00132     return NULL;
00133 }
00134 
00135 static inline int
00136 __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
00137 {
00138   if (__tpf_pthread_active ())
00139     return __gthrw_(pthread_setspecific) (__key, __ptr);
00140   else
00141     return -1;
00142 }
00143 
00144 static inline int
00145 __gthread_mutex_destroy (__gthread_mutex_t *__mutex)
00146 {
00147   if (__tpf_pthread_active ())
00148     return __gthrw_(pthread_mutex_destroy) (__mutex);
00149   else
00150     return 0;
00151 }
00152 
00153 static inline int
00154 __gthread_mutex_lock (__gthread_mutex_t *__mutex)
00155 {
00156   if (__tpf_pthread_active ())
00157     return __gthrw_(pthread_mutex_lock) (__mutex);
00158   else
00159     return 0;
00160 }
00161 
00162 static inline int
00163 __gthread_mutex_trylock (__gthread_mutex_t *__mutex)
00164 {
00165   if (__tpf_pthread_active ())
00166     return __gthrw_(pthread_mutex_trylock) (__mutex);
00167   else
00168     return 0;
00169 }
00170 
00171 static inline int
00172 __gthread_mutex_unlock (__gthread_mutex_t *__mutex)
00173 {
00174   if (__tpf_pthread_active ())
00175     return __gthrw_(pthread_mutex_unlock) (__mutex);
00176   else
00177     return 0;
00178 }
00179 
00180 static inline int
00181 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *__mutex)
00182 {
00183   if (__tpf_pthread_active ())
00184     return __gthread_mutex_lock (__mutex);
00185   else
00186     return 0;
00187 }
00188 
00189 static inline int
00190 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *__mutex)
00191 {
00192   if (__tpf_pthread_active ())
00193     return __gthread_mutex_trylock (__mutex);
00194   else
00195     return 0;
00196 }
00197 
00198 static inline int
00199 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *__mutex)
00200 {
00201   if (__tpf_pthread_active ())
00202     return __gthread_mutex_unlock (__mutex);
00203   else
00204     return 0;
00205 }
00206 
00207 static inline int
00208 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *__mutex)
00209 {
00210   if (__tpf_pthread_active ())
00211     {
00212       pthread_mutexattr_t __attr;
00213       int __r;
00214 
00215       __r = __gthrw_(pthread_mutexattr_init) (&__attr);
00216       if (!__r)
00217     __r = __gthrw_(pthread_mutexattr_settype) (&__attr,
00218                            PTHREAD_MUTEX_RECURSIVE);
00219       if (!__r)
00220     __r = __gthrw_(pthread_mutex_init) (__mutex, &__attr);
00221       if (!__r)
00222     __r = __gthrw_(pthread_mutexattr_destroy) (&__attr);
00223       return __r;
00224     }
00225   return 0;
00226 }
00227 
00228 
00229 #endif /* ! _GLIBCXX_GCC_GTHR_TPF_H */