1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 // detail/sp_counted_base_w32.hpp
13 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
14 // Copyright 2004-2005 Peter Dimov
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/smart_ptr/detail/sp_interlocked.hpp>
28 #include <boost/detail/workaround.hpp>
29 #include <boost/detail/sp_typeinfo.hpp>
41 sp_counted_base( sp_counted_base const & );
42 sp_counted_base & operator= ( sp_counted_base const & );
44 long use_count_; // #shared
45 long weak_count_; // #weak + (#shared != 0)
49 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
53 virtual ~sp_counted_base() // nothrow
57 // dispose() is called when use_count_ drops to zero, to release
58 // the resources managed by *this.
60 virtual void dispose() = 0; // nothrow
62 // destroy() is called when weak_count_ drops to zero.
64 virtual void destroy() // nothrow
69 virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
70 virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0;
71 virtual void * get_untyped_deleter() = 0;
75 BOOST_SP_INTERLOCKED_INCREMENT( &use_count_ );
78 bool add_ref_lock() // true on success
82 long tmp = static_cast< long const volatile& >( use_count_ );
83 if( tmp == 0 ) return false;
85 #if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1200 )
87 // work around a code generation bug
90 if( BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp2, tmp ) == tmp2 - 1 ) return true;
94 if( BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp + 1, tmp ) == tmp ) return true;
100 void release() // nothrow
102 if( BOOST_SP_INTERLOCKED_DECREMENT( &use_count_ ) == 0 )
109 void weak_add_ref() // nothrow
111 BOOST_SP_INTERLOCKED_INCREMENT( &weak_count_ );
114 void weak_release() // nothrow
116 if( BOOST_SP_INTERLOCKED_DECREMENT( &weak_count_ ) == 0 )
122 long use_count() const // nothrow
124 return static_cast<long const volatile &>( use_count_ );
128 } // namespace detail
132 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED