]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/smart_ptr/detail/sp_counted_base_w32.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / smart_ptr / detail / sp_counted_base_w32.hpp
1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
3
4 // MS compatible compilers support #pragma once
5
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
9
10 //
11 // detail/sp_counted_base_w32.hpp
12 //
13 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
14 // Copyright 2004-2005 Peter Dimov
15 //
16 // Distributed under the Boost Software License, Version 1.0. (See
17 // accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
19 //
20 //
21 // Lock-free algorithm by Alexander Terekhov
22 //
23 // Thanks to Ben Hitchings for the #weak + (#shared != 0)
24 // formulation
25 //
26
27 #include <boost/smart_ptr/detail/sp_interlocked.hpp>
28 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
29 #include <boost/detail/workaround.hpp>
30 #include <boost/config.hpp>
31
32 namespace boost
33 {
34
35 namespace detail
36 {
37
38 class BOOST_SYMBOL_VISIBLE sp_counted_base
39 {
40 private:
41
42 sp_counted_base( sp_counted_base const & );
43 sp_counted_base & operator= ( sp_counted_base const & );
44
45 long use_count_; // #shared
46 long weak_count_; // #weak + (#shared != 0)
47
48 public:
49
50 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
51 {
52 }
53
54 virtual ~sp_counted_base() // nothrow
55 {
56 }
57
58 // dispose() is called when use_count_ drops to zero, to release
59 // the resources managed by *this.
60
61 virtual void dispose() = 0; // nothrow
62
63 // destroy() is called when weak_count_ drops to zero.
64
65 virtual void destroy() // nothrow
66 {
67 delete this;
68 }
69
70 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
71 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
72 virtual void * get_untyped_deleter() = 0;
73
74 void add_ref_copy()
75 {
76 BOOST_SP_INTERLOCKED_INCREMENT( &use_count_ );
77 }
78
79 bool add_ref_lock() // true on success
80 {
81 for( ;; )
82 {
83 long tmp = static_cast< long const volatile& >( use_count_ );
84 if( tmp == 0 ) return false;
85
86 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1200 )
87
88 // work around a code generation bug
89
90 long tmp2 = tmp + 1;
91 if( BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp2, tmp ) == tmp2 - 1 ) return true;
92
93 #else
94
95 if( BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp + 1, tmp ) == tmp ) return true;
96
97 #endif
98 }
99 }
100
101 void release() // nothrow
102 {
103 if( BOOST_SP_INTERLOCKED_DECREMENT( &use_count_ ) == 0 )
104 {
105 dispose();
106 weak_release();
107 }
108 }
109
110 void weak_add_ref() // nothrow
111 {
112 BOOST_SP_INTERLOCKED_INCREMENT( &weak_count_ );
113 }
114
115 void weak_release() // nothrow
116 {
117 if( BOOST_SP_INTERLOCKED_DECREMENT( &weak_count_ ) == 0 )
118 {
119 destroy();
120 }
121 }
122
123 long use_count() const // nothrow
124 {
125 return static_cast<long const volatile &>( use_count_ );
126 }
127 };
128
129 } // namespace detail
130
131 } // namespace boost
132
133 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED