]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/smart_ptr/detail/sp_counted_base_vacpp_ppc.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / smart_ptr / detail / sp_counted_base_vacpp_ppc.hpp
1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_VACPP_PPC_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_VACPP_PPC_HPP_INCLUDED
3
4 //
5 // detail/sp_counted_base_vacpp_ppc.hpp - xlC(vacpp) on POWER
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 // Copyright 2012 IBM Corp.
12 //
13 // Distributed under the Boost Software License, Version 1.0. (See
14 // accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
16 //
17 //
18 // Lock-free algorithm by Alexander Terekhov
19 //
20 // Thanks to Ben Hitchings for the #weak + (#shared != 0)
21 // formulation
22 //
23
24 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
25 #include <boost/smart_ptr/detail/sp_obsolete.hpp>
26 #include <boost/config.hpp>
27
28 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
29
30 #include <boost/config/pragma_message.hpp>
31 BOOST_PRAGMA_MESSAGE("Using xlC/PowerPC sp_counted_base")
32
33 #endif
34
35 BOOST_SP_OBSOLETE()
36
37 extern "builtin" void __lwsync(void);
38 extern "builtin" void __isync(void);
39 extern "builtin" int __fetch_and_add(volatile int* addr, int val);
40 extern "builtin" int __compare_and_swap(volatile int*, int*, int);
41
42 namespace boost
43 {
44
45 namespace detail
46 {
47
48 inline void atomic_increment( int *pw )
49 {
50 // ++*pw;
51 __lwsync();
52 __fetch_and_add(pw, 1);
53 __isync();
54 }
55
56 inline int atomic_decrement( int *pw )
57 {
58 // return --*pw;
59 __lwsync();
60 int originalValue = __fetch_and_add(pw, -1);
61 __isync();
62
63 return (originalValue - 1);
64 }
65
66 inline int atomic_conditional_increment( int *pw )
67 {
68 // if( *pw != 0 ) ++*pw;
69 // return *pw;
70
71 __lwsync();
72 int v = *const_cast<volatile int*>(pw);
73 for (;;)
74 // loop until state is known
75 {
76 if (v == 0) return 0;
77 if (__compare_and_swap(pw, &v, v + 1))
78 {
79 __isync(); return (v + 1);
80 }
81 }
82 }
83
84 class BOOST_SYMBOL_VISIBLE sp_counted_base
85 {
86 private:
87
88 sp_counted_base( sp_counted_base const & );
89 sp_counted_base & operator= ( sp_counted_base const & );
90
91 int use_count_; // #shared
92 int weak_count_; // #weak + (#shared != 0)
93 char pad[64] __attribute__((__aligned__(64)));
94 // pad to prevent false sharing
95 public:
96
97 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
98 {
99 }
100
101 virtual ~sp_counted_base() // nothrow
102 {
103 }
104
105 // dispose() is called when use_count_ drops to zero, to release
106 // the resources managed by *this.
107
108 virtual void dispose() = 0; // nothrow
109
110 // destroy() is called when weak_count_ drops to zero.
111
112 virtual void destroy() // nothrow
113 {
114 delete this;
115 }
116
117 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
118 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
119 virtual void * get_untyped_deleter() = 0;
120
121 void add_ref_copy()
122 {
123 atomic_increment( &use_count_ );
124 }
125
126 bool add_ref_lock() // true on success
127 {
128 return atomic_conditional_increment( &use_count_ ) != 0;
129 }
130
131 void release() // nothrow
132 {
133 if( atomic_decrement( &use_count_ ) == 0 )
134 {
135 dispose();
136 weak_release();
137 }
138 }
139
140 void weak_add_ref() // nothrow
141 {
142 atomic_increment( &weak_count_ );
143 }
144
145 void weak_release() // nothrow
146 {
147 if( atomic_decrement( &weak_count_ ) == 0 )
148 {
149 destroy();
150 }
151 }
152
153 long use_count() const // nothrow
154 {
155 return *const_cast<volatile int*>(&use_count_);
156 }
157 };
158
159 } // namespace detail
160
161 } // namespace boost
162
163 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_VACPP_PPC_HPP_INCLUDED