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