HP and DHP SMR totally refactored
[libcds.git] / test / unit / queue / intrusive_segmented_queue_dhp.cpp
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
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 #include "test_intrusive_segmented_queue.h"
32
33 #include <cds/gc/dhp.h>
34 #include <cds/intrusive/segmented_queue.h>
35 #include <vector>
36
37 namespace {
38     namespace ci = cds::intrusive;
39     typedef cds::gc::DHP gc_type;
40
41     class IntrusiveSegmentedQueue_DHP : public cds_test::intrusive_segmented_queue
42     {
43         typedef cds_test::intrusive_segmented_queue base_class;
44
45     protected:
46         static const size_t c_QuasiFactor = 15;
47
48         void SetUp()
49         {
50             typedef ci::SegmentedQueue< gc_type, item > queue_type;
51
52             cds::gc::dhp::smr::construct( queue_type::c_nHazardPtrCount );
53             cds::threading::Manager::attachThread();
54         }
55
56         void TearDown()
57         {
58             cds::threading::Manager::detachThread();
59             cds::gc::dhp::smr::destruct();
60         }
61
62         template <typename V>
63         void check_array( V& arr )
64         {
65             for ( size_t i = 0; i < arr.size(); ++i ) {
66                 EXPECT_EQ( arr[i].nDisposeCount, 2u );
67                 EXPECT_EQ( arr[i].nDispose2Count, 1u );
68             }
69         }
70     };
71
72     TEST_F( IntrusiveSegmentedQueue_DHP, defaulted )
73     {
74         struct queue_traits : public cds::intrusive::segmented_queue::traits
75         {
76             typedef Disposer disposer;
77         };
78         typedef cds::intrusive::SegmentedQueue< gc_type, item, queue_traits > queue_type;
79
80         std::vector<typename queue_type::value_type> arr;
81         {
82             queue_type q( c_QuasiFactor );
83             test( q, arr );
84         }
85         queue_type::gc::force_dispose();
86         check_array( arr );
87     }
88
89     TEST_F( IntrusiveSegmentedQueue_DHP, mutex )
90     {
91         struct queue_traits : public
92             cds::intrusive::segmented_queue::make_traits <
93                 cds::intrusive::opt::disposer< Disposer >
94                 ,cds::opt::lock_type < std::mutex >
95             > ::type
96         {};
97         typedef cds::intrusive::SegmentedQueue< gc_type, item, queue_traits > queue_type;
98
99         std::vector<typename queue_type::value_type> arr;
100         {
101             queue_type q( c_QuasiFactor );
102             test( q, arr );
103         }
104         queue_type::gc::force_dispose();
105         check_array( arr );
106     }
107
108     TEST_F( IntrusiveSegmentedQueue_DHP, shuffle )
109     {
110         typedef cds::intrusive::SegmentedQueue< gc_type, item,
111             cds::intrusive::segmented_queue::make_traits<
112                 cds::intrusive::opt::disposer< Disposer >
113                 ,cds::opt::item_counter< cds::atomicity::item_counter >
114                 ,cds::opt::permutation_generator< cds::opt::v::random_shuffle_permutation<> >
115             >::type
116         > queue_type;
117
118         std::vector<typename queue_type::value_type> arr;
119         {
120             queue_type q( c_QuasiFactor );
121             test( q, arr );
122         }
123         queue_type::gc::force_dispose();
124         check_array( arr );
125     }
126
127     TEST_F( IntrusiveSegmentedQueue_DHP, padding )
128     {
129         struct queue_traits : public cds::intrusive::segmented_queue::traits
130         {
131             typedef Disposer disposer;
132             enum { padding = cds::opt::cache_line_padding };
133             typedef ci::segmented_queue::stat<> stat;
134         };
135         typedef cds::intrusive::SegmentedQueue< gc_type, item, queue_traits > queue_type;
136
137         std::vector<typename queue_type::value_type> arr;
138         {
139             queue_type q( c_QuasiFactor );
140             test( q, arr );
141         }
142         queue_type::gc::force_dispose();
143         check_array( arr );
144     }
145
146     TEST_F( IntrusiveSegmentedQueue_DHP, bigdata_padding )
147     {
148         struct queue_traits : public cds::intrusive::segmented_queue::traits
149         {
150             typedef Disposer disposer;
151             enum { padding = cds::opt::cache_line_padding | cds::opt::padding_tiny_data_only };
152             typedef cds::opt::v::sequential_consistent memory_model;
153         };
154         typedef cds::intrusive::SegmentedQueue< gc_type, big_item, queue_traits > queue_type;
155
156         std::vector<typename queue_type::value_type> arr;
157         {
158             queue_type q( c_QuasiFactor );
159             test( q, arr );
160         }
161         queue_type::gc::force_dispose();
162         check_array( arr );
163     }
164
165 } // namespace
166