Go to the documentation of this file.00001 #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED
00003
00004
00005
00006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00007 # pragma once
00008 #endif
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <boost/detail/sp_typeinfo.hpp>
00022 #include <pthread.h>
00023
00024 namespace boost
00025 {
00026
00027 namespace detail
00028 {
00029
00030 class sp_counted_base
00031 {
00032 private:
00033
00034 sp_counted_base( sp_counted_base const & );
00035 sp_counted_base & operator= ( sp_counted_base const & );
00036
00037 long use_count_;
00038 long weak_count_;
00039
00040 mutable pthread_mutex_t m_;
00041
00042 public:
00043
00044 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
00045 {
00046
00047
00048 #if defined(__hpux) && defined(_DECTHREADS_)
00049 pthread_mutex_init( &m_, pthread_mutexattr_default );
00050 #else
00051 pthread_mutex_init( &m_, 0 );
00052 #endif
00053 }
00054
00055 virtual ~sp_counted_base()
00056 {
00057 pthread_mutex_destroy( &m_ );
00058 }
00059
00060
00061
00062
00063 virtual void dispose() = 0;
00064
00065
00066
00067 virtual void destroy()
00068 {
00069 delete this;
00070 }
00071
00072 virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
00073
00074 void add_ref_copy()
00075 {
00076 pthread_mutex_lock( &m_ );
00077 ++use_count_;
00078 pthread_mutex_unlock( &m_ );
00079 }
00080
00081 bool add_ref_lock()
00082 {
00083 pthread_mutex_lock( &m_ );
00084 bool r = use_count_ == 0? false: ( ++use_count_, true );
00085 pthread_mutex_unlock( &m_ );
00086 return r;
00087 }
00088
00089 void release()
00090 {
00091 pthread_mutex_lock( &m_ );
00092 long new_use_count = --use_count_;
00093 pthread_mutex_unlock( &m_ );
00094
00095 if( new_use_count == 0 )
00096 {
00097 dispose();
00098 weak_release();
00099 }
00100 }
00101
00102 void weak_add_ref()
00103 {
00104 pthread_mutex_lock( &m_ );
00105 ++weak_count_;
00106 pthread_mutex_unlock( &m_ );
00107 }
00108
00109 void weak_release()
00110 {
00111 pthread_mutex_lock( &m_ );
00112 long new_weak_count = --weak_count_;
00113 pthread_mutex_unlock( &m_ );
00114
00115 if( new_weak_count == 0 )
00116 {
00117 destroy();
00118 }
00119 }
00120
00121 long use_count() const
00122 {
00123 pthread_mutex_lock( &m_ );
00124 long r = use_count_;
00125 pthread_mutex_unlock( &m_ );
00126
00127 return r;
00128 }
00129 };
00130
00131 }
00132
00133 }
00134
00135 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED