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_INTRUSIVE_DETAILS_LAZY_LIST_BASE_H
32 #define CDSLIB_INTRUSIVE_DETAILS_LAZY_LIST_BASE_H
34 #include <cds/intrusive/details/base.h>
35 #include <cds/opt/compare.h>
36 #include <cds/details/marked_ptr.h>
37 #include <cds/details/make_const_type.h>
38 #include <cds/sync/spinlock.h>
39 #include <cds/urcu/options.h>
41 namespace cds { namespace intrusive {
43 /// LazyList ordered list related definitions
44 /** @ingroup cds_intrusive_helper
50 - GC - garbage collector
51 - Lock - lock type. Default is \p cds::sync::spin
52 - Tag - a \ref cds_intrusive_hook_tag "tag"
56 ,typename Lock = cds::sync::spin
57 ,typename Tag = opt::none
61 typedef GC gc ; ///< Garbage collector
62 typedef Lock lock_type ; ///< Lock type
63 typedef Tag tag ; ///< tag
65 typedef cds::details::marked_ptr<node, 1> marked_ptr ; ///< marked pointer
66 typedef typename gc::template atomic_marked_ptr< marked_ptr> atomic_marked_ptr ; ///< atomic marked pointer specific for GC
68 atomic_marked_ptr m_pNext; ///< pointer to the next node in the list + logical deletion mark
69 mutable lock_type m_Lock; ///< Node lock
71 /// Checks if node is marked
72 bool is_marked() const
74 return m_pNext.load(atomics::memory_order_relaxed).bits() != 0;
84 template <typename GC, typename Node, typename MemoryModel>
86 void operator()( Node * p )
88 typedef typename Node::marked_ptr marked_ptr;
89 p->m_pNext.store( marked_ptr(), MemoryModel::memory_order_release );
97 typedef undefined_gc gc;
98 typedef opt::none tag;
99 typedef sync::spin lock_type;
104 template < typename HookType, typename... Options>
107 typedef typename opt::make_options< default_hook, Options...>::type options;
108 typedef typename options::gc gc;
109 typedef typename options::tag tag;
110 typedef typename options::lock_type lock_type;
111 typedef node<gc, lock_type, tag> node_type;
112 typedef HookType hook_type;
119 - opt::gc - garbage collector
120 - opt::lock_type - lock type used for node locking. Default is sync::spin
121 - opt::tag - a \ref cds_intrusive_hook_tag "tag"
123 template < typename... Options >
124 struct base_hook: public hook< opt::base_hook_tag, Options... >
129 \p MemberOffset defines offset in bytes of \ref node member into your structure.
130 Use \p offsetof macro to define \p MemberOffset
133 - opt::gc - garbage collector
134 - opt::lock_type - lock type used for node locking. Default is sync::spin
135 - opt::tag - a \ref cds_intrusive_hook_tag "tag"
137 template < size_t MemberOffset, typename... Options >
138 struct member_hook: public hook< opt::member_hook_tag, Options... >
141 static const size_t c_nMemberOffset = MemberOffset;
147 \p NodeTraits defines type traits for node.
148 See \ref node_traits for \p NodeTraits interface description
151 - opt::gc - garbage collector used.
152 - opt::lock_type - lock type used for node locking. Default is sync::spin
153 - opt::tag - a \ref cds_intrusive_hook_tag "tag"
155 template <typename NodeTraits, typename... Options >
156 struct traits_hook: public hook< opt::traits_hook_tag, Options... >
159 typedef NodeTraits node_traits;
164 template <typename Node>
168 typedef Node node_type;
171 /// Checks if the link field of node \p pNode is \p nullptr
173 An asserting is generated if \p pNode link field is not \p nullptr
175 static void is_empty( node_type const * pNode )
177 assert( pNode->m_pNext.load( atomics::memory_order_relaxed ) == nullptr );
183 template <class GC, typename Node, opt::link_check_type LinkType >
184 struct link_checker_selector;
186 template <typename GC, typename Node>
187 struct link_checker_selector< GC, Node, opt::never_check_link >
189 typedef intrusive::opt::v::empty_link_checker<Node> type;
192 template <typename GC, typename Node>
193 struct link_checker_selector< GC, Node, opt::debug_check_link >
196 typedef link_checker<Node> type;
198 typedef intrusive::opt::v::empty_link_checker<Node> type;
202 template <typename GC, typename Node>
203 struct link_checker_selector< GC, Node, opt::always_check_link >
205 typedef link_checker<Node> type;
209 /// Metafunction for selecting appropriate link checking policy
210 template < typename Node, opt::link_check_type LinkType >
211 struct get_link_checker
214 typedef typename link_checker_selector< typename Node::gc, Node, LinkType>::type type;
223 Possible values are: \p lazy_list::base_hook, \p lazy_list::member_hook, \p lazy_list::traits_hook.
225 typedef base_hook<> hook;
227 /// Key comparing functor
229 No default functor is provided. If the functor is not specified, the \p less is used.
231 typedef opt::none compare;
233 /// Specifies binary predicate used for comparing keys
235 Default is \p std::less<T>.
237 typedef opt::none less;
239 /// Specifies binary functor used for comparing keys for equality (for unordered list only)
241 If \p equal_to option is not specified, \p compare is used, if \p compare is not specified, \p less is used,
242 if \p less is not specified, then \p std::equal_to<T> is used.
244 typedef opt::none equal_to;
246 /// Specifies list ordering policy
248 If \p sort is \p true, than list maintains items in sorted order, otherwise the list is unordered.
250 Note that if \p sort is \p false, than lookup operations scan entire list.
252 static const bool sort = true;
254 /// Back-off strategy
255 typedef cds::backoff::Default back_off;
257 /// Disposer for removing items
258 typedef opt::v::empty_disposer disposer;
260 /// Item counting feature; by default, disabled. Use \p cds::atomicity::item_counter to enable item counting
261 typedef atomicity::empty_item_counter item_counter;
263 /// Link fields checking feature
265 Default is \p opt::debug_check_link
267 static const opt::link_check_type link_checker = opt::debug_check_link;
269 /// C++ memory ordering model
271 Can be \p opt::v::relaxed_ordering (relaxed memory model, the default)
272 or \p opt::v::sequential_consistent (sequentially consisnent memory model).
274 typedef opt::v::relaxed_ordering memory_model;
276 /// RCU deadlock checking policy (only for \ref cds_intrusive_LazyList_rcu "RCU-based LazyList")
278 List of available options see \p opt::rcu_check_deadlock
280 typedef opt::v::rcu_throw_deadlock rcu_check_deadlock;
283 /// Metafunction converting option list to \p lazy_list::traits
285 Supported \p Options are:
286 - \p opt::hook - hook used. Possible values are: \p lazy_list::base_hook, \p lazy_list::member_hook, \p lazy_list::traits_hook.
287 If the option is not specified, \p %lazy_list::base_hook and \p gc::HP is used.
288 - \p opt::compare - key comparison functor. No default functor is provided.
289 If the option is not specified, the \p opt::less is used.
290 - \p opt::less - specifies binary predicate used for key comparison. Default is \p std::less<T>.
291 - \p opt::equal_to - specifies binary functor for comparing keys for equality. This option is applicable only for unordered list.
292 If \p equal_to is not specified, \p compare is used, \p compare is not specified, \p less is used.
293 - \p opt::sort - specifies ordering policy. Default value is \p true, i.e. the list is ordered.
294 Note: unordering feature is not fully supported yet.
295 - \p opt::back_off - back-off strategy used. If the option is not specified, the \p cds::backoff::Default is used.
296 - \p opt::disposer - the functor used for dispose removed items. Default is \p opt::v::empty_disposer. Due the nature
297 of GC schema the disposer may be called asynchronously.
298 - \p opt::link_checker - the type of node's link fields checking. Default is \p opt::debug_check_link
299 - \p opt::item_counter - the type of item counting feature. Default is disabled (\p atomicity::empty_item_counter).
300 To enable item counting use \p atomicity::item_counter.
301 - \p opt::memory_model - C++ memory ordering model. Can be \p opt::v::relaxed_ordering (relaxed memory model, the default)
302 or \p opt::v::sequential_consistent (sequentially consisnent memory model).
303 - \p opt::rcu_check_deadlock - a deadlock checking policy for \ref cds_intrusive_MichaelList_rcu "RCU-based MichaelList"
304 Default is \p opt::v::rcu_throw_deadlock
306 template <typename... Options>
308 # ifdef CDS_DOXYGEN_INVOKED
309 typedef implementation_defined type ; ///< Metafunction result
311 typedef typename cds::opt::make_options<
312 typename cds::opt::find_type_traits< traits, Options... >::type
318 } // namespace lazy_list
321 // Forward declaration
322 template < class GC, typename T, class Traits = lazy_list::traits >
326 }} // namespace cds::intrusive
328 #endif // #ifndef CDSLIB_INTRUSIVE_DETAILS_LAZY_LIST_BASE_H