]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/smart_ptr/detail/sp_counted_base_snc_ps3.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / smart_ptr / detail / sp_counted_base_snc_ps3.hpp
CommitLineData
7c673cae
FG
1#ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SNC_PS3_HPP_INCLUDED
2#define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SNC_PS3_HPP_INCLUDED
3
4// MS compatible compilers support #pragma once
5#if defined(_MSC_VER) && (_MSC_VER >= 1020)
6# pragma once
7#endif
8
9// detail/sp_counted_base_gcc_sparc.hpp - g++ on Sparc V8+
10//
11// Copyright (c) 2006 Piotr Wyderski
12// Copyright (c) 2006 Tomas Puverle
13// Copyright (c) 2006 Peter Dimov
14// Copyright (c) 2011 Emil Dotchevski
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
92f5a8d4
TL
22#include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
23#include <boost/config.hpp>
7c673cae
FG
24#include <inttypes.h> // uint32_t
25
26namespace boost
27{
28
29namespace detail
30{
31
32inline uint32_t compare_and_swap( uint32_t * dest_, uint32_t compare_, uint32_t swap_ )
33{
34 return __builtin_cellAtomicCompareAndSwap32(dest_,compare_,swap_);
35}
36
37inline uint32_t atomic_fetch_and_add( uint32_t * pw, uint32_t dv )
38{
39 // long r = *pw;
40 // *pw += dv;
41 // return r;
42
43 for( ;; )
44 {
45 uint32_t r = *pw;
46
47 if( __builtin_expect((compare_and_swap(pw, r, r + dv) == r), 1) )
48 {
49 return r;
50 }
51 }
52}
53
54inline void atomic_increment( uint32_t * pw )
55{
56 (void) __builtin_cellAtomicIncr32( pw );
57}
58
59inline uint32_t atomic_decrement( uint32_t * pw )
60{
61 return __builtin_cellAtomicDecr32( pw );
62}
63
64inline uint32_t atomic_conditional_increment( uint32_t * pw )
65{
66 // long r = *pw;
67 // if( r != 0 ) ++*pw;
68 // return r;
69
70 for( ;; )
71 {
72 uint32_t r = *pw;
73
74 if( r == 0 )
75 {
76 return r;
77 }
78
79 if( __builtin_expect( ( compare_and_swap( pw, r, r + 1 ) == r ), 1 ) )
80 {
81 return r;
82 }
83 }
84}
85
92f5a8d4 86class BOOST_SYMBOL_VISIBLE sp_counted_base
7c673cae
FG
87{
88private:
89
90 sp_counted_base( sp_counted_base const & );
91 sp_counted_base & operator= ( sp_counted_base const & );
92
93 uint32_t use_count_; // #shared
94 uint32_t weak_count_; // #weak + (#shared != 0)
95
96public:
97
98 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
99 {
100 }
101
102 virtual ~sp_counted_base() // nothrow
103 {
104 }
105
106 // dispose() is called when use_count_ drops to zero, to release
107 // the resources managed by *this.
108
109 virtual void dispose() = 0; // nothrow
110
111 // destroy() is called when weak_count_ drops to zero.
112
113 virtual void destroy() // nothrow
114 {
115 delete this;
116 }
117
92f5a8d4
TL
118 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
119 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
7c673cae
FG
120 virtual void * get_untyped_deleter() = 0;
121
122 void add_ref_copy()
123 {
124 atomic_increment( &use_count_ );
125 }
126
127 bool add_ref_lock() // true on success
128 {
129 return atomic_conditional_increment( &use_count_ ) != 0;
130 }
131
132 void release() // nothrow
133 {
134 if( atomic_decrement( &use_count_ ) == 1 )
135 {
136 dispose();
137 weak_release();
138 }
139 }
140
141 void weak_add_ref() // nothrow
142 {
143 atomic_increment( &weak_count_ );
144 }
145
146 void weak_release() // nothrow
147 {
148 if( atomic_decrement( &weak_count_ ) == 1 )
149 {
150 destroy();
151 }
152 }
153
154 long use_count() const // nothrow
155 {
156 return const_cast< uint32_t const volatile & >( use_count_ );
157 }
158};
159
160} // namespace detail
161
162} // namespace boost
163
164#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SNC_PS3_HPP_INCLUDED