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>
34
sp_counted_base( sp_counted_base const & );
35
sp_counted_base & operator= ( sp_counted_base const & );
37
long use_count_; // #shared
38
long weak_count_; // #weak + (#shared != 0)
40
mutable pthread_mutex_t m_;
44
sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
46
// HPUX 10.20 / DCE has a nonstandard pthread_mutex_init
48
#if defined(__hpux) && defined(_DECTHREADS_)
49
pthread_mutex_init( &m_, pthread_mutexattr_default );
51
pthread_mutex_init( &m_, 0 );
55
virtual ~sp_counted_base() // nothrow
57
pthread_mutex_destroy( &m_ );
60
// dispose() is called when use_count_ drops to zero, to release
61
// the resources managed by *this.
63
virtual void dispose() = 0; // nothrow
65
// destroy() is called when weak_count_ drops to zero.
67
virtual void destroy() // nothrow
72
virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
76
pthread_mutex_lock( &m_ );
78
pthread_mutex_unlock( &m_ );
81
bool add_ref_lock() // true on success
83
pthread_mutex_lock( &m_ );
84
bool r = use_count_ == 0? false: ( ++use_count_, true );
85
pthread_mutex_unlock( &m_ );
89
void release() // nothrow
91
pthread_mutex_lock( &m_ );
92
long new_use_count = --use_count_;
93
pthread_mutex_unlock( &m_ );
95
if( new_use_count == 0 )
102
void weak_add_ref() // nothrow
104
pthread_mutex_lock( &m_ );
106
pthread_mutex_unlock( &m_ );
109
void weak_release() // nothrow
111
pthread_mutex_lock( &m_ );
112
long new_weak_count = --weak_count_;
113
pthread_mutex_unlock( &m_ );
115
if( new_weak_count == 0 )
121
long use_count() const // nothrow
123
pthread_mutex_lock( &m_ );
125
pthread_mutex_unlock( &m_ );
131
} // namespace detail
135
#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED