1 #ifndef BOOST_INTERPROCESS_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED
2 #define BOOST_INTERPROCESS_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
10 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
11 // Copyright 2004-2005 Peter Dimov
12 // Copyright 2007-2012 Ion Gaztanaga
14 // Distributed under the Boost Software License, Version 1.0. (See
15 // accompanying file LICENSE_1_0.txt or copy at
16 // http://www.boost.org/LICENSE_1_0.txt)
19 // Lock-free algorithm by Alexander Terekhov
21 // Thanks to Ben Hitchings for the #weak + (#shared != 0)
25 #include <boost/interprocess/detail/config_begin.hpp>
26 #include <boost/interprocess/detail/workaround.hpp>
28 #include <boost/interprocess/detail/atomic.hpp>
33 namespace interprocess {
41 sp_counted_base( sp_counted_base const & );
42 sp_counted_base & operator= ( sp_counted_base const & );
44 boost::uint32_t use_count_; // #shared
45 boost::uint32_t weak_count_; // #weak + (#shared != 0)
49 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
52 ~sp_counted_base() // nothrow
57 ipcdetail::atomic_inc32( &use_count_ );
60 bool add_ref_lock() // true on success
64 boost::uint32_t tmp = static_cast< boost::uint32_t const volatile& >( use_count_ );
65 if( tmp == 0 ) return false;
66 if( ipcdetail::atomic_cas32( &use_count_, tmp + 1, tmp ) == tmp )
71 bool ref_release() // nothrow
72 { return 1 == ipcdetail::atomic_dec32( &use_count_ ); }
74 void weak_add_ref() // nothrow
75 { ipcdetail::atomic_inc32( &weak_count_ ); }
77 bool weak_release() // nothrow
78 { return 1 == ipcdetail::atomic_dec32( &weak_count_ ); }
80 long use_count() const // nothrow
81 { return (long)static_cast<boost::uint32_t const volatile &>( use_count_ ); }
84 } // namespace ipcdetail
86 } // namespace interprocess
90 #include <boost/interprocess/detail/config_end.hpp>
92 #endif // #ifndef BOOST_INTERPROCESS_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED