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