]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.3.3/include/tr1_impl/boost_sp_counted_base.h
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.3.3 / include / tr1_impl / boost_sp_counted_base.h
1 // <tr1_impl/boost_sp_counted_base.h> -*- C++ -*-
2
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.
10
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.
15
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.
20
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.
29
30 //  shared_count.hpp
31 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
32
33 //  shared_ptr.hpp
34 //  Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes.
35 //  Copyright (C) 2001, 2002, 2003 Peter Dimov
36
37 //  weak_ptr.hpp
38 //  Copyright (C) 2001, 2002, 2003 Peter Dimov
39
40 //  enable_shared_from_this.hpp
41 //  Copyright (C) 2002 Peter Dimov
42
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)
46
47 // GCC Note:  based on version 1.32.0 of the Boost library.
48
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.
52  */
53
54
55 namespace std
56 {
57 _GLIBCXX_BEGIN_NAMESPACE_TR1
58
59   class bad_weak_ptr : public std::exception
60   {
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
69   };
70
71   // Substitute for bad_weak_ptr object in the case of -fno-exceptions.
72   inline void
73   __throw_bad_weak_ptr()
74   {
75 #if __EXCEPTIONS
76     throw bad_weak_ptr();
77 #else
78     __builtin_abort();
79 #endif
80   }
81
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;
87
88   // Empty helper class except when the template argument is _S_mutex.
89   template<_Lock_policy _Lp>
90     class _Mutex_base
91     {
92     protected:
93       // The atomic policy uses fully-fenced builtins, single doesn't care.
94       enum { _S_need_barriers = 0 };
95     };
96
97   template<>
98     class _Mutex_base<_S_mutex>
99     : public __gnu_cxx::__mutex
100     {
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 };
106     };
107
108   template<_Lock_policy _Lp = __default_lock_policy>
109     class _Sp_counted_base
110     : public _Mutex_base<_Lp>
111     {
112     public:  
113       _Sp_counted_base()
114       : _M_use_count(1), _M_weak_count(1) { }
115       
116       virtual
117       ~_Sp_counted_base() // nothrow 
118       { }
119   
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
124       
125       // Called when _M_weak_count drops to zero.
126       virtual void
127       _M_destroy() // nothrow
128       { delete this; }
129       
130       virtual void*
131       _M_get_deleter(const std::type_info&) = 0;
132
133       void
134       _M_add_ref_copy()
135       { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); }
136   
137       void
138       _M_add_ref_lock();
139       
140       void
141       _M_release() // nothrow
142       {
143         if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1)
144           {
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)
151               {
152                 _GLIBCXX_READ_MEM_BARRIER;
153                 _GLIBCXX_WRITE_MEM_BARRIER;
154               }
155
156             if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count,
157                                                        -1) == 1)
158               _M_destroy();
159           }
160       }
161   
162       void
163       _M_weak_add_ref() // nothrow
164       { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); }
165
166       void
167       _M_weak_release() // nothrow
168       {
169         if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1)
170           {
171             if (_Mutex_base<_Lp>::_S_need_barriers)
172               {
173                 // See _M_release(),
174                 // destroy() must observe results of dispose()
175                 _GLIBCXX_READ_MEM_BARRIER;
176                 _GLIBCXX_WRITE_MEM_BARRIER;
177               }
178             _M_destroy();
179           }
180       }
181   
182       long
183       _M_get_use_count() const // nothrow
184       {
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);
188       }
189
190     private:  
191       _Sp_counted_base(_Sp_counted_base const&);
192       _Sp_counted_base& operator=(_Sp_counted_base const&);
193
194       _Atomic_word  _M_use_count;     // #shared
195       _Atomic_word  _M_weak_count;    // #weak + (#shared != 0)
196     };
197
198   template<>
199     inline void
200     _Sp_counted_base<_S_single>::
201     _M_add_ref_lock()
202     {
203       if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
204         {
205           _M_use_count = 0;
206           __throw_bad_weak_ptr();
207         }
208     }
209
210   template<>
211     inline void
212     _Sp_counted_base<_S_mutex>::
213     _M_add_ref_lock()
214     {
215       __gnu_cxx::__scoped_lock sentry(*this);
216       if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0)
217         {
218           _M_use_count = 0;
219           __throw_bad_weak_ptr();
220         }
221     }
222
223   template<> 
224     inline void
225     _Sp_counted_base<_S_atomic>::
226     _M_add_ref_lock()
227     {
228       // Perform lock-free add-if-not-zero operation.
229       _Atomic_word __count;
230       do
231         {
232           __count = _M_use_count;
233           if (__count == 0)
234             __throw_bad_weak_ptr();
235           
236           // Replace the current counter value with the old value + 1, as
237           // long as it's not changed meanwhile. 
238         }
239       while (!__sync_bool_compare_and_swap(&_M_use_count, __count,
240                                            __count + 1));
241     }
242
243 _GLIBCXX_END_NAMESPACE_TR1
244 }