]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CW_PPC_HPP_INCLUDED |
2 | #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CW_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_cw_ppc.hpp - CodeWarrior 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 | ||
92f5a8d4 TL |
27 | #include <boost/smart_ptr/detail/sp_typeinfo_.hpp> |
28 | #include <boost/config.hpp> | |
7c673cae FG |
29 | |
30 | namespace boost | |
31 | { | |
32 | ||
33 | namespace detail | |
34 | { | |
35 | ||
36 | inline void atomic_increment( register long * pw ) | |
37 | { | |
38 | register int a; | |
39 | ||
40 | asm | |
41 | { | |
42 | loop: | |
43 | ||
44 | lwarx a, 0, pw | |
45 | addi a, a, 1 | |
46 | stwcx. a, 0, pw | |
47 | bne- loop | |
48 | } | |
49 | } | |
50 | ||
51 | inline long atomic_decrement( register long * pw ) | |
52 | { | |
53 | register int a; | |
54 | ||
55 | asm | |
56 | { | |
57 | sync | |
58 | ||
59 | loop: | |
60 | ||
61 | lwarx a, 0, pw | |
62 | addi a, a, -1 | |
63 | stwcx. a, 0, pw | |
64 | bne- loop | |
65 | ||
66 | isync | |
67 | } | |
68 | ||
69 | return a; | |
70 | } | |
71 | ||
72 | inline long atomic_conditional_increment( register long * pw ) | |
73 | { | |
74 | register int a; | |
75 | ||
76 | asm | |
77 | { | |
78 | loop: | |
79 | ||
80 | lwarx a, 0, pw | |
81 | cmpwi a, 0 | |
82 | beq store | |
83 | ||
84 | addi a, a, 1 | |
85 | ||
86 | store: | |
87 | ||
88 | stwcx. a, 0, pw | |
89 | bne- loop | |
90 | } | |
91 | ||
92 | return a; | |
93 | } | |
94 | ||
92f5a8d4 | 95 | class BOOST_SYMBOL_VISIBLE sp_counted_base |
7c673cae FG |
96 | { |
97 | private: | |
98 | ||
99 | sp_counted_base( sp_counted_base const & ); | |
100 | sp_counted_base & operator= ( sp_counted_base const & ); | |
101 | ||
102 | long use_count_; // #shared | |
103 | long weak_count_; // #weak + (#shared != 0) | |
104 | ||
105 | public: | |
106 | ||
107 | sp_counted_base(): use_count_( 1 ), weak_count_( 1 ) | |
108 | { | |
109 | } | |
110 | ||
111 | virtual ~sp_counted_base() // nothrow | |
112 | { | |
113 | } | |
114 | ||
115 | // dispose() is called when use_count_ drops to zero, to release | |
116 | // the resources managed by *this. | |
117 | ||
118 | virtual void dispose() = 0; // nothrow | |
119 | ||
120 | // destroy() is called when weak_count_ drops to zero. | |
121 | ||
122 | virtual void destroy() // nothrow | |
123 | { | |
124 | delete this; | |
125 | } | |
126 | ||
92f5a8d4 TL |
127 | virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0; |
128 | virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0; | |
7c673cae FG |
129 | virtual void * get_untyped_deleter() = 0; |
130 | ||
131 | void add_ref_copy() | |
132 | { | |
133 | atomic_increment( &use_count_ ); | |
134 | } | |
135 | ||
136 | bool add_ref_lock() // true on success | |
137 | { | |
138 | return atomic_conditional_increment( &use_count_ ) != 0; | |
139 | } | |
140 | ||
141 | void release() // nothrow | |
142 | { | |
143 | if( atomic_decrement( &use_count_ ) == 0 ) | |
144 | { | |
145 | dispose(); | |
146 | weak_release(); | |
147 | } | |
148 | } | |
149 | ||
150 | void weak_add_ref() // nothrow | |
151 | { | |
152 | atomic_increment( &weak_count_ ); | |
153 | } | |
154 | ||
155 | void weak_release() // nothrow | |
156 | { | |
157 | if( atomic_decrement( &weak_count_ ) == 0 ) | |
158 | { | |
159 | destroy(); | |
160 | } | |
161 | } | |
162 | ||
163 | long use_count() const // nothrow | |
164 | { | |
165 | return static_cast<long const volatile &>( use_count_ ); | |
166 | } | |
167 | }; | |
168 | ||
169 | } // namespace detail | |
170 | ||
171 | } // namespace boost | |
172 | ||
173 | #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CW_PPC_HPP_INCLUDED |