Merged branch 'master' of https://github.com/Nemo1369/libcds
[libcds.git] / test / stress / stack / intrusive_push_pop.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 "intrusive_stack_push_pop.h"
32
33 namespace cds_test {
34     /*static*/ size_t intrusive_stack_push_pop::s_nPushThreadCount = 4;
35     /*static*/ size_t intrusive_stack_push_pop::s_nPopThreadCount = 4;
36     /*static*/ size_t intrusive_stack_push_pop::s_nStackSize = 10000000;
37     /*static*/ size_t intrusive_stack_push_pop::s_nEliminationSize = 4;
38     /*static*/ bool intrusive_stack_push_pop::s_bFCIterative = false;
39     /*static*/ unsigned int intrusive_stack_push_pop::s_nFCCombinePassCount = 64;
40     /*static*/ unsigned int intrusive_stack_push_pop::s_nFCCompactFactor = 1024;
41
42     /*static*/ atomics::atomic<size_t> intrusive_stack_push_pop::s_nWorkingProducers( 0 );
43
44     /*static*/ void intrusive_stack_push_pop::SetUpTestCase()
45     {
46         cds_test::config const& cfg = get_config( "IntrusiveStack_PushPop" );
47
48         s_nPushThreadCount = cfg.get_size_t( "PushThreadCount", s_nPushThreadCount );
49         s_nPopThreadCount = cfg.get_size_t( "PopThreadCount", s_nPopThreadCount );
50         s_nStackSize = cfg.get_size_t( "StackSize", s_nStackSize );
51         s_nEliminationSize = cfg.get_size_t( "EliminationSize", s_nEliminationSize );
52         s_bFCIterative = cfg.get_bool( "FCIterate", s_bFCIterative );
53         s_nFCCombinePassCount = cfg.get_uint( "FCCombinePassCount", s_nFCCombinePassCount );
54         s_nFCCompactFactor = cfg.get_uint( "FCCompactFactor", s_nFCCompactFactor );
55
56         if ( s_nPushThreadCount == 0 )
57             s_nPushThreadCount = 1;
58         if ( s_nPopThreadCount == 0 )
59             s_nPopThreadCount = 1;
60         if ( s_nEliminationSize == 0 )
61             s_nEliminationSize = 1;
62     }
63 } // namespace cds_test
64
65 namespace {
66     class intrusive_stack_push_pop : public cds_test::intrusive_stack_push_pop
67     {
68         typedef cds_test::intrusive_stack_push_pop base_class;
69     protected:
70         typedef base_class::value_type<> value_type;
71         typedef base_class::value_type< cds::intrusive::treiber_stack::node< cds::gc::HP >> hp_value_type;
72         typedef base_class::value_type< cds::intrusive::treiber_stack::node< cds::gc::DHP >> dhp_value_type;
73
74         template <typename Stack>
75         void test()
76         {
77             value_array<typename Stack::value_type> arrValue( s_nStackSize );
78             {
79                 Stack stack;
80                 do_test( stack, arrValue );
81             }
82             Stack::gc::force_dispose();
83         }
84
85         void check_elimination_stat( cds::intrusive::treiber_stack::empty_stat const& )
86         {}
87
88         void check_elimination_stat( cds::intrusive::treiber_stack::stat<> const& s )
89         {
90             EXPECT_EQ( s.m_PushCount.get() + s.m_ActivePushCollision.get() + s.m_PassivePushCollision.get(), s_nStackSize );
91             EXPECT_EQ( s.m_PopCount.get() + s.m_ActivePopCollision.get() + s.m_PassivePopCollision.get(), s_nStackSize );
92             EXPECT_EQ( s.m_PushCount.get(), s.m_PopCount.get());
93             EXPECT_EQ( s.m_ActivePopCollision.get(), s.m_PassivePushCollision.get());
94             EXPECT_EQ( s.m_ActivePushCollision.get(), s.m_PassivePopCollision.get());
95         }
96
97         template <typename Stack>
98         void test_elimination()
99         {
100             value_array<typename Stack::value_type> arrValue( s_nStackSize );
101             {
102                 Stack stack( s_nEliminationSize );
103                 do_test( stack, arrValue );
104                 check_elimination_stat( stack.statistics());
105             }
106             Stack::gc::force_dispose();
107         }
108
109         template <typename Stack>
110         void test_std()
111         {
112             value_array<typename Stack::value_type> arrValue( s_nStackSize );
113             Stack stack;
114             do_test( stack, arrValue );
115         }
116     };
117
118     // TreiberStack<cds::gc::HP>
119 #define CDSSTRESS_Stack_F( test_fixture, stack_impl ) \
120     TEST_F( test_fixture, stack_impl ) \
121     { \
122         typedef typename istack::Types<hp_value_type>::stack_impl stack_type; \
123         test< stack_type >(); \
124     }
125
126     CDSSTRESS_TreiberStack_HP( intrusive_stack_push_pop )
127
128 #undef CDSSTRESS_Stack_F
129
130     // TreiberStack<cds::gc::DHP>
131 #define CDSSTRESS_Stack_F( test_fixture, stack_impl ) \
132     TEST_F( test_fixture, stack_impl ) \
133     { \
134         typedef typename istack::Types<dhp_value_type>::stack_impl stack_type; \
135         test< stack_type >(); \
136     }
137
138     CDSSTRESS_TreiberStack_DHP( intrusive_stack_push_pop )
139
140 #undef CDSSTRESS_Stack_F
141
142     // TreiberStack<cds::gc::HP> + elimination enabled
143 #define CDSSTRESS_Stack_F( test_fixture, stack_impl ) \
144     TEST_F( test_fixture, stack_impl ) \
145     { \
146         typedef typename istack::Types<hp_value_type>::stack_impl stack_type; \
147         test_elimination< stack_type >(); \
148     }
149
150     CDSSTRESS_EliminationStack_HP( intrusive_stack_push_pop )
151
152 #undef CDSSTRESS_Stack_F
153
154
155     // TreiberStack<cds::gc::DHP> + elimination enabled
156 #define CDSSTRESS_Stack_F( test_fixture, stack_impl ) \
157     TEST_F( test_fixture, stack_impl ) \
158     { \
159         typedef typename istack::Types<dhp_value_type>::stack_impl stack_type; \
160         test_elimination< stack_type >(); \
161     }
162
163     CDSSTRESS_EliminationStack_DHP( intrusive_stack_push_pop )
164
165 #undef CDSSTRESS_Stack_F
166
167
168     // StdStack
169 #define CDSSTRESS_Stack_F( test_fixture, stack_impl ) \
170     TEST_F( test_fixture, stack_impl ) \
171     { \
172         typedef typename istack::Types<value_type>::stack_impl stack_type; \
173         test_std< stack_type >(); \
174     }
175
176     CDSSTRESS_StdStack( intrusive_stack_push_pop )
177
178 #undef CDSSTRESS_Stack_F
179
180     //INSTANTIATE_TEST_CASE_P( a, intrusive_stack_push_pop, ::testing::Values(1));
181
182 } // namespace