Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:42

0001 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED
0002 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED
0003 
0004 // MS compatible compilers support #pragma once
0005 
0006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
0007 # pragma once
0008 #endif
0009 
0010 //
0011 //  detail/sp_counted_base_pt.hpp
0012 //
0013 //  Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
0014 //  Copyright 2004-2005 Peter Dimov
0015 //
0016 // Distributed under the Boost Software License, Version 1.0. (See
0017 // accompanying file LICENSE_1_0.txt or copy at
0018 // http://www.boost.org/LICENSE_1_0.txt)
0019 //
0020 
0021 #include <boost/smart_ptr/detail/sp_typeinfo_.hpp>
0022 #include <boost/assert.hpp>
0023 #include <boost/config.hpp>
0024 #include <boost/cstdint.hpp>
0025 #include <pthread.h>
0026 
0027 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
0028 
0029 #include <boost/config/pragma_message.hpp>
0030 BOOST_PRAGMA_MESSAGE("Using pthread_mutex sp_counted_base")
0031 
0032 #endif
0033 
0034 namespace boost
0035 {
0036 
0037 namespace detail
0038 {
0039 
0040 class BOOST_SYMBOL_VISIBLE sp_counted_base
0041 {
0042 private:
0043 
0044     sp_counted_base( sp_counted_base const & );
0045     sp_counted_base & operator= ( sp_counted_base const & );
0046 
0047     boost::int_least32_t use_count_;        // #shared
0048     boost::int_least32_t weak_count_;       // #weak + (#shared != 0)
0049 
0050     mutable pthread_mutex_t m_;
0051 
0052 public:
0053 
0054     sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
0055     {
0056 // HPUX 10.20 / DCE has a nonstandard pthread_mutex_init
0057 
0058 #if defined(__hpux) && defined(_DECTHREADS_)
0059         BOOST_VERIFY( pthread_mutex_init( &m_, pthread_mutexattr_default ) == 0 );
0060 #else
0061         BOOST_VERIFY( pthread_mutex_init( &m_, 0 ) == 0 );
0062 #endif
0063     }
0064 
0065     virtual ~sp_counted_base() // nothrow
0066     {
0067         BOOST_VERIFY( pthread_mutex_destroy( &m_ ) == 0 );
0068     }
0069 
0070     // dispose() is called when use_count_ drops to zero, to release
0071     // the resources managed by *this.
0072 
0073     virtual void dispose() = 0; // nothrow
0074 
0075     // destroy() is called when weak_count_ drops to zero.
0076 
0077     virtual void destroy() // nothrow
0078     {
0079         delete this;
0080     }
0081 
0082     virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
0083     virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
0084     virtual void * get_untyped_deleter() = 0;
0085 
0086     void add_ref_copy()
0087     {
0088         BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
0089         ++use_count_;
0090         BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
0091     }
0092 
0093     bool add_ref_lock() // true on success
0094     {
0095         BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
0096         bool r = use_count_ == 0? false: ( ++use_count_, true );
0097         BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
0098         return r;
0099     }
0100 
0101     void release() // nothrow
0102     {
0103         BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
0104         boost::int_least32_t new_use_count = --use_count_;
0105         BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
0106 
0107         if( new_use_count == 0 )
0108         {
0109             dispose();
0110             weak_release();
0111         }
0112     }
0113 
0114     void weak_add_ref() // nothrow
0115     {
0116         BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
0117         ++weak_count_;
0118         BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
0119     }
0120 
0121     void weak_release() // nothrow
0122     {
0123         BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
0124         boost::int_least32_t new_weak_count = --weak_count_;
0125         BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
0126 
0127         if( new_weak_count == 0 )
0128         {
0129             destroy();
0130         }
0131     }
0132 
0133     long use_count() const // nothrow
0134     {
0135         BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
0136         boost::int_least32_t r = use_count_;
0137         BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
0138 
0139         return r;
0140     }
0141 };
0142 
0143 } // namespace detail
0144 
0145 } // namespace boost
0146 
0147 #endif  // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED