]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/smart_ptr/detail/sp_counted_base_gcc_mips.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / smart_ptr / detail / sp_counted_base_gcc_mips.hpp
1 #ifndef BOOST_DETAIL_SP_COUNTED_BASE_GCC_MIPS_HPP_INCLUDED
2 #define BOOST_DETAIL_SP_COUNTED_BASE_GCC_MIPS_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_mips.hpp - g++ on MIPS
12 //
13 // Copyright (c) 2009, Spirent Communications, Inc.
14 //
15 // Distributed under the Boost Software License, Version 1.0. (See
16 // accompanying file LICENSE_1_0.txt or copy at
17 // http://www.boost.org/LICENSE_1_0.txt)
18 //
19 //
20 // Lock-free algorithm by Alexander Terekhov
21 //
22
23 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
24 #include <boost/smart_ptr/detail/sp_obsolete.hpp>
25 #include <boost/config.hpp>
26
27 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
28
29 #include <boost/config/pragma_message.hpp>
30 BOOST_PRAGMA_MESSAGE("Using g++/MIPS sp_counted_base")
31
32 #endif
33
34 BOOST_SP_OBSOLETE()
35
36 namespace boost
37 {
38
39 namespace detail
40 {
41
42 inline void atomic_increment( int * pw )
43 {
44 // ++*pw;
45
46 int tmp;
47
48 __asm__ __volatile__
49 (
50 "0:\n\t"
51 ".set push\n\t"
52 #if !defined(__mips_isa_rev) || (__mips_isa_rev < 6)
53 ".set mips2\n\t"
54 #endif
55 "ll %0, %1\n\t"
56 "addiu %0, 1\n\t"
57 "sc %0, %1\n\t"
58 ".set pop\n\t"
59 "beqz %0, 0b":
60 "=&r"( tmp ), "=m"( *pw ):
61 "m"( *pw )
62 );
63 }
64
65 inline int atomic_decrement( int * pw )
66 {
67 // return --*pw;
68
69 int rv, tmp;
70
71 __asm__ __volatile__
72 (
73 "0:\n\t"
74 ".set push\n\t"
75 #if !defined(__mips_isa_rev) || (__mips_isa_rev < 6)
76 ".set mips2\n\t"
77 #endif
78 "ll %1, %2\n\t"
79 "addiu %0, %1, -1\n\t"
80 "sc %0, %2\n\t"
81 ".set pop\n\t"
82 "beqz %0, 0b\n\t"
83 "addiu %0, %1, -1":
84 "=&r"( rv ), "=&r"( tmp ), "=m"( *pw ):
85 "m"( *pw ):
86 "memory"
87 );
88
89 return rv;
90 }
91
92 inline int atomic_conditional_increment( int * pw )
93 {
94 // if( *pw != 0 ) ++*pw;
95 // return *pw;
96
97 int rv, tmp;
98
99 __asm__ __volatile__
100 (
101 "0:\n\t"
102 ".set push\n\t"
103 #if !defined(__mips_isa_rev) || (__mips_isa_rev < 6)
104 ".set mips2\n\t"
105 #endif
106 "ll %0, %2\n\t"
107 "beqz %0, 1f\n\t"
108 "addiu %1, %0, 1\n\t"
109 "sc %1, %2\n\t"
110 ".set pop\n\t"
111 "beqz %1, 0b\n\t"
112 "addiu %0, %0, 1\n\t"
113 "1:":
114 "=&r"( rv ), "=&r"( tmp ), "=m"( *pw ):
115 "m"( *pw ):
116 "memory"
117 );
118
119 return rv;
120 }
121
122 class BOOST_SYMBOL_VISIBLE sp_counted_base
123 {
124 private:
125
126 sp_counted_base( sp_counted_base const & );
127 sp_counted_base & operator= ( sp_counted_base const & );
128
129 int use_count_; // #shared
130 int weak_count_; // #weak + (#shared != 0)
131
132 public:
133
134 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
135 {
136 }
137
138 virtual ~sp_counted_base() // nothrow
139 {
140 }
141
142 // dispose() is called when use_count_ drops to zero, to release
143 // the resources managed by *this.
144
145 virtual void dispose() = 0; // nothrow
146
147 // destroy() is called when weak_count_ drops to zero.
148
149 virtual void destroy() // nothrow
150 {
151 delete this;
152 }
153
154 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
155 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
156 virtual void * get_untyped_deleter() = 0;
157
158 void add_ref_copy()
159 {
160 atomic_increment( &use_count_ );
161 }
162
163 bool add_ref_lock() // true on success
164 {
165 return atomic_conditional_increment( &use_count_ ) != 0;
166 }
167
168 void release() // nothrow
169 {
170 if( atomic_decrement( &use_count_ ) == 0 )
171 {
172 dispose();
173 weak_release();
174 }
175 }
176
177 void weak_add_ref() // nothrow
178 {
179 atomic_increment( &weak_count_ );
180 }
181
182 void weak_release() // nothrow
183 {
184 if( atomic_decrement( &weak_count_ ) == 0 )
185 {
186 destroy();
187 }
188 }
189
190 long use_count() const // nothrow
191 {
192 return static_cast<int const volatile &>( use_count_ );
193 }
194 };
195
196 } // namespace detail
197
198 } // namespace boost
199
200 #endif // #ifndef BOOST_DETAIL_SP_COUNTED_BASE_GCC_MIPS_HPP_INCLUDED