]> git.lyx.org Git - lyx.git/blob - 3rdparty/boost/boost/smart_ptr/detail/sp_counted_base_aix.hpp
Update to boost 1.72
[lyx.git] / 3rdparty / boost / boost / smart_ptr / detail / sp_counted_base_aix.hpp
1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_AIX_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_AIX_HPP_INCLUDED
3
4 //
5 //  detail/sp_counted_base_aix.hpp
6 //   based on: detail/sp_counted_base_w32.hpp
7 //
8 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
9 //  Copyright 2004-2005 Peter Dimov
10 //  Copyright 2006 Michael van der Westhuizen
11 //
12 //  Distributed under the Boost Software License, Version 1.0. (See
13 //  accompanying file LICENSE_1_0.txt or copy at
14 //  http://www.boost.org/LICENSE_1_0.txt)
15 //
16 //
17 //  Lock-free algorithm by Alexander Terekhov
18 //
19 //  Thanks to Ben Hitchings for the #weak + (#shared != 0)
20 //  formulation
21 //
22
23 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
24 #include <boost/config.hpp>
25 #include <builtins.h>
26 #include <sys/atomic_op.h>
27
28 namespace boost
29 {
30
31 namespace detail
32 {
33
34 inline void atomic_increment( int32_t* pw )
35 {
36     // ++*pw;
37
38     fetch_and_add( pw, 1 );
39 }
40
41 inline int32_t atomic_decrement( int32_t * pw )
42 {
43     // return --*pw;
44
45     int32_t originalValue;
46
47     __lwsync();
48     originalValue = fetch_and_add( pw, -1 );
49     __isync();
50
51     return (originalValue - 1);
52 }
53
54 inline int32_t atomic_conditional_increment( int32_t * pw )
55 {
56     // if( *pw != 0 ) ++*pw;
57     // return *pw;
58
59     int32_t tmp = fetch_and_add( pw, 0 );
60     for( ;; )
61     {
62         if( tmp == 0 ) return 0;
63         if( compare_and_swap( pw, &tmp, tmp + 1 ) ) return (tmp + 1);
64     }
65 }
66
67 class BOOST_SYMBOL_VISIBLE sp_counted_base
68 {
69 private:
70
71     sp_counted_base( sp_counted_base const & );
72     sp_counted_base & operator= ( sp_counted_base const & );
73
74     int32_t use_count_;        // #shared
75     int32_t weak_count_;       // #weak + (#shared != 0)
76
77 public:
78
79     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
80     {
81     }
82
83     virtual ~sp_counted_base() // nothrow
84     {
85     }
86
87     // dispose() is called when use_count_ drops to zero, to release
88     // the resources managed by *this.
89
90     virtual void dispose() = 0; // nothrow
91
92     // destroy() is called when weak_count_ drops to zero.
93
94     virtual void destroy() // nothrow
95     {
96         delete this;
97     }
98
99     virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
100     virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
101     virtual void * get_untyped_deleter() = 0;
102
103     void add_ref_copy()
104     {
105         atomic_increment( &use_count_ );
106     }
107
108     bool add_ref_lock() // true on success
109     {
110         return atomic_conditional_increment( &use_count_ ) != 0;
111     }
112
113     void release() // nothrow
114     {
115         if( atomic_decrement( &use_count_ ) == 0 )
116         {
117             dispose();
118             weak_release();
119         }
120     }
121
122     void weak_add_ref() // nothrow
123     {
124         atomic_increment( &weak_count_ );
125     }
126
127     void weak_release() // nothrow
128     {
129         if( atomic_decrement( &weak_count_ ) == 0 )
130         {
131             destroy();
132         }
133     }
134
135     long use_count() const // nothrow
136     {
137         return fetch_and_add( const_cast<int32_t*>(&use_count_), 0 );
138     }
139 };
140
141 } // namespace detail
142
143 } // namespace boost
144
145 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_AIX_HPP_INCLUDED