1 // <tr1_impl/boost_sp_counted_base.h> -*- C++ -*-
3 // Copyright (C) 2007 Free Software Foundation, Inc.
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)
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,
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.
31 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
34 // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
35 // Copyright (C) 2001, 2002, 2003 Peter Dimov
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.
57 _GLIBCXX_BEGIN_NAMESPACE_TR1
59 class bad_weak_ptr
: public std::exception
64 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
65 { return "std::bad_weak_ptr"; }
67 { return "tr1::bad_weak_ptr"; }
71 // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
73 __throw_bad_weak_ptr()
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
>
93 // The atomic policy uses fully-fenced builtins, single doesn't care.
94 enum { _S_need_barriers
= 0 };
98 class _Mutex_base
<_S_mutex
>
99 : public __gnu_cxx::__mutex
102 // This policy is used when atomic builtins are not available.
103 // The replacement atomic operations might not have the necessary
105 enum { _S_need_barriers
= 1 };
108 template<_Lock_policy _Lp
= __default_lock_policy
>
109 class _Sp_counted_base
110 : public _Mutex_base
<_Lp
>
114 : _M_use_count(1), _M_weak_count(1) { }
117 ~_Sp_counted_base() // nothrow
120 // Called when _M_use_count drops to zero, to release the resources
123 _M_dispose() = 0; // nothrow
125 // Called when _M_weak_count drops to zero.
127 _M_destroy() // nothrow
131 _M_get_deleter(const std::type_info
&) = 0;
135 { __gnu_cxx::__atomic_add_dispatch(&_M_use_count
, 1); }
141 _M_release() // nothrow
143 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, -1) == 1)
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
,
163 _M_weak_add_ref() // nothrow
164 { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count
, 1); }
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
)
174 // destroy() must observe results of dispose()
175 _GLIBCXX_READ_MEM_BARRIER
;
176 _GLIBCXX_WRITE_MEM_BARRIER
;
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
);
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)
200 _Sp_counted_base
<_S_single
>::
203 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
206 __throw_bad_weak_ptr();
212 _Sp_counted_base
<_S_mutex
>::
215 __gnu_cxx::__scoped_lock
sentry(*this);
216 if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count
, 1) == 0)
219 __throw_bad_weak_ptr();
225 _Sp_counted_base
<_S_atomic
>::
228 // Perform lock-free add-if-not-zero operation.
229 _Atomic_word __count
;
232 __count
= _M_use_count
;
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
,
243 _GLIBCXX_END_NAMESPACE_TR1