fix doc example typo
[boost.git] / boost / interprocess / smart_ptr / detail / sp_counted_base_atomic.hpp
blob3fee4e7c1a853f956437d3e768fd128ae8f0c753
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
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
10 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
11 // Copyright 2004-2005 Peter Dimov
12 // Copyright 2007-2008 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)
22 // formulation
25 #include <boost/interprocess/detail/config_begin.hpp>
26 #include <boost/interprocess/detail/workaround.hpp>
28 #include <boost/interprocess/detail/atomic.hpp>
29 #include <typeinfo>
31 namespace boost {
33 namespace interprocess {
35 namespace detail {
37 class sp_counted_base
39 private:
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)
47 public:
49 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
52 ~sp_counted_base() // nothrow
55 void add_ref_copy()
57 detail::atomic_inc32( &use_count_ );
60 bool add_ref_lock() // true on success
62 for( ;; )
64 boost::uint32_t tmp = static_cast< boost::uint32_t const volatile& >( use_count_ );
65 if( tmp == 0 ) return false;
66 if( detail::atomic_cas32( &use_count_, tmp + 1, tmp ) == tmp )
67 return true;
71 bool ref_release() // nothrow
72 { return 1 == detail::atomic_dec32( &use_count_ ); }
74 void weak_add_ref() // nothrow
75 { detail::atomic_inc32( &weak_count_ ); }
77 bool weak_release() // nothrow
78 { return 1 == detail::atomic_dec32( &weak_count_ ); }
80 long use_count() const // nothrow
81 { return (long)static_cast<boost::uint32_t const volatile &>( use_count_ ); }
84 } // namespace detail
86 } // namespace interprocess
88 } // namespace boost
90 #include <boost/interprocess/detail/config_end.hpp>
92 #endif // #ifndef BOOST_INTERPROCESS_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED