1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/detail/sp_counted_base_pt.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,135 @@
1.4 +#ifndef BOOST_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED
1.5 +#define BOOST_DETAIL_SP_COUNTED_BASE_PT_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_pt.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 +#include <typeinfo>
1.25 +#include <pthread.h>
1.26 +
1.27 +namespace boost
1.28 +{
1.29 +
1.30 +namespace detail
1.31 +{
1.32 +
1.33 +class sp_counted_base
1.34 +{
1.35 +private:
1.36 +
1.37 + sp_counted_base( sp_counted_base const & );
1.38 + sp_counted_base & operator= ( sp_counted_base const & );
1.39 +
1.40 + long use_count_; // #shared
1.41 + long weak_count_; // #weak + (#shared != 0)
1.42 +
1.43 + mutable pthread_mutex_t m_;
1.44 +
1.45 +public:
1.46 +
1.47 + sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
1.48 + {
1.49 +// HPUX 10.20 / DCE has a nonstandard pthread_mutex_init
1.50 +
1.51 +#if defined(__hpux) && defined(_DECTHREADS_)
1.52 + pthread_mutex_init( &m_, pthread_mutexattr_default );
1.53 +#else
1.54 + pthread_mutex_init( &m_, 0 );
1.55 +#endif
1.56 + }
1.57 +
1.58 + virtual ~sp_counted_base() // nothrow
1.59 + {
1.60 + pthread_mutex_destroy( &m_ );
1.61 + }
1.62 +
1.63 + // dispose() is called when use_count_ drops to zero, to release
1.64 + // the resources managed by *this.
1.65 +
1.66 + virtual void dispose() = 0; // nothrow
1.67 +
1.68 + // destroy() is called when weak_count_ drops to zero.
1.69 +
1.70 + virtual void destroy() // nothrow
1.71 + {
1.72 + delete this;
1.73 + }
1.74 +
1.75 + virtual void * get_deleter( std::type_info const & ti ) = 0;
1.76 +
1.77 + void add_ref_copy()
1.78 + {
1.79 + pthread_mutex_lock( &m_ );
1.80 + ++use_count_;
1.81 + pthread_mutex_unlock( &m_ );
1.82 + }
1.83 +
1.84 + bool add_ref_lock() // true on success
1.85 + {
1.86 + pthread_mutex_lock( &m_ );
1.87 + bool r = use_count_ == 0? false: ( ++use_count_, true );
1.88 + pthread_mutex_unlock( &m_ );
1.89 + return r;
1.90 + }
1.91 +
1.92 + void release() // nothrow
1.93 + {
1.94 + pthread_mutex_lock( &m_ );
1.95 + long new_use_count = --use_count_;
1.96 + pthread_mutex_unlock( &m_ );
1.97 +
1.98 + if( new_use_count == 0 )
1.99 + {
1.100 + dispose();
1.101 + weak_release();
1.102 + }
1.103 + }
1.104 +
1.105 + void weak_add_ref() // nothrow
1.106 + {
1.107 + pthread_mutex_lock( &m_ );
1.108 + ++weak_count_;
1.109 + pthread_mutex_unlock( &m_ );
1.110 + }
1.111 +
1.112 + void weak_release() // nothrow
1.113 + {
1.114 + pthread_mutex_lock( &m_ );
1.115 + long new_weak_count = --weak_count_;
1.116 + pthread_mutex_unlock( &m_ );
1.117 +
1.118 + if( new_weak_count == 0 )
1.119 + {
1.120 + destroy();
1.121 + }
1.122 + }
1.123 +
1.124 + long use_count() const // nothrow
1.125 + {
1.126 + pthread_mutex_lock( &m_ );
1.127 + long r = use_count_;
1.128 + pthread_mutex_unlock( &m_ );
1.129 +
1.130 + return r;
1.131 + }
1.132 +};
1.133 +
1.134 +} // namespace detail
1.135 +
1.136 +} // namespace boost
1.137 +
1.138 +#endif // #ifndef BOOST_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED