1 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
11 // detail/sp_counted_base_pt.hpp
13 // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
14 // Copyright 2004-2005 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/detail/sp_typeinfo.hpp>
22 #include <boost/assert.hpp>
35 sp_counted_base( sp_counted_base const & );
36 sp_counted_base & operator= ( sp_counted_base const & );
38 long use_count_; // #shared
39 long weak_count_; // #weak + (#shared != 0)
41 mutable pthread_mutex_t m_;
45 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
47 // HPUX 10.20 / DCE has a nonstandard pthread_mutex_init
49 #if defined(__hpux) && defined(_DECTHREADS_)
50 BOOST_VERIFY( pthread_mutex_init( &m_, pthread_mutexattr_default ) == 0 );
52 BOOST_VERIFY( pthread_mutex_init( &m_, 0 ) == 0 );
56 virtual ~sp_counted_base() // nothrow
58 BOOST_VERIFY( pthread_mutex_destroy( &m_ ) == 0 );
61 // dispose() is called when use_count_ drops to zero, to release
62 // the resources managed by *this.
64 virtual void dispose() = 0; // nothrow
66 // destroy() is called when weak_count_ drops to zero.
68 virtual void destroy() // nothrow
73 virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
74 virtual void * get_untyped_deleter() = 0;
78 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
80 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
83 bool add_ref_lock() // true on success
85 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
86 bool r = use_count_ == 0? false: ( ++use_count_, true );
87 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
91 void release() // nothrow
93 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
94 long new_use_count = --use_count_;
95 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
97 if( new_use_count == 0 )
104 void weak_add_ref() // nothrow
106 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
108 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
111 void weak_release() // nothrow
113 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
114 long new_weak_count = --weak_count_;
115 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
117 if( new_weak_count == 0 )
123 long use_count() const // nothrow
125 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
127 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
133 } // namespace detail
137 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED