Install gcc-4.4.0-tdm-1-core-2.tar.gz
[msysgit.git] / mingw / lib / gcc / mingw32 / 4.3.3 / include / c++ / tr1_impl / boost_sp_counted_base.h
blob3cb4175b376d33920f1b486458769f903cba3ad8
1 // <tr1_impl/boost_sp_counted_base.h> -*- C++ -*-
3 // Copyright (C) 2007 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // shared_count.hpp
31 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
33 // shared_ptr.hpp
34 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
37 // weak_ptr.hpp
38 // Copyright (C) 2001, 2002, 2003 Peter Dimov
40 // enable_shared_from_this.hpp
41 // Copyright (C) 2002 Peter Dimov
43 // Distributed under the Boost Software License, Version 1.0. (See
44 // accompanying file LICENSE_1_0.txt or copy at
45 // http://www.boost.org/LICENSE_1_0.txt)
47 // GCC Note: based on version 1.32.0 of the Boost library.
49 /** @file tr1_impl/boost_sp_counted_base.h
50 * This is an internal header file, included by other library headers.
51 * You should not attempt to use it directly.
55 namespace std
57 _GLIBCXX_BEGIN_NAMESPACE_TR1
59 class bad_weak_ptr : public std::exception
61 public:
62 virtual char const*
63 what() const throw()
64 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
65 { return "std::bad_weak_ptr"; }
66 #else
67 { return "tr1::bad_weak_ptr"; }
68 #endif
71 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
72 inline void
73 __throw_bad_weak_ptr()
75 #if __EXCEPTIONS
76 throw bad_weak_ptr();
77 #else
78 __builtin_abort();
79 #endif
82 using __gnu_cxx::_Lock_policy;
83 using __gnu_cxx::__default_lock_policy;
84 using __gnu_cxx::_S_single;
85 using __gnu_cxx::_S_mutex;
86 using __gnu_cxx::_S_atomic;
88 // Empty helper class except when the template argument is _S_mutex.
89 template<_Lock_policy _Lp>
90 class _Mutex_base
92 protected:
93 // The atomic policy uses fully-fenced builtins, single doesn't care.
94 enum { _S_need_barriers = 0 };
97 template<>
98 class _Mutex_base<_S_mutex>
99 : public __gnu_cxx::__mutex
101 protected:
102 // This policy is used when atomic builtins are not available.
103 // The replacement atomic operations might not have the necessary
104 // memory barriers.
105 enum { _S_need_barriers = 1 };
108 template<_Lock_policy _Lp = __default_lock_policy>
109 class _Sp_counted_base
110 : public _Mutex_base<_Lp>
112 public:
113 _Sp_counted_base()
114 : _M_use_count(1), _M_weak_count(1) { }
116 virtual
117 ~_Sp_counted_base() // nothrow
120 // Called when _M_use_count drops to zero, to release the resources
121 // managed by *this.
122 virtual void
123 _M_dispose() = 0; // nothrow
125 // Called when _M_weak_count drops to zero.
126 virtual void
127 _M_destroy() // nothrow
128 { delete this; }
130 virtual void*
131 _M_get_deleter(const std::type_info&) = 0;
133 void
134 _M_add_ref_copy()
135 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
137 void
138 _M_add_ref_lock();
140 void
141 _M_release() // nothrow
143 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
145 _M_dispose();
146 // There must be a memory barrier between dispose() and destroy()
147 // to ensure that the effects of dispose() are observed in the
148 // thread that runs destroy().
149 // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html
150 if (_Mutex_base<_Lp>::_S_need_barriers)
152 _GLIBCXX_READ_MEM_BARRIER;
153 _GLIBCXX_WRITE_MEM_BARRIER;
156 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
157 -1) == 1)
158 _M_destroy();
162 void
163 _M_weak_add_ref() // nothrow
164 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
166 void
167 _M_weak_release() // nothrow
169 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
171 if (_Mutex_base<_Lp>::_S_need_barriers)
173 // See _M_release(),
174 // destroy() must observe results of dispose()
175 _GLIBCXX_READ_MEM_BARRIER;
176 _GLIBCXX_WRITE_MEM_BARRIER;
178 _M_destroy();
182 long
183 _M_get_use_count() const // nothrow
185 // No memory barrier is used here so there is no synchronization
186 // with other threads.
187 return const_cast<const volatile _Atomic_word&>(_M_use_count);
190 private:
191 _Sp_counted_base(_Sp_counted_base const&);
192 _Sp_counted_base& operator=(_Sp_counted_base const&);
194 _Atomic_word _M_use_count; // #shared
195 _Atomic_word _M_weak_count; // #weak + (#shared != 0)
198 template<>
199 inline void
200 _Sp_counted_base<_S_single>::
201 _M_add_ref_lock()
203 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
205 _M_use_count = 0;
206 __throw_bad_weak_ptr();
210 template<>
211 inline void
212 _Sp_counted_base<_S_mutex>::
213 _M_add_ref_lock()
215 __gnu_cxx::__scoped_lock sentry(*this);
216 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
218 _M_use_count = 0;
219 __throw_bad_weak_ptr();
223 template<>
224 inline void
225 _Sp_counted_base<_S_atomic>::
226 _M_add_ref_lock()
228 // Perform lock-free add-if-not-zero operation.
229 _Atomic_word __count;
232 __count = _M_use_count;
233 if (__count == 0)
234 __throw_bad_weak_ptr();
236 // Replace the current counter value with the old value + 1, as
237 // long as it's not changed meanwhile.
239 while (!__sync_bool_compare_and_swap(&_M_use_count, __count,
240 __count + 1));
243 _GLIBCXX_END_NAMESPACE_TR1