fixed adding file problem
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / dependencies / libcds-2.3.2 / test / stress / freelist / put_get.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: public cds_test::stress_fixture
41     {
42     protected:
43         static size_t s_nThreadCount;
44         static size_t s_nPassCount;
45         static size_t const c_nArraySize = 100;
46
47         template <typename FreeList >
48         struct value_type: public FreeList::node
49         {
50             size_t  counter;
51
52             value_type()
53                 : counter(0)
54             {}
55         };
56
57         template <class FreeList>
58         class Worker: public cds_test::thread
59         {
60             typedef cds_test::thread base_class;
61         public:
62             FreeList&           m_FreeList;
63             size_t              m_nSuccess = 0;
64
65         public:
66             Worker( cds_test::thread_pool& pool, FreeList& s )
67                 : base_class( pool )
68                 , m_FreeList( s )
69             {}
70
71             Worker( Worker& src )
72                 : base_class( src )
73                 , m_FreeList( src.m_FreeList )
74             {}
75
76             virtual thread * clone()
77             {
78                 return new Worker( *this );
79             }
80
81             virtual void test()
82             {
83                 typedef value_type<FreeList> item_type;
84                 item_type* arr[ c_nArraySize ];
85
86                 for ( size_t pass = 0; pass < s_nPassCount; ++pass ) {
87                     size_t n = 0;
88                     item_type* p;
89
90                     while ( (p = static_cast<item_type*>( m_FreeList.get())) != nullptr ) {
91                         CDS_TSAN_ANNOTATE_IGNORE_RW_BEGIN;
92                         p->counter++;
93                         CDS_TSAN_ANNOTATE_IGNORE_RW_END;
94                         arr[n] = p;
95                         ++m_nSuccess;
96                         ++n;
97                     }
98
99                     for ( size_t i = 0; i < n; ++i )
100                         m_FreeList.put( arr[i] );
101                 }
102             }
103         };
104
105     public:
106         static void SetUpTestCase()
107         {
108             cds_test::config const& cfg = get_config( "free_list" );
109
110             s_nThreadCount = cfg.get_size_t( "ThreadCount", s_nThreadCount );
111             s_nPassCount = cfg.get_size_t( "PassCount", s_nPassCount );
112
113             if ( s_nThreadCount == 0 )
114                 s_nThreadCount = 1;
115             if ( s_nPassCount == 0 )
116                 s_nPassCount = 1000;
117         }
118         //static void TearDownTestCase();
119
120     protected:
121
122         template <typename FreeList>
123         void test( FreeList& list )
124         {
125             cds_test::thread_pool& pool = get_pool();
126
127             value_type<FreeList> arr[c_nArraySize];
128
129             for ( auto& i : arr )
130                 list.put( &i );
131
132             pool.add( new Worker<FreeList>( pool, list ), s_nThreadCount );
133
134             propout() << std::make_pair( "work_thread", s_nThreadCount )
135                       << std::make_pair( "pass_count", s_nPassCount );
136
137             std::chrono::milliseconds duration = pool.run();
138
139             propout() << std::make_pair( "duration", duration );
140
141             // analyze result
142             size_t nTotal = 0;
143             for ( auto const& i : arr )
144                 nTotal += i.counter;
145
146             size_t nSuccess = 0;
147             for ( size_t threadNo = 0; threadNo < pool.size(); ++threadNo )
148                 nSuccess += static_cast<Worker<FreeList>&>( pool.get( threadNo )).m_nSuccess;
149
150             EXPECT_EQ( nSuccess, nTotal );
151
152             list.clear( []( typename FreeList::node* ) {} );
153         }
154     };
155
156     size_t put_get::s_nThreadCount = 4;
157     size_t put_get::s_nPassCount = 100000;
158
159 #define CDSSTRESS_FREELIST_F( name, freelist_type ) \
160     TEST_F( put_get, name ) \
161     { \
162         freelist_type fl; \
163         test( fl ); \
164     }
165
166     CDSSTRESS_FREELIST_F( FreeList, cds::intrusive::FreeList )
167
168     typedef cds::intrusive::CachedFreeList<cds::intrusive::FreeList> cached_free_list;
169     CDSSTRESS_FREELIST_F( CachedFreeList, cached_free_list )
170
171 #ifdef CDS_DCAS_SUPPORT
172     TEST_F( put_get, TaggetFreeList )
173     {
174         struct tagged_ptr {
175             void* p;
176             uintptr_t tag;
177         };
178
179         atomics::atomic<tagged_ptr> tp;
180         if ( tp.is_lock_free()) {
181             cds::intrusive::TaggedFreeList fl;
182             test( fl );
183         }
184         else
185             std::cout << "Double-width CAS is not supported\n";
186     }
187 #endif
188
189 } // namespace