00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #ifndef __SGI_STL_INTERNAL_THREADS_H
00049 #define __SGI_STL_INTERNAL_THREADS_H
00050
00051
00052 #include "bits/gthr.h"
00053
00054 namespace std
00055 {
00056
00057
00058
00059
00060 struct _Refcount_Base
00061 {
00062
00063 typedef size_t _RC_t;
00064
00065
00066 volatile _RC_t _M_ref_count;
00067
00068
00069 __gthread_mutex_t _M_ref_count_lock;
00070
00071 _Refcount_Base(_RC_t __n) : _M_ref_count(__n)
00072 {
00073 #ifdef __GTHREAD_MUTEX_INIT
00074 __gthread_mutex_t __tmp = __GTHREAD_MUTEX_INIT;
00075 _M_ref_count_lock = __tmp;
00076 #elif defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00077 __GTHREAD_MUTEX_INIT_FUNCTION (&_M_ref_count_lock);
00078 #else
00079 #error __GTHREAD_MUTEX_INIT or __GTHREAD_MUTEX_INIT_FUNCTION should be defined by gthr.h abstraction layer, report problem to libstdc++@gcc.gnu.org.
00080 #endif
00081 }
00082
00083 void
00084 _M_incr()
00085 {
00086 __gthread_mutex_lock(&_M_ref_count_lock);
00087 ++_M_ref_count;
00088 __gthread_mutex_unlock(&_M_ref_count_lock);
00089 }
00090
00091 _RC_t
00092 _M_decr()
00093 {
00094 __gthread_mutex_lock(&_M_ref_count_lock);
00095 volatile _RC_t __tmp = --_M_ref_count;
00096 __gthread_mutex_unlock(&_M_ref_count_lock);
00097 return __tmp;
00098 }
00099 };
00100
00101
00102
00103
00104
00105 #if defined (__GTHREAD_MUTEX_INIT)
00106
00107
00108
00109 template<int __dummy>
00110 struct _Swap_lock_struct
00111 { static __gthread_mutex_t _S_swap_lock; };
00112
00113 template<int __dummy>
00114 __gthread_mutex_t
00115 _Swap_lock_struct<__dummy>::_S_swap_lock = __GTHREAD_MUTEX_INIT;
00116
00117
00118
00119 inline unsigned long
00120 _Atomic_swap(unsigned long * __p, unsigned long __q)
00121 {
00122 __gthread_mutex_lock(&_Swap_lock_struct<0>::_S_swap_lock);
00123 unsigned long __result = *__p;
00124 *__p = __q;
00125 __gthread_mutex_unlock(&_Swap_lock_struct<0>::_S_swap_lock);
00126 return __result;
00127 }
00128 #endif
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 #if !defined(__GTHREAD_MUTEX_INIT) && defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00145 namespace __gnu_cxx
00146 {
00147 extern __gthread_mutex_t _GLIBCPP_mutex;
00148 extern __gthread_mutex_t *_GLIBCPP_mutex_address;
00149 extern __gthread_once_t _GLIBCPP_once;
00150 extern void _GLIBCPP_mutex_init (void);
00151 extern void _GLIBCPP_mutex_address_init (void);
00152 }
00153 #endif
00154
00155 namespace std
00156 {
00157 struct _STL_mutex_lock
00158 {
00159
00160 #if !defined(__GTHREAD_MUTEX_INIT) && defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00161 volatile int _M_init_flag;
00162 __gthread_once_t _M_once;
00163 #endif
00164 __gthread_mutex_t _M_lock;
00165
00166 void
00167 _M_initialize()
00168 {
00169 #ifdef __GTHREAD_MUTEX_INIT
00170
00171 #elif defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00172 if (_M_init_flag) return;
00173 if (__gthread_once (&__gnu_cxx::_GLIBCPP_once,
00174 __gnu_cxx::_GLIBCPP_mutex_init) != 0
00175 && __gthread_active_p ())
00176 abort ();
00177 __gthread_mutex_lock (&__gnu_cxx::_GLIBCPP_mutex);
00178 if (!_M_init_flag)
00179 {
00180
00181
00182
00183 __gnu_cxx::_GLIBCPP_mutex_address = &_M_lock;
00184 if (__gthread_once (&_M_once,
00185 __gnu_cxx::_GLIBCPP_mutex_address_init) != 0
00186 && __gthread_active_p ())
00187 abort ();
00188 _M_init_flag = 1;
00189 }
00190 __gthread_mutex_unlock (&__gnu_cxx::_GLIBCPP_mutex);
00191 #endif
00192 }
00193
00194 void
00195 _M_acquire_lock()
00196 {
00197 #if !defined(__GTHREAD_MUTEX_INIT) && defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00198 if (!_M_init_flag) _M_initialize();
00199 #endif
00200 __gthread_mutex_lock(&_M_lock);
00201 }
00202
00203 void
00204 _M_release_lock()
00205 {
00206 #if !defined(__GTHREAD_MUTEX_INIT) && defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00207 if (!_M_init_flag) _M_initialize();
00208 #endif
00209 __gthread_mutex_unlock(&_M_lock);
00210 }
00211 };
00212
00213 #ifdef __GTHREAD_MUTEX_INIT
00214 #define __STL_MUTEX_INITIALIZER = { __GTHREAD_MUTEX_INIT }
00215 #elif defined(__GTHREAD_MUTEX_INIT_FUNCTION)
00216 #ifdef __GTHREAD_MUTEX_INIT_DEFAULT
00217 #define __STL_MUTEX_INITIALIZER \
00218 = { 0, __GTHREAD_ONCE_INIT, __GTHREAD_MUTEX_INIT_DEFAULT }
00219 #else
00220 #define __STL_MUTEX_INITIALIZER = { 0, __GTHREAD_ONCE_INIT }
00221 #endif
00222 #endif
00223
00224
00225
00226
00227
00228 struct _STL_auto_lock
00229 {
00230 _STL_mutex_lock& _M_lock;
00231
00232 _STL_auto_lock(_STL_mutex_lock& __lock) : _M_lock(__lock)
00233 { _M_lock._M_acquire_lock(); }
00234
00235 ~_STL_auto_lock() { _M_lock._M_release_lock(); }
00236
00237 private:
00238 void operator=(const _STL_auto_lock&);
00239 _STL_auto_lock(const _STL_auto_lock&);
00240 };
00241
00242 }
00243
00244 #endif