1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_PPC_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_PPC_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_ppc.hpp - g++ on PowerPC
12 //
13 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
14 // Copyright 2004-2005 Peter Dimov
15 //
16 // Distributed under the Boost Software License, Version 1.0. (See
17 // accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
19 //
20 //
21 // Lock-free algorithm by Alexander Terekhov
22 //
23 // Thanks to Ben Hitchings for the #weak + (#shared != 0)
24 // formulation
25 //
26
27 #include <boost/detail/sp_typeinfo.hpp>
28 #include <boost/config.hpp>
29
30 namespace boost
31 {
32
33 namespace detail
34 {
35
atomic_increment(int * pw)36 inline void atomic_increment( int * pw )
37 {
38 // ++*pw;
39
40 int tmp;
41
42 __asm__
43 (
44 "0:\n\t"
45 "lwarx %1, 0, %2\n\t"
46 "addi %1, %1, 1\n\t"
47 "stwcx. %1, 0, %2\n\t"
48 "bne- 0b":
49
50 "=m"( *pw ), "=&b"( tmp ):
51 "r"( pw ), "m"( *pw ):
52 "cc"
53 );
54 }
55
atomic_decrement(int * pw)56 inline int atomic_decrement( int * pw )
57 {
58 // return --*pw;
59
60 int rv;
61
62 __asm__ __volatile__
63 (
64 "sync\n\t"
65 "0:\n\t"
66 "lwarx %1, 0, %2\n\t"
67 "addi %1, %1, -1\n\t"
68 "stwcx. %1, 0, %2\n\t"
69 "bne- 0b\n\t"
70 "isync":
71
72 "=m"( *pw ), "=&b"( rv ):
73 "r"( pw ), "m"( *pw ):
74 "memory", "cc"
75 );
76
77 return rv;
78 }
79
atomic_conditional_increment(int * pw)80 inline int atomic_conditional_increment( int * pw )
81 {
82 // if( *pw != 0 ) ++*pw;
83 // return *pw;
84
85 int rv;
86
87 __asm__
88 (
89 "0:\n\t"
90 "lwarx %1, 0, %2\n\t"
91 "cmpwi %1, 0\n\t"
92 "beq 1f\n\t"
93 "addi %1, %1, 1\n\t"
94 "1:\n\t"
95 "stwcx. %1, 0, %2\n\t"
96 "bne- 0b":
97
98 "=m"( *pw ), "=&b"( rv ):
99 "r"( pw ), "m"( *pw ):
100 "cc"
101 );
102
103 return rv;
104 }
105
106 class BOOST_SYMBOL_VISIBLE sp_counted_base
107 {
108 private:
109
110 sp_counted_base( sp_counted_base const & );
111 sp_counted_base & operator= ( sp_counted_base const & );
112
113 int use_count_; // #shared
114 int weak_count_; // #weak + (#shared != 0)
115
116 public:
117
sp_counted_base()118 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
119 {
120 }
121
~sp_counted_base()122 virtual ~sp_counted_base() // nothrow
123 {
124 }
125
126 // dispose() is called when use_count_ drops to zero, to release
127 // the resources managed by *this.
128
129 virtual void dispose() = 0; // nothrow
130
131 // destroy() is called when weak_count_ drops to zero.
132
destroy()133 virtual void destroy() // nothrow
134 {
135 delete this;
136 }
137
138 virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
139 virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
140 virtual void * get_untyped_deleter() = 0;
141
add_ref_copy()142 void add_ref_copy()
143 {
144 atomic_increment( &use_count_ );
145 }
146
add_ref_lock()147 bool add_ref_lock() // true on success
148 {
149 return atomic_conditional_increment( &use_count_ ) != 0;
150 }
151
release()152 void release() // nothrow
153 {
154 if( atomic_decrement( &use_count_ ) == 0 )
155 {
156 dispose();
157 weak_release();
158 }
159 }
160
weak_add_ref()161 void weak_add_ref() // nothrow
162 {
163 atomic_increment( &weak_count_ );
164 }
165
weak_release()166 void weak_release() // nothrow
167 {
168 if( atomic_decrement( &weak_count_ ) == 0 )
169 {
170 destroy();
171 }
172 }
173
use_count() const174 long use_count() const // nothrow
175 {
176 return static_cast<int const volatile &>( use_count_ );
177 }
178 };
179
180 } // namespace detail
181
182 } // namespace boost
183
184 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_PPC_HPP_INCLUDED
185