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/detail/sp_typeinfo.hpp>
|
---|
24 | #include <sys/atomic_op.h>
|
---|
25 |
|
---|
26 | namespace boost
|
---|
27 | {
|
---|
28 |
|
---|
29 | inline void atomic_increment( int32_t* pw )
|
---|
30 | {
|
---|
31 | // ++*pw;
|
---|
32 |
|
---|
33 | fetch_and_add( pw, 1 );
|
---|
34 | }
|
---|
35 |
|
---|
36 | inline int32_t atomic_decrement( int32_t * pw )
|
---|
37 | {
|
---|
38 | // return --*pw;
|
---|
39 |
|
---|
40 | int32_t originalValue;
|
---|
41 |
|
---|
42 | __asm__ __volatile__( "sync" );
|
---|
43 | originalValue = fetch_and_add( pw, -1 );
|
---|
44 | __asm__ __volatile__( "isync" );
|
---|
45 |
|
---|
46 | return (originalValue - 1);
|
---|
47 | }
|
---|
48 |
|
---|
49 | inline int32_t atomic_conditional_increment( int32_t * pw )
|
---|
50 | {
|
---|
51 | // if( *pw != 0 ) ++*pw;
|
---|
52 | // return *pw;
|
---|
53 |
|
---|
54 | int32_t tmp = fetch_and_add( pw, 0 );
|
---|
55 | for( ;; )
|
---|
56 | {
|
---|
57 | if( tmp == 0 ) return 0;
|
---|
58 | if( compare_and_swap( pw, &tmp, tmp + 1 ) ) return (tmp + 1);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | namespace detail
|
---|
63 | {
|
---|
64 |
|
---|
65 | class sp_counted_base
|
---|
66 | {
|
---|
67 | private:
|
---|
68 |
|
---|
69 | sp_counted_base( sp_counted_base const & );
|
---|
70 | sp_counted_base & operator= ( sp_counted_base const & );
|
---|
71 |
|
---|
72 | int32_t use_count_; // #shared
|
---|
73 | int32_t weak_count_; // #weak + (#shared != 0)
|
---|
74 |
|
---|
75 | public:
|
---|
76 |
|
---|
77 | sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
|
---|
78 | {
|
---|
79 | }
|
---|
80 |
|
---|
81 | virtual ~sp_counted_base() // nothrow
|
---|
82 | {
|
---|
83 | }
|
---|
84 |
|
---|
85 | // dispose() is called when use_count_ drops to zero, to release
|
---|
86 | // the resources managed by *this.
|
---|
87 |
|
---|
88 | virtual void dispose() = 0; // nothrow
|
---|
89 |
|
---|
90 | // destroy() is called when weak_count_ drops to zero.
|
---|
91 |
|
---|
92 | virtual void destroy() // nothrow
|
---|
93 | {
|
---|
94 | delete this;
|
---|
95 | }
|
---|
96 |
|
---|
97 | virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
|
---|
98 |
|
---|
99 | void add_ref_copy()
|
---|
100 | {
|
---|
101 | atomic_increment( &use_count_ );
|
---|
102 | }
|
---|
103 |
|
---|
104 | bool add_ref_lock() // true on success
|
---|
105 | {
|
---|
106 | return atomic_conditional_increment( &use_count_ ) != 0;
|
---|
107 | }
|
---|
108 |
|
---|
109 | void release() // nothrow
|
---|
110 | {
|
---|
111 | if( atomic_decrement( &use_count_ ) == 0 )
|
---|
112 | {
|
---|
113 | dispose();
|
---|
114 | weak_release();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | void weak_add_ref() // nothrow
|
---|
119 | {
|
---|
120 | atomic_increment( &weak_count_ );
|
---|
121 | }
|
---|
122 |
|
---|
123 | void weak_release() // nothrow
|
---|
124 | {
|
---|
125 | if( atomic_decrement( &weak_count_ ) == 0 )
|
---|
126 | {
|
---|
127 | destroy();
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | long use_count() const // nothrow
|
---|
132 | {
|
---|
133 | return fetch_and_add( const_cast<int32_t*>(&use_count_), 0 );
|
---|
134 | }
|
---|
135 | };
|
---|
136 |
|
---|
137 | } // namespace detail
|
---|
138 |
|
---|
139 | } // namespace boost
|
---|
140 |
|
---|
141 | #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_AIX_HPP_INCLUDED
|
---|