1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/detail/sp_counted_base_w32.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,130 @@
1.4 +#ifndef BOOST_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
1.5 +#define BOOST_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED
1.6 +
1.7 +// MS compatible compilers support #pragma once
1.8 +
1.9 +#if defined(_MSC_VER) && (_MSC_VER >= 1020)
1.10 +# pragma once
1.11 +#endif
1.12 +
1.13 +//
1.14 +// detail/sp_counted_base_w32.hpp
1.15 +//
1.16 +// Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
1.17 +// Copyright 2004-2005 Peter Dimov
1.18 +//
1.19 +// Distributed under the Boost Software License, Version 1.0. (See
1.20 +// accompanying file LICENSE_1_0.txt or copy at
1.21 +// http://www.boost.org/LICENSE_1_0.txt)
1.22 +//
1.23 +//
1.24 +// Lock-free algorithm by Alexander Terekhov
1.25 +//
1.26 +// Thanks to Ben Hitchings for the #weak + (#shared != 0)
1.27 +// formulation
1.28 +//
1.29 +
1.30 +#include <boost/detail/interlocked.hpp>
1.31 +#include <boost/detail/workaround.hpp>
1.32 +#include <typeinfo>
1.33 +
1.34 +namespace boost
1.35 +{
1.36 +
1.37 +namespace detail
1.38 +{
1.39 +
1.40 +class sp_counted_base
1.41 +{
1.42 +private:
1.43 +
1.44 + sp_counted_base( sp_counted_base const & );
1.45 + sp_counted_base & operator= ( sp_counted_base const & );
1.46 +
1.47 + long use_count_; // #shared
1.48 + long weak_count_; // #weak + (#shared != 0)
1.49 +
1.50 +public:
1.51 +
1.52 + sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
1.53 + {
1.54 + }
1.55 +
1.56 + virtual ~sp_counted_base() // nothrow
1.57 + {
1.58 + }
1.59 +
1.60 + // dispose() is called when use_count_ drops to zero, to release
1.61 + // the resources managed by *this.
1.62 +
1.63 + virtual void dispose() = 0; // nothrow
1.64 +
1.65 + // destroy() is called when weak_count_ drops to zero.
1.66 +
1.67 + virtual void destroy() // nothrow
1.68 + {
1.69 + delete this;
1.70 + }
1.71 +
1.72 + virtual void * get_deleter( std::type_info const & ti ) = 0;
1.73 +
1.74 + void add_ref_copy()
1.75 + {
1.76 + BOOST_INTERLOCKED_INCREMENT( &use_count_ );
1.77 + }
1.78 +
1.79 + bool add_ref_lock() // true on success
1.80 + {
1.81 + for( ;; )
1.82 + {
1.83 + long tmp = static_cast< long const volatile& >( use_count_ );
1.84 + if( tmp == 0 ) return false;
1.85 +
1.86 +#if defined( BOOST_MSVC ) && BOOST_WORKAROUND( BOOST_MSVC, == 1200 )
1.87 +
1.88 + // work around a code generation bug
1.89 +
1.90 + long tmp2 = tmp + 1;
1.91 + if( BOOST_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp2, tmp ) == tmp2 - 1 ) return true;
1.92 +
1.93 +#else
1.94 +
1.95 + if( BOOST_INTERLOCKED_COMPARE_EXCHANGE( &use_count_, tmp + 1, tmp ) == tmp ) return true;
1.96 +
1.97 +#endif
1.98 + }
1.99 + }
1.100 +
1.101 + void release() // nothrow
1.102 + {
1.103 + if( BOOST_INTERLOCKED_DECREMENT( &use_count_ ) == 0 )
1.104 + {
1.105 + dispose();
1.106 + weak_release();
1.107 + }
1.108 + }
1.109 +
1.110 + void weak_add_ref() // nothrow
1.111 + {
1.112 + BOOST_INTERLOCKED_INCREMENT( &weak_count_ );
1.113 + }
1.114 +
1.115 + void weak_release() // nothrow
1.116 + {
1.117 + if( BOOST_INTERLOCKED_DECREMENT( &weak_count_ ) == 0 )
1.118 + {
1.119 + destroy();
1.120 + }
1.121 + }
1.122 +
1.123 + long use_count() const // nothrow
1.124 + {
1.125 + return static_cast<long const volatile &>( use_count_ );
1.126 + }
1.127 +};
1.128 +
1.129 +} // namespace detail
1.130 +
1.131 +} // namespace boost
1.132 +
1.133 +#endif // #ifndef BOOST_DETAIL_SP_COUNTED_BASE_W32_HPP_INCLUDED