1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SPIN_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SPIN_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 // detail/sp_counted_base_spin.hpp - spinlock pool atomic emulation
13 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
14 // Copyright 2004-2008 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 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
22 #include <boost/smart_ptr/detail/spinlock_pool.hpp>
23 #include <boost/config.hpp>
25 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
27 #include <boost/config/pragma_message.hpp>
28 BOOST_PRAGMA_MESSAGE("Using spinlock-based sp_counted_base")
38 inline int atomic_exchange_and_add( int * pw, int dv )
40 spinlock_pool<1>::scoped_lock lock( pw );
47 inline void atomic_increment( int * pw )
49 spinlock_pool<1>::scoped_lock lock( pw );
53 inline int atomic_conditional_increment( int * pw )
55 spinlock_pool<1>::scoped_lock lock( pw );
62 class BOOST_SYMBOL_VISIBLE sp_counted_base
66 sp_counted_base( sp_counted_base const & );
67 sp_counted_base & operator= ( sp_counted_base const & );
69 int use_count_; // #shared
70 int weak_count_; // #weak + (#shared != 0)
74 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
78 virtual ~sp_counted_base() // nothrow
82 // dispose() is called when use_count_ drops to zero, to release
83 // the resources managed by *this.
85 virtual void dispose() = 0; // nothrow
87 // destroy() is called when weak_count_ drops to zero.
89 virtual void destroy() // nothrow
94 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
95 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
96 virtual void * get_untyped_deleter() = 0;
100 atomic_increment( &use_count_ );
103 bool add_ref_lock() // true on success
105 return atomic_conditional_increment( &use_count_ ) != 0;
108 void release() // nothrow
110 if( atomic_exchange_and_add( &use_count_, -1 ) == 1 )
117 void weak_add_ref() // nothrow
119 atomic_increment( &weak_count_ );
122 void weak_release() // nothrow
124 if( atomic_exchange_and_add( &weak_count_, -1 ) == 1 )
130 long use_count() const // nothrow
132 spinlock_pool<1>::scoped_lock lock( &use_count_ );
137 } // namespace detail
141 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_SPIN_HPP_INCLUDED