Install gcc-4.4.0-tdm-1-g++-2.tar.gz
[msysgit.git] / mingw / lib / gcc / mingw32 / 4.4.0 / include / c++ / tr1_impl / boost_sp_counted_base.h
blob48e601f2171df27557d404fe776fa05581e59b71
1 // <tr1_impl/boost_sp_counted_base.h> -*- C++ -*-
3 // Copyright (C) 2007, 2009 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 // shared_count.hpp
26 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
28 // shared_ptr.hpp
29 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
30 // Copyright (C) 2001, 2002, 2003 Peter Dimov
32 // weak_ptr.hpp
33 // Copyright (C) 2001, 2002, 2003 Peter Dimov
35 // enable_shared_from_this.hpp
36 // Copyright (C) 2002 Peter Dimov
38 // Distributed under the Boost Software License, Version 1.0. (See
39 // accompanying file LICENSE_1_0.txt or copy at
40 // http://www.boost.org/LICENSE_1_0.txt)
42 // GCC Note: based on version 1.32.0 of the Boost library.
44 /** @file tr1_impl/boost_sp_counted_base.h
45 * This is an internal header file, included by other library headers.
46 * You should not attempt to use it directly.
50 namespace std
52 _GLIBCXX_BEGIN_NAMESPACE_TR1
54 class bad_weak_ptr : public std::exception
56 public:
57 virtual char const*
58 what() const throw()
59 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
60 { return "std::bad_weak_ptr"; }
61 #else
62 { return "tr1::bad_weak_ptr"; }
63 #endif
66 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
67 inline void
68 __throw_bad_weak_ptr()
70 #if __EXCEPTIONS
71 throw bad_weak_ptr();
72 #else
73 __builtin_abort();
74 #endif
77 using __gnu_cxx::_Lock_policy;
78 using __gnu_cxx::__default_lock_policy;
79 using __gnu_cxx::_S_single;
80 using __gnu_cxx::_S_mutex;
81 using __gnu_cxx::_S_atomic;
83 // Empty helper class except when the template argument is _S_mutex.
84 template<_Lock_policy _Lp>
85 class _Mutex_base
87 protected:
88 // The atomic policy uses fully-fenced builtins, single doesn't care.
89 enum { _S_need_barriers = 0 };
92 template<>
93 class _Mutex_base<_S_mutex>
94 : public __gnu_cxx::__mutex
96 protected:
97 // This policy is used when atomic builtins are not available.
98 // The replacement atomic operations might not have the necessary
99 // memory barriers.
100 enum { _S_need_barriers = 1 };
103 template<_Lock_policy _Lp = __default_lock_policy>
104 class _Sp_counted_base
105 : public _Mutex_base<_Lp>
107 public:
108 _Sp_counted_base()
109 : _M_use_count(1), _M_weak_count(1) { }
111 virtual
112 ~_Sp_counted_base() // nothrow
115 // Called when _M_use_count drops to zero, to release the resources
116 // managed by *this.
117 virtual void
118 _M_dispose() = 0; // nothrow
120 // Called when _M_weak_count drops to zero.
121 virtual void
122 _M_destroy() // nothrow
123 { delete this; }
125 virtual void*
126 _M_get_deleter(const std::type_info&) = 0;
128 void
129 _M_add_ref_copy()
130 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
132 void
133 _M_add_ref_lock();
135 void
136 _M_release() // nothrow
138 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
140 _M_dispose();
141 // There must be a memory barrier between dispose() and destroy()
142 // to ensure that the effects of dispose() are observed in the
143 // thread that runs destroy().
144 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
145 if (_Mutex_base<_Lp>::_S_need_barriers)
147 _GLIBCXX_READ_MEM_BARRIER;
148 _GLIBCXX_WRITE_MEM_BARRIER;
151 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
152 -1) == 1)
153 _M_destroy();
157 void
158 _M_weak_add_ref() // nothrow
159 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
161 void
162 _M_weak_release() // nothrow
164 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
166 if (_Mutex_base<_Lp>::_S_need_barriers)
168 // See _M_release(),
169 // destroy() must observe results of dispose()
170 _GLIBCXX_READ_MEM_BARRIER;
171 _GLIBCXX_WRITE_MEM_BARRIER;
173 _M_destroy();
177 long
178 _M_get_use_count() const // nothrow
180 // No memory barrier is used here so there is no synchronization
181 // with other threads.
182 return const_cast<const volatile _Atomic_word&>(_M_use_count);
185 private:
186 _Sp_counted_base(_Sp_counted_base const&);
187 _Sp_counted_base& operator=(_Sp_counted_base const&);
189 _Atomic_word _M_use_count; // #shared
190 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
193 template<>
194 inline void
195 _Sp_counted_base<_S_single>::
196 _M_add_ref_lock()
198 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
200 _M_use_count = 0;
201 __throw_bad_weak_ptr();
205 template<>
206 inline void
207 _Sp_counted_base<_S_mutex>::
208 _M_add_ref_lock()
210 __gnu_cxx::__scoped_lock sentry(*this);
211 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
213 _M_use_count = 0;
214 __throw_bad_weak_ptr();
218 template<>
219 inline void
220 _Sp_counted_base<_S_atomic>::
221 _M_add_ref_lock()
223 // Perform lock-free add-if-not-zero operation.
224 _Atomic_word __count;
227 __count = _M_use_count;
228 if (__count == 0)
229 __throw_bad_weak_ptr();
231 // Replace the current counter value with the old value + 1, as
232 // long as it's not changed meanwhile.
234 while (!__sync_bool_compare_and_swap(&_M_use_count, __count,
235 __count + 1));
238 _GLIBCXX_END_NAMESPACE_TR1