]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/smart_ptr/detail/sp_counted_base_gcc_x86.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / smart_ptr / detail / sp_counted_base_gcc_x86.hpp
1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_X86_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_X86_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_gcc_x86.hpp - g++ on 486+ or AMD64
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_typeinfo_.hpp>
28 #include <boost/config.hpp>
29
30 namespace boost
31 {
32
33 namespace detail
34 {
35
36 inline int atomic_exchange_and_add( int * pw, int dv )
37 {
38 // int r = *pw;
39 // *pw += dv;
40 // return r;
41
42 int r;
43
44 __asm__ __volatile__
45 (
46 "lock\n\t"
47 "xadd %1, %0":
48 "=m"( *pw ), "=r"( r ): // outputs (%0, %1)
49 "m"( *pw ), "1"( dv ): // inputs (%2, %3 == %1)
50 "memory", "cc" // clobbers
51 );
52
53 return r;
54 }
55
56 inline void atomic_increment( int * pw )
57 {
58 //atomic_exchange_and_add( pw, 1 );
59
60 __asm__
61 (
62 "lock\n\t"
63 "incl %0":
64 "=m"( *pw ): // output (%0)
65 "m"( *pw ): // input (%1)
66 "cc" // clobbers
67 );
68 }
69
70 inline int atomic_conditional_increment( int * pw )
71 {
72 // int rv = *pw;
73 // if( rv != 0 ) ++*pw;
74 // return rv;
75
76 int rv, tmp;
77
78 __asm__
79 (
80 "movl %0, %%eax\n\t"
81 "0:\n\t"
82 "test %%eax, %%eax\n\t"
83 "je 1f\n\t"
84 "movl %%eax, %2\n\t"
85 "incl %2\n\t"
86 "lock\n\t"
87 "cmpxchgl %2, %0\n\t"
88 "jne 0b\n\t"
89 "1:":
90 "=m"( *pw ), "=&a"( rv ), "=&r"( tmp ): // outputs (%0, %1, %2)
91 "m"( *pw ): // input (%3)
92 "cc" // clobbers
93 );
94
95 return rv;
96 }
97
98 class BOOST_SYMBOL_VISIBLE sp_counted_base
99 {
100 private:
101
102 sp_counted_base( sp_counted_base const & );
103 sp_counted_base & operator= ( sp_counted_base const & );
104
105 int use_count_; // #shared
106 int weak_count_; // #weak + (#shared != 0)
107
108 public:
109
110 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
111 {
112 }
113
114 virtual ~sp_counted_base() // nothrow
115 {
116 }
117
118 // dispose() is called when use_count_ drops to zero, to release
119 // the resources managed by *this.
120
121 virtual void dispose() = 0; // nothrow
122
123 // destroy() is called when weak_count_ drops to zero.
124
125 virtual void destroy() // nothrow
126 {
127 delete this;
128 }
129
130 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
131 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
132 virtual void * get_untyped_deleter() = 0;
133
134 void add_ref_copy()
135 {
136 atomic_increment( &use_count_ );
137 }
138
139 bool add_ref_lock() // true on success
140 {
141 return atomic_conditional_increment( &use_count_ ) != 0;
142 }
143
144 void release() // nothrow
145 {
146 if( atomic_exchange_and_add( &use_count_, -1 ) == 1 )
147 {
148 dispose();
149 weak_release();
150 }
151 }
152
153 void weak_add_ref() // nothrow
154 {
155 atomic_increment( &weak_count_ );
156 }
157
158 void weak_release() // nothrow
159 {
160 if( atomic_exchange_and_add( &weak_count_, -1 ) == 1 )
161 {
162 destroy();
163 }
164 }
165
166 long use_count() const // nothrow
167 {
168 return static_cast<int const volatile &>( use_count_ );
169 }
170 };
171
172 } // namespace detail
173
174 } // namespace boost
175
176 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_X86_HPP_INCLUDED