]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/smart_ptr/detail/sp_counted_base_gcc_sparc.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / smart_ptr / detail / sp_counted_base_gcc_sparc.hpp
1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_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 // detail/sp_counted_base_gcc_sparc.hpp - g++ on Sparc V8+
11 //
12 // Copyright (c) 2006 Piotr Wyderski
13 // Copyright (c) 2006 Tomas Puverle
14 // Copyright (c) 2006 Peter Dimov
15 //
16 // Distributed under the Boost Software License, Version 1.0.
17 // See accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt
19 //
20 // Thanks to Michael van der Westhuizen
21
22 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
23 #include <boost/smart_ptr/detail/sp_obsolete.hpp>
24 #include <boost/config.hpp>
25 #include <inttypes.h> // int32_t
26
27 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
28
29 #include <boost/config/pragma_message.hpp>
30 BOOST_PRAGMA_MESSAGE("Using g++/Sparc sp_counted_base")
31
32 #endif
33
34 BOOST_SP_OBSOLETE()
35
36 namespace boost
37 {
38
39 namespace detail
40 {
41
42 inline int32_t compare_and_swap( int32_t * dest_, int32_t compare_, int32_t swap_ )
43 {
44 __asm__ __volatile__( "cas [%1], %2, %0"
45 : "+r" (swap_)
46 : "r" (dest_), "r" (compare_)
47 : "memory" );
48
49 return swap_;
50 }
51
52 inline int32_t atomic_fetch_and_add( int32_t * pw, int32_t dv )
53 {
54 // long r = *pw;
55 // *pw += dv;
56 // return r;
57
58 for( ;; )
59 {
60 int32_t r = *pw;
61
62 if( __builtin_expect((compare_and_swap(pw, r, r + dv) == r), 1) )
63 {
64 return r;
65 }
66 }
67 }
68
69 inline void atomic_increment( int32_t * pw )
70 {
71 atomic_fetch_and_add( pw, 1 );
72 }
73
74 inline int32_t atomic_decrement( int32_t * pw )
75 {
76 return atomic_fetch_and_add( pw, -1 );
77 }
78
79 inline int32_t atomic_conditional_increment( int32_t * pw )
80 {
81 // long r = *pw;
82 // if( r != 0 ) ++*pw;
83 // return r;
84
85 for( ;; )
86 {
87 int32_t r = *pw;
88
89 if( r == 0 )
90 {
91 return r;
92 }
93
94 if( __builtin_expect( ( compare_and_swap( pw, r, r + 1 ) == r ), 1 ) )
95 {
96 return r;
97 }
98 }
99 }
100
101 class BOOST_SYMBOL_VISIBLE sp_counted_base
102 {
103 private:
104
105 sp_counted_base( sp_counted_base const & );
106 sp_counted_base & operator= ( sp_counted_base const & );
107
108 int32_t use_count_; // #shared
109 int32_t weak_count_; // #weak + (#shared != 0)
110
111 public:
112
113 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
114 {
115 }
116
117 virtual ~sp_counted_base() // nothrow
118 {
119 }
120
121 // dispose() is called when use_count_ drops to zero, to release
122 // the resources managed by *this.
123
124 virtual void dispose() = 0; // nothrow
125
126 // destroy() is called when weak_count_ drops to zero.
127
128 virtual void destroy() // nothrow
129 {
130 delete this;
131 }
132
133 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
134 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
135 virtual void * get_untyped_deleter() = 0;
136
137 void add_ref_copy()
138 {
139 atomic_increment( &use_count_ );
140 }
141
142 bool add_ref_lock() // true on success
143 {
144 return atomic_conditional_increment( &use_count_ ) != 0;
145 }
146
147 void release() // nothrow
148 {
149 if( atomic_decrement( &use_count_ ) == 1 )
150 {
151 dispose();
152 weak_release();
153 }
154 }
155
156 void weak_add_ref() // nothrow
157 {
158 atomic_increment( &weak_count_ );
159 }
160
161 void weak_release() // nothrow
162 {
163 if( atomic_decrement( &weak_count_ ) == 1 )
164 {
165 destroy();
166 }
167 }
168
169 long use_count() const // nothrow
170 {
171 return const_cast< int32_t const volatile & >( use_count_ );
172 }
173 };
174
175 } // namespace detail
176
177 } // namespace boost
178
179 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED