1 #ifndef BOOST_INTERPROCESS_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED
2 #define BOOST_INTERPROCESS_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED
4 #ifndef BOOST_CONFIG_HPP
5 # include <boost/config.hpp>
8 #if defined(BOOST_HAS_PRAGMA_ONCE)
12 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
13 // Copyright 2004-2005 Peter Dimov
14 // Copyright 2007-2012 Ion Gaztanaga
16 // Distributed under the Boost Software License, Version 1.0. (See
17 // accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
21 // Lock-free algorithm by Alexander Terekhov
23 // Thanks to Ben Hitchings for the #weak + (#shared != 0)
27 #include <boost/interprocess/detail/config_begin.hpp>
28 #include <boost/interprocess/detail/workaround.hpp>
30 #include <boost/interprocess/detail/atomic.hpp>
35 namespace interprocess {
43 sp_counted_base( sp_counted_base const & );
44 sp_counted_base & operator= ( sp_counted_base const & );
46 boost::uint32_t use_count_; // #shared
47 boost::uint32_t weak_count_; // #weak + (#shared != 0)
51 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
54 ~sp_counted_base() // nothrow
59 ipcdetail::atomic_inc32( &use_count_ );
62 bool add_ref_lock() // true on success
66 boost::uint32_t tmp = static_cast< boost::uint32_t const volatile& >( use_count_ );
67 if( tmp == 0 ) return false;
68 if( ipcdetail::atomic_cas32( &use_count_, tmp + 1, tmp ) == tmp )
73 bool ref_release() // nothrow
74 { return 1 == ipcdetail::atomic_dec32( &use_count_ ); }
76 void weak_add_ref() // nothrow
77 { ipcdetail::atomic_inc32( &weak_count_ ); }
79 bool weak_release() // nothrow
80 { return 1 == ipcdetail::atomic_dec32( &weak_count_ ); }
82 long use_count() const // nothrow
83 { return (long)static_cast<boost::uint32_t const volatile &>( use_count_ ); }
86 } // namespace ipcdetail
88 } // namespace interprocess
92 #include <boost/interprocess/detail/config_end.hpp>
94 #endif // #ifndef BOOST_INTERPROCESS_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED