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