1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_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 //  detail/sp_counted_base_gcc_sparc.hpp - g++ on Sparc V8+
11 //
12 //  Copyright (c) 2006 Piotr Wyderski
13 //  Copyright (c) 2006 Tomas Puverle
14 //  Copyright (c) 2006 Peter Dimov
15 //
16 //  Distributed under the Boost Software License, Version 1.0.
17 //  See accompanying file LICENSE_1_0.txt or copy at
18 //  http://www.boost.org/LICENSE_1_0.txt
19 //
20 //  Thanks to Michael van der Westhuizen
21 
22 #include <boost/detail/sp_typeinfo.hpp>
23 #include <inttypes.h> // int32_t
24 
25 namespace boost
26 {
27 
28 namespace detail
29 {
30 
compare_and_swap(int32_t * dest_,int32_t compare_,int32_t swap_)31 inline int32_t compare_and_swap( int32_t * dest_, int32_t compare_, int32_t swap_ )
32 {
33     __asm__ __volatile__( "cas [%1], %2, %0"
34                         : "+r" (swap_)
35                         : "r" (dest_), "r" (compare_)
36                         : "memory" );
37 
38     return swap_;
39 }
40 
atomic_fetch_and_add(int32_t * pw,int32_t dv)41 inline int32_t atomic_fetch_and_add( int32_t * pw, int32_t dv )
42 {
43     // long r = *pw;
44     // *pw += dv;
45     // return r;
46 
47     for( ;; )
48     {
49         int32_t r = *pw;
50 
51         if( __builtin_expect((compare_and_swap(pw, r, r + dv) == r), 1) )
52         {
53             return r;
54         }
55     }
56 }
57 
atomic_increment(int32_t * pw)58 inline void atomic_increment( int32_t * pw )
59 {
60     atomic_fetch_and_add( pw, 1 );
61 }
62 
atomic_decrement(int32_t * pw)63 inline int32_t atomic_decrement( int32_t * pw )
64 {
65     return atomic_fetch_and_add( pw, -1 );
66 }
67 
atomic_conditional_increment(int32_t * pw)68 inline int32_t atomic_conditional_increment( int32_t * pw )
69 {
70     // long r = *pw;
71     // if( r != 0 ) ++*pw;
72     // return r;
73 
74     for( ;; )
75     {
76         int32_t r = *pw;
77 
78         if( r == 0 )
79         {
80             return r;
81         }
82 
83         if( __builtin_expect( ( compare_and_swap( pw, r, r + 1 ) == r ), 1 ) )
84         {
85             return r;
86         }
87     }
88 }
89 
90 class sp_counted_base
91 {
92 private:
93 
94     sp_counted_base( sp_counted_base const & );
95     sp_counted_base & operator= ( sp_counted_base const & );
96 
97     int32_t use_count_;        // #shared
98     int32_t weak_count_;       // #weak + (#shared != 0)
99 
100 public:
101 
sp_counted_base()102     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
103     {
104     }
105 
~sp_counted_base()106     virtual ~sp_counted_base() // nothrow
107     {
108     }
109 
110     // dispose() is called when use_count_ drops to zero, to release
111     // the resources managed by *this.
112 
113     virtual void dispose() = 0; // nothrow
114 
115     // destroy() is called when weak_count_ drops to zero.
116 
destroy()117     virtual void destroy() // nothrow
118     {
119         delete this;
120     }
121 
122     virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
123     virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
124     virtual void * get_untyped_deleter() = 0;
125 
add_ref_copy()126     void add_ref_copy()
127     {
128         atomic_increment( &use_count_ );
129     }
130 
add_ref_lock()131     bool add_ref_lock() // true on success
132     {
133         return atomic_conditional_increment( &use_count_ ) != 0;
134     }
135 
release()136     void release() // nothrow
137     {
138         if( atomic_decrement( &use_count_ ) == 1 )
139         {
140             dispose();
141             weak_release();
142         }
143     }
144 
weak_add_ref()145     void weak_add_ref() // nothrow
146     {
147         atomic_increment( &weak_count_ );
148     }
149 
weak_release()150     void weak_release() // nothrow
151     {
152         if( atomic_decrement( &weak_count_ ) == 1 )
153         {
154             destroy();
155         }
156     }
157 
use_count() const158     long use_count() const // nothrow
159     {
160         return const_cast< int32_t const volatile & >( use_count_ );
161     }
162 };
163 
164 } // namespace detail
165 
166 } // namespace boost
167 
168 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_GCC_SPARC_HPP_INCLUDED
169