| 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 | namespace detail |
| 30 | { |
| 31 | |
| 32 | class sp_counted_base |
| 33 | { |
| 34 | private: |
| 35 | |
| 36 | sp_counted_base( sp_counted_base const & ); |
| 37 | sp_counted_base & operator= ( sp_counted_base const & ); |
| 38 | |
| 39 | int32_t use_count_; // #shared |
| 40 | int32_t weak_count_; // #weak + (#shared != 0) |
| 41 | |
| 42 | public: |
| 43 | |
| 44 | sp_counted_base(): use_count_( 1 ), weak_count_( 1 ) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | virtual ~sp_counted_base() // nothrow |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | // dispose() is called when use_count_ drops to zero, to release |
| 53 | // the resources managed by *this. |
| 54 | |
| 55 | virtual void dispose() = 0; // nothrow |
| 56 | |
| 57 | // destroy() is called when weak_count_ drops to zero. |
| 58 | |
| 59 | virtual void destroy() // nothrow |
| 60 | { |
| 61 | delete this; |
| 62 | } |
| 63 | |
| 64 | virtual void * get_deleter( sp_typeinfo const & ti ) = 0; |
| 65 | |
| 66 | void add_ref_copy() |
| 67 | { |
| 68 | fetch_and_add( &use_count_, 1 ); |
| 69 | } |
| 70 | |
| 71 | bool add_ref_lock() // true on success |
| 72 | { |
| 73 | int32_t tmp = fetch_and_add( &use_count_, 0 ); |
| 74 | for( ;; ) |
| 75 | { |
| 76 | if( tmp == 0 ) return false; |
| 77 | if( compare_and_swap( &use_count_, &tmp, tmp + 1 ) ) return true; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void release() // nothrow |
| 82 | { |
| 83 | if( fetch_and_add( &use_count_, -1 ) == 1 ) |
| 84 | { |
| 85 | dispose(); |
| 86 | weak_release(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void weak_add_ref() // nothrow |
| 91 | { |
| 92 | fetch_and_add( &weak_count_, 1 ); |
| 93 | } |
| 94 | |
| 95 | void weak_release() // nothrow |
| 96 | { |
| 97 | if( fetch_and_add( &weak_count_, -1 ) == 1 ) |
| 98 | { |
| 99 | destroy(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | long use_count() const // nothrow |
| 104 | { |
| 105 | return fetch_and_add( const_cast<int32_t*>(&use_count_), 0 ); |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | } // namespace detail |
| 110 | |
| 111 | } // namespace boost |
| 112 | |
| 113 | #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_AIX_HPP_INCLUDED |