1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED 2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED 3 4 // MS compatible compilers support #pragma once 5 6 #if defined(_MSC_VER) && (_MSC_VER >= 1020) 7 # pragma once 8 #endif 9 10 // 11 // detail/sp_counted_base_pt.hpp 12 // 13 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. 14 // Copyright 2004-2005 Peter Dimov 15 // 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) 19 // 20 21 #include <boost/detail/sp_typeinfo.hpp> 22 #include <boost/assert.hpp> 23 #include <pthread.h> 24 25 namespace boost 26 { 27 28 namespace detail 29 { 30 31 class sp_counted_base 32 { 33 private: 34 35 sp_counted_base( sp_counted_base const & ); 36 sp_counted_base & operator= ( sp_counted_base const & ); 37 38 long use_count_; // #shared 39 long weak_count_; // #weak + (#shared != 0) 40 41 mutable pthread_mutex_t m_; 42 43 public: 44 sp_counted_base()45 sp_counted_base(): use_count_( 1 ), weak_count_( 1 ) 46 { 47 // HPUX 10.20 / DCE has a nonstandard pthread_mutex_init 48 49 #if defined(__hpux) && defined(_DECTHREADS_) 50 BOOST_VERIFY( pthread_mutex_init( &m_, pthread_mutexattr_default ) == 0 ); 51 #else 52 BOOST_VERIFY( pthread_mutex_init( &m_, 0 ) == 0 ); 53 #endif 54 } 55 ~sp_counted_base()56 virtual ~sp_counted_base() // nothrow 57 { 58 BOOST_VERIFY( pthread_mutex_destroy( &m_ ) == 0 ); 59 } 60 61 // dispose() is called when use_count_ drops to zero, to release 62 // the resources managed by *this. 63 64 virtual void dispose() = 0; // nothrow 65 66 // destroy() is called when weak_count_ drops to zero. 67 destroy()68 virtual void destroy() // nothrow 69 { 70 delete this; 71 } 72 73 virtual void * get_deleter( sp_typeinfo const & ti ) = 0; 74 virtual void * get_local_deleter( sp_typeinfo const & ti ) = 0; 75 virtual void * get_untyped_deleter() = 0; 76 add_ref_copy()77 void add_ref_copy() 78 { 79 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); 80 ++use_count_; 81 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); 82 } 83 add_ref_lock()84 bool add_ref_lock() // true on success 85 { 86 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); 87 bool r = use_count_ == 0? false: ( ++use_count_, true ); 88 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); 89 return r; 90 } 91 release()92 void release() // nothrow 93 { 94 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); 95 long new_use_count = --use_count_; 96 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); 97 98 if( new_use_count == 0 ) 99 { 100 dispose(); 101 weak_release(); 102 } 103 } 104 weak_add_ref()105 void weak_add_ref() // nothrow 106 { 107 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); 108 ++weak_count_; 109 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); 110 } 111 weak_release()112 void weak_release() // nothrow 113 { 114 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); 115 long new_weak_count = --weak_count_; 116 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); 117 118 if( new_weak_count == 0 ) 119 { 120 destroy(); 121 } 122 } 123 use_count() const124 long use_count() const // nothrow 125 { 126 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); 127 long r = use_count_; 128 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); 129 130 return r; 131 } 132 }; 133 134 } // namespace detail 135 136 } // namespace boost 137 138 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED 139