1 | #ifndef BOOST_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED
|
---|
2 | #define BOOST_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED
|
---|
3 |
|
---|
4 | //#warning "Using experimental atomic shared pointer"
|
---|
5 |
|
---|
6 | // MS compatible compilers support #pragma once
|
---|
7 |
|
---|
8 | #if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
---|
9 | # pragma once
|
---|
10 | #endif
|
---|
11 |
|
---|
12 | //
|
---|
13 | // detail/sp_counted_base_atomic.hpp
|
---|
14 | //
|
---|
15 | // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
|
---|
16 | // Copyright 2004-2005 Peter Dimov
|
---|
17 | // Copyright 2009 Philip Endecott
|
---|
18 | //
|
---|
19 | // Distributed under the Boost Software License, Version 1.0. (See
|
---|
20 | // accompanying file LICENSE_1_0.txt or copy at
|
---|
21 | // http://www.boost.org/LICENSE_1_0.txt)
|
---|
22 | //
|
---|
23 |
|
---|
24 | #include <boost/detail/sp_typeinfo.hpp>
|
---|
25 | #include <boost/atomic.hpp>
|
---|
26 |
|
---|
27 |
|
---|
28 | namespace boost
|
---|
29 | {
|
---|
30 |
|
---|
31 | namespace detail
|
---|
32 | {
|
---|
33 |
|
---|
34 | class sp_counted_base
|
---|
35 | {
|
---|
36 | private:
|
---|
37 |
|
---|
38 | sp_counted_base( sp_counted_base const & );
|
---|
39 | sp_counted_base & operator= ( sp_counted_base const & );
|
---|
40 |
|
---|
41 | boost::atomic<long> use_count_; // #shared
|
---|
42 | boost::atomic<long> weak_count_; // #weak + (#shared != 0)
|
---|
43 |
|
---|
44 | public:
|
---|
45 |
|
---|
46 | sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
|
---|
47 | {
|
---|
48 | }
|
---|
49 |
|
---|
50 | virtual ~sp_counted_base() // nothrow
|
---|
51 | {
|
---|
52 | }
|
---|
53 |
|
---|
54 | // dispose() is called when use_count_ drops to zero, to release
|
---|
55 | // the resources managed by *this.
|
---|
56 |
|
---|
57 | virtual void dispose() = 0; // nothrow
|
---|
58 |
|
---|
59 | // destroy() is called when weak_count_ drops to zero.
|
---|
60 |
|
---|
61 | virtual void destroy() // nothrow
|
---|
62 | {
|
---|
63 | delete this;
|
---|
64 | }
|
---|
65 |
|
---|
66 | virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
|
---|
67 |
|
---|
68 | void add_ref_copy()
|
---|
69 | {
|
---|
70 | ++use_count_;
|
---|
71 | }
|
---|
72 |
|
---|
73 | bool add_ref_lock() // true on success
|
---|
74 | {
|
---|
75 | while (1) {
|
---|
76 | long c = use_count_;
|
---|
77 | if (c == 0) return false;
|
---|
78 | if (use_count_.compare_exchange_weak(c,c+1)) return true;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | void release() // nothrow
|
---|
83 | {
|
---|
84 | if( --use_count_ == 0 )
|
---|
85 | {
|
---|
86 | dispose();
|
---|
87 | weak_release();
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | void weak_add_ref() // nothrow
|
---|
92 | {
|
---|
93 | ++weak_count_;
|
---|
94 | }
|
---|
95 |
|
---|
96 | void weak_release() // nothrow
|
---|
97 | {
|
---|
98 | if( --weak_count_ == 0 )
|
---|
99 | {
|
---|
100 | destroy();
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | long use_count() const // nothrow
|
---|
105 | {
|
---|
106 | return use_count_;
|
---|
107 | }
|
---|
108 | };
|
---|
109 |
|
---|
110 | } // namespace detail
|
---|
111 |
|
---|
112 | } // namespace boost
|
---|
113 |
|
---|
114 | #endif // #ifndef BOOST_DETAIL_SP_COUNTED_BASE_ATOMIC_HPP_INCLUDED
|
---|