]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
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 | ||
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 | ||
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_untyped_deleter() = 0; | |
75 | ||
76 | void add_ref_copy() | |
77 | { | |
78 | BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); | |
79 | ++use_count_; | |
80 | BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); | |
81 | } | |
82 | ||
83 | bool add_ref_lock() // true on success | |
84 | { | |
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 ); | |
88 | return r; | |
89 | } | |
90 | ||
91 | void release() // nothrow | |
92 | { | |
93 | BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); | |
94 | long new_use_count = --use_count_; | |
95 | BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); | |
96 | ||
97 | if( new_use_count == 0 ) | |
98 | { | |
99 | dispose(); | |
100 | weak_release(); | |
101 | } | |
102 | } | |
103 | ||
104 | void weak_add_ref() // nothrow | |
105 | { | |
106 | BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); | |
107 | ++weak_count_; | |
108 | BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); | |
109 | } | |
110 | ||
111 | void weak_release() // nothrow | |
112 | { | |
113 | BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); | |
114 | long new_weak_count = --weak_count_; | |
115 | BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); | |
116 | ||
117 | if( new_weak_count == 0 ) | |
118 | { | |
119 | destroy(); | |
120 | } | |
121 | } | |
122 | ||
123 | long use_count() const // nothrow | |
124 | { | |
125 | BOOST_VERIFY( pthread_mutex_lock( &m_ ) == 0 ); | |
126 | long r = use_count_; | |
127 | BOOST_VERIFY( pthread_mutex_unlock( &m_ ) == 0 ); | |
128 | ||
129 | return r; | |
130 | } | |
131 | }; | |
132 | ||
133 | } // namespace detail | |
134 | ||
135 | } // namespace boost | |
136 | ||
137 | #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_PT_HPP_INCLUDED |