1#ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CLANG_HPP_INCLUDED
2#define BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CLANG_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// detail/sp_counted_base_clang.hpp - __c11 clang intrinsics
11//
12// Copyright (c) 2007, 2013, 2015 Peter Dimov
13//
14// Distributed under the Boost Software License, Version 1.0.
15// See accompanying file LICENSE_1_0.txt or copy at
16// http://www.boost.org/LICENSE_1_0.txt
17
18#include <boost/detail/sp_typeinfo.hpp>
19#include <boost/cstdint.hpp>
20
21namespace boost
22{
23
24namespace detail
25{
26
27typedef _Atomic( boost::int_least32_t ) atomic_int_least32_t;
28
29inline void atomic_increment( atomic_int_least32_t * pw )
30{
31 __c11_atomic_fetch_add( pw, 1, __ATOMIC_RELAXED );
32}
33
34inline boost::int_least32_t atomic_decrement( atomic_int_least32_t * pw )
35{
36 return __c11_atomic_fetch_sub( pw, 1, __ATOMIC_ACQ_REL );
37}
38
39inline boost::int_least32_t atomic_conditional_increment( atomic_int_least32_t * pw )
40{
41 // long r = *pw;
42 // if( r != 0 ) ++*pw;
43 // return r;
44
45 boost::int_least32_t r = __c11_atomic_load( pw, __ATOMIC_RELAXED );
46
47 for( ;; )
48 {
49 if( r == 0 )
50 {
51 return r;
52 }
53
54 if( __c11_atomic_compare_exchange_weak( pw, &r, r + 1, __ATOMIC_RELAXED, __ATOMIC_RELAXED ) )
55 {
56 return r;
57 }
58 }
59}
60
61class sp_counted_base
62{
63private:
64
65 sp_counted_base( sp_counted_base const & );
66 sp_counted_base & operator= ( sp_counted_base const & );
67
68 atomic_int_least32_t use_count_; // #shared
69 atomic_int_least32_t weak_count_; // #weak + (#shared != 0)
70
71public:
72
73 sp_counted_base()
74 {
75 __c11_atomic_init( &use_count_, 1 );
76 __c11_atomic_init( &weak_count_, 1 );
77 }
78
79 virtual ~sp_counted_base() // nothrow
80 {
81 }
82
83 // dispose() is called when use_count_ drops to zero, to release
84 // the resources managed by *this.
85
86 virtual void dispose() = 0; // nothrow
87
88 // destroy() is called when weak_count_ drops to zero.
89
90 virtual void destroy() // nothrow
91 {
92 delete this;
93 }
94
95 virtual void * get_deleter( sp_typeinfo const & ti ) = 0;
96 virtual void * get_untyped_deleter() = 0;
97
98 void add_ref_copy()
99 {
100 atomic_increment( &use_count_ );
101 }
102
103 bool add_ref_lock() // true on success
104 {
105 return atomic_conditional_increment( &use_count_ ) != 0;
106 }
107
108 void release() // nothrow
109 {
110 if( atomic_decrement( &use_count_ ) == 1 )
111 {
112 dispose();
113 weak_release();
114 }
115 }
116
117 void weak_add_ref() // nothrow
118 {
119 atomic_increment( &weak_count_ );
120 }
121
122 void weak_release() // nothrow
123 {
124 if( atomic_decrement( &weak_count_ ) == 1 )
125 {
126 destroy();
127 }
128 }
129
130 long use_count() const // nothrow
131 {
132 return __c11_atomic_load( const_cast< atomic_int_least32_t* >( &use_count_ ), __ATOMIC_ACQUIRE );
133 }
134};
135
136} // namespace detail
137
138} // namespace boost
139
140#endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_COUNTED_BASE_CLANG_HPP_INCLUDED
141