2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CDSLIB_URCU_EXEMPT_PTR_H
32 #define CDSLIB_URCU_EXEMPT_PTR_H
34 #include <type_traits>
35 #include <cds/details/defs.h>
37 namespace cds { namespace urcu {
41 template <typename Node, typename Value>
42 struct conventional_exempt_member_cast
44 Value * operator()( Node * p ) const
50 template <typename Node, typename Value>
51 struct conventional_exempt_pair_cast
53 Value * operator()( Node * p ) const
58 } // namespace details
61 /// Exempt pointer for RCU
63 This special pointer class is intended for returning extracted node from RCU-based container.
64 The destructor (and \p release() member function) invokes <tt>RCU::retire_ptr< Disposer >()</tt> function to dispose the node.
65 For non-intrusive containers from \p cds::container namespace \p Disposer is usually an invocation
66 of node deallocator. For intrusive containers the disposer can be empty or it can trigger an event "node can be reused safely".
67 In any case, the exempt pointer concept keeps RCU semantics.
68 The destructor or \p release() should be called outside the RCU lock of current thread.
70 You don't need use this helper class directly. Any RCU-based container typedefs a simplified version of this template.
73 - \p RCU - one of \ref cds_urcu_gc "RCU type"
74 - \p NodeType - container's node type
75 - \p ValueType - value type stored in container's node. For intrusive containers it is the same as \p NodeType
76 - \p Disposer - a disposer functor
77 - \p Cast - a functor for casting from \p NodeType to \p ValueType. For intrusive containers
78 the casting is usually disabled, i.e. \p Cast is \p void.
85 #ifdef CDS_DOXYGEN_INVOKED
88 typename Cast=details::conventional_exempt_member_cast<NodeType, ValueType>
95 ValueType * operator()( NodeType * p ) const
102 typedef RCU rcu ; ///< RCU type - one of <tt>cds::urcu::gc< ... ></tt>
103 typedef NodeType node_type ; ///< Node type
104 typedef ValueType value_type ; ///< Value type
105 typedef Disposer disposer ; ///< Disposer calling when release
106 /// Functor converting \p node_type to \p value_type
107 typedef typename std::conditional< std::is_same< Cast, void >::value, trivial_cast, Cast>::type node_to_value_cast;
115 /// Constructs empty pointer
116 exempt_ptr() CDS_NOEXCEPT
121 /// Creates exempt pointer for \p pNode. Only for internal use.
122 explicit exempt_ptr( node_type * pNode ) CDS_NOEXCEPT
125 explicit exempt_ptr( std::nullptr_t ) CDS_NOEXCEPT
131 exempt_ptr( exempt_ptr&& p ) CDS_NOEXCEPT
132 : m_pNode( p.m_pNode )
137 /// The exempt pointer is not copy-constructible
138 exempt_ptr( exempt_ptr const& ) = delete;
140 /// Releases the pointer, see \p release()
146 /// Checks if the pointer is \p nullptr
147 bool empty() const CDS_NOEXCEPT
149 return m_pNode == nullptr;
152 /// \p bool operator returns <tt>!empty()</tt>
153 explicit operator bool() const CDS_NOEXCEPT
158 /// Dereference operator
159 value_type * operator->() const CDS_NOEXCEPT
161 return !empty() ? node_to_value_cast()(m_pNode) : nullptr;
164 /// Returns a reference to the value
165 value_type& operator *() CDS_NOEXCEPT
168 return *node_to_value_cast()( m_pNode );
171 /// Move assignment. Can be called only outside of RCU critical section
172 exempt_ptr& operator =( exempt_ptr&& p ) CDS_NOEXCEPT
180 /// The exempt pointer is not copy-assignable
181 exempt_ptr& operator=(exempt_ptr const&) = delete;
183 /// Disposes the pointer. Should be called only outside of RCU critical section
187 assert( !rcu::is_locked() );
188 rcu::template retire_ptr<disposer>( m_pNode );
193 }} // namespace cds::urcu
195 #endif //#ifndef CDSLIB_URCU_EXEMPT_PTR_H