Merge branch 'flat_combinig_add_stress_and_unint_tests' of https://github.com/mgalimu...
[libcds.git] / test / stress / freelist / put_get_single.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 <cds_test/stress_test.h>
32
33 #include <cds/intrusive/free_list.h>
34 #include <cds/intrusive/free_list_cached.h>
35 #ifdef CDS_DCAS_SUPPORT
36 #   include <cds/intrusive/free_list_tagged.h>
37 #endif
38
39 namespace {
40     class put_get_single: public cds_test::stress_fixture
41     {
42     protected:
43         static size_t s_nThreadCount;
44         static size_t s_nPassCount;
45
46         template <typename FreeList >
47         struct value_type: public FreeList::node
48         {
49             atomics::atomic<size_t> counter;
50
51             value_type()
52                 : counter(0)
53             {}
54         };
55
56         template <class FreeList>
57         class Worker: public cds_test::thread
58         {
59             typedef cds_test::thread base_class;
60         public:
61             FreeList&           m_FreeList;
62             size_t              m_nSuccess = 0;
63
64         public:
65             Worker( cds_test::thread_pool& pool, FreeList& s )
66                 : base_class( pool )
67                 , m_FreeList( s )
68             {}
69
70             Worker( Worker& src )
71                 : base_class( src )
72                 , m_FreeList( src.m_FreeList )
73             {}
74
75             virtual thread * clone()
76             {
77                 return new Worker( *this );
78             }
79
80             virtual void test()
81             {
82                 typedef value_type<FreeList> item_type;
83
84                 for ( size_t pass = 0; pass < s_nPassCount; ++pass ) {
85                     item_type* p;
86                     while ( (p = static_cast<item_type*>( m_FreeList.get())) == nullptr );
87                     p->counter.fetch_add( 1, atomics::memory_order_relaxed );
88                     m_FreeList.put( p );
89                 }
90             }
91         };
92
93     public:
94         static void SetUpTestCase()
95         {
96             cds_test::config const& cfg = get_config( "free_list" );
97
98             s_nThreadCount = cfg.get_size_t( "ThreadCount", s_nThreadCount );
99             s_nPassCount = cfg.get_size_t( "PassCount", s_nPassCount );
100
101             if ( s_nThreadCount == 0 )
102                 s_nThreadCount = 1;
103             if ( s_nPassCount == 0 )
104                 s_nPassCount = 1000;
105         }
106         //static void TearDownTestCase();
107
108     protected:
109
110         template <typename FreeList>
111         void test( FreeList& list )
112         {
113             cds_test::thread_pool& pool = get_pool();
114
115             value_type<FreeList> item;;
116             list.put( &item );
117
118             pool.add( new Worker<FreeList>( pool, list ), s_nThreadCount );
119
120             propout() << std::make_pair( "work_thread", s_nThreadCount )
121                       << std::make_pair( "pass_count", s_nPassCount );
122
123             std::chrono::milliseconds duration = pool.run();
124
125             propout() << std::make_pair( "duration", duration );
126
127             // analyze result
128             EXPECT_EQ( item.counter.load( atomics::memory_order_relaxed ), s_nPassCount * s_nThreadCount );
129
130             list.clear( []( typename FreeList::node* ) {} );
131         }
132     };
133
134     size_t put_get_single::s_nThreadCount = 4;
135     size_t put_get_single::s_nPassCount = 100000;
136
137 #define CDSSTRESS_FREELIST_F( name, freelist_type ) \
138     TEST_F( put_get_single, name ) \
139     { \
140         freelist_type fl; \
141         test( fl ); \
142     }
143
144     CDSSTRESS_FREELIST_F( FreeList, cds::intrusive::FreeList )
145
146     typedef cds::intrusive::CachedFreeList<cds::intrusive::FreeList> cached_free_list;
147     CDSSTRESS_FREELIST_F( CachedFreeList, cached_free_list )
148
149 #ifdef CDS_DCAS_SUPPORT
150     TEST_F( put_get_single, TaggetFreeList )
151     {
152         struct tagged_ptr {
153             void* p;
154             uintptr_t tag;
155         };
156
157         atomics::atomic<tagged_ptr> tp;
158         if ( tp.is_lock_free()) {
159             cds::intrusive::TaggedFreeList fl;
160             test( fl );
161         }
162         else
163             std::cout << "Double-width CAS is not supported\n";
164     }
165 #endif
166
167 } // namespace