]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/smart_ptr/detail/sp_counted_base_pt.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / smart_ptr / detail / sp_counted_base_pt.hpp
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/smart_ptr/detail/sp_typeinfo_.hpp>
22 #include <boost/assert.hpp>
23 #include <boost/config.hpp>
24 #include <boost/cstdint.hpp>
25 #include <pthread.h>
26
27 #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
28
29 #include <boost/config/pragma_message.hpp>
30 BOOST_PRAGMA_MESSAGE("Using pthread_mutex sp_counted_base")
31
32 #endif
33
34 namespace boost
35 {
36
37 namespace detail
38 {
39
40 class BOOST_SYMBOL_VISIBLE sp_counted_base
41 {
42 private:
43
44 sp_counted_base( sp_counted_base const & );
45 sp_counted_base & operator= ( sp_counted_base const & );
46
47 boost::int_least32_t use_count_; // #shared
48 boost::int_least32_t weak_count_; // #weak + (#shared != 0)
49
50 mutable pthread_mutex_t m_;
51
52 public:
53
54 sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
55 {
56 // HPUX 10.20 / DCE has a nonstandard pthread_mutex_init
57
58 #if defined(__hpux) && defined(_DECTHREADS_)
59 BOOST_VERIFY( pthread_mutex_init( &m_, pthread_mutexattr_default ) == 0 );
60 #else
61 BOOST_VERIFY( pthread_mutex_init( &m_, 0 ) == 0 );
62 #endif
63 }
64
65 virtual ~sp_counted_base() // nothrow
66 {
67 BOOST_VERIFY( pthread_mutex_destroy( &m_ ) == 0 );
68 }
69
70 // dispose() is called when use_count_ drops to zero, to release
71 // the resources managed by *this.
72
73 virtual void dispose() = 0; // nothrow
74
75 // destroy() is called when weak_count_ drops to zero.
76
77 virtual void destroy() // nothrow
78 {
79 delete this;
80 }
81
82 virtual void * get_deleter( sp_typeinfo_ const & ti ) = 0;
83 virtual void * get_local_deleter( sp_typeinfo_ const & ti ) = 0;
84 virtual void * get_untyped_deleter() = 0;
85
86 void add_ref_copy()
87 {
88 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
89 ++use_count_;
90 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
91 }
92
93 bool add_ref_lock() // true on success
94 {
95 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
96 bool r = use_count_ == 0? false: ( ++use_count_, true );
97 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
98 return r;
99 }
100
101 void release() // nothrow
102 {
103 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
104 boost::int_least32_t new_use_count = --use_count_;
105 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
106
107 if( new_use_count == 0 )
108 {
109 dispose();
110 weak_release();
111 }
112 }
113
114 void weak_add_ref() // nothrow
115 {
116 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
117 ++weak_count_;
118 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
119 }
120
121 void weak_release() // nothrow
122 {
123 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
124 boost::int_least32_t new_weak_count = --weak_count_;
125 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
126
127 if( new_weak_count == 0 )
128 {
129 destroy();
130 }
131 }
132
133 long use_count() const // nothrow
134 {
135 BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 );
136 boost::int_least32_t r = use_count_;
137 BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 );
138
139 return r;
140 }
141 };
142
143 } // namespace detail
144
145 } // namespace boost
146
147 #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED