]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/smart_ptr/include/boost/smart_ptr/detail/sp_counted_base_solaris.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / smart_ptr / include / boost / smart_ptr / detail / sp_counted_base_solaris.hpp
1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SOLARIS_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SOLARIS_HPP_INCLUDED
3
4 //
5 // detail/sp_counted_base_solaris.hpp
6 // based on: detail/sp_counted_base_w32.hpp
7 //
8 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
9 // Copyright 2004-2005 Peter Dimov
10 // Copyright 2006 Michael van der Westhuizen
11 //
12 // Distributed under the Boost Software License, Version 1.0. (See
13 // accompanying file LICENSE_1_0.txt or copy at
14 // http://www.boost.org/LICENSE_1_0.txt)
15 //
16 //
17 // Lock-free algorithm by Alexander Terekhov
18 //
19 // Thanks to Ben Hitchings for the #weak + (#shared != 0)
20 // formulation
21 //
22
23 #include <boost/detail/sp_typeinfo.hpp>
24 #include <atomic.h>
25
26 namespace boost
27 {
28
29 namespace detail
30 {
31
32 class sp_counted_base
33 {
34 private:
35
36 sp_counted_base( sp_counted_base const & );
37 sp_counted_base & operator= ( sp_counted_base const & );
38
39 uint32_t use_count_; // #shared
40 uint32_t weak_count_; // #weak + (#shared != 0)
41
42 public:
43
44 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
45 {
46 }
47
48 virtual ~sp_counted_base() // nothrow
49 {
50 }
51
52 // dispose() is called when use_count_ drops to zero, to release
53 // the resources managed by *this.
54
55 virtual void dispose() = 0; // nothrow
56
57 // destroy() is called when weak_count_ drops to zero.
58
59 virtual void destroy() // nothrow
60 {
61 delete this;
62 }
63
64 virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
65 virtual void * get_untyped_deleter() = 0;
66
67 void add_ref_copy()
68 {
69 atomic_inc_32( &use_count_ );
70 }
71
72 bool add_ref_lock() // true on success
73 {
74 for( ;; )
75 {
76 uint32_t tmp = static_cast< uint32_t const volatile& >( use_count_ );
77 if( tmp == 0 ) return false;
78 if( atomic_cas_32( &use_count_, tmp, tmp + 1 ) == tmp ) return true;
79 }
80 }
81
82 void release() // nothrow
83 {
84 if( atomic_dec_32_nv( &use_count_ ) == 0 )
85 {
86 dispose();
87 weak_release();
88 }
89 }
90
91 void weak_add_ref() // nothrow
92 {
93 atomic_inc_32( &weak_count_ );
94 }
95
96 void weak_release() // nothrow
97 {
98 if( atomic_dec_32_nv( &weak_count_ ) == 0 )
99 {
100 destroy();
101 }
102 }
103
104 long use_count() const // nothrow
105 {
106 return static_cast<long const volatile &>( use_count_ );
107 }
108 };
109
110 } // namespace detail
111
112 } // namespace boost
113
114 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SOLARIS_HPP_INCLUDED