IterableList: fixed a complex bug that can be called "ABA problem of null pointer"
[libcds.git] / cds / container / details / make_split_list_set_iterable_list.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.
29 */
30
31 #ifndef CDSLIB_CONTAINER_DETAILS_MAKE_SPLIT_LIST_SET_ITERABLE_LIST_H
32 #define CDSLIB_CONTAINER_DETAILS_MAKE_SPLIT_LIST_SET_ITERABLE_LIST_H
33
34 //@cond
35 namespace cds { namespace container { namespace details {
36
37     template <typename GC, typename T, typename Traits>
38     struct make_split_list_set< GC, T, iterable_list_tag, Traits >
39     {
40         typedef GC      gc;
41         typedef T       value_type;
42         typedef Traits  original_traits;
43
44         typedef typename cds::opt::select_default<
45             typename original_traits::ordered_list_traits,
46             cds::container::iterable_list::traits
47         >::type original_ordered_list_traits;
48
49         struct node_type: public cds::intrusive::split_list::node< void >
50         {
51             value_type  m_Value;
52
53             template <typename Q>
54             explicit node_type( Q&& v )
55                 : m_Value( std::forward<Q>( v ))
56             {}
57
58             template <typename Q, typename... Args>
59             explicit node_type( Q&& q, Args&&... args )
60                 : m_Value( std::forward<Q>(q), std::forward<Args>(args)... )
61             {}
62
63             node_type() = delete;
64         };
65
66         typedef typename cds::opt::select_default<
67             typename original_traits::ordered_list_traits,
68             typename original_traits::allocator,
69             typename cds::opt::select_default<
70                 typename original_traits::ordered_list_traits::allocator,
71                 typename original_traits::allocator
72             >::type
73         >::type node_allocator_;
74
75         typedef typename node_allocator_::template rebind<node_type>::other node_allocator_type;
76
77         typedef cds::details::Allocator< node_type, node_allocator_type > cxx_node_allocator;
78         struct node_deallocator
79         {
80             void operator ()( node_type * pNode )
81             {
82                 cxx_node_allocator().Delete( pNode );
83             }
84         };
85
86         typedef typename opt::details::make_comparator< value_type, original_ordered_list_traits >::type key_comparator;
87
88         typedef typename original_traits::key_accessor key_accessor;
89
90         struct value_accessor
91         {
92             typename key_accessor::key_type const& operator()( node_type const& node ) const
93             {
94                 return key_accessor()(node.m_Value);
95             }
96         };
97
98         template <typename Predicate>
99         struct predicate_wrapper {
100             typedef cds::details::predicate_wrapper< node_type, Predicate, value_accessor > type;
101         };
102
103         struct ordered_list_traits: public original_ordered_list_traits
104         {
105             typedef cds::atomicity::empty_item_counter item_counter;
106             typedef node_deallocator disposer;
107             typedef cds::details::compare_wrapper< node_type, key_comparator, value_accessor > compare;
108         };
109
110         struct traits: public original_traits
111         {
112             struct hash: public original_traits::hash
113             {
114                 typedef typename original_traits::hash  base_class;
115
116                 size_t operator()(node_type const& v ) const
117                 {
118                     return base_class::operator()( key_accessor()( v.m_Value ));
119                 }
120
121                 template <typename Q>
122                 size_t operator()( Q const& k ) const
123                 {
124                     return base_class::operator()( k );
125                 }
126             };
127         };
128
129         class ordered_list: public cds::intrusive::IterableList< gc, node_type, ordered_list_traits >
130         {};
131
132         typedef cds::intrusive::SplitListSet< gc, ordered_list, traits > type;
133     };
134
135 }}}  // namespace cds::container::details
136 //@endcond
137
138 #endif // #ifndef CDSLIB_CONTAINER_DETAILS_MAKE_SPLIT_LIST_SET_ITERABLE_LIST_H