Removed unused vars
[libcds.git] / tests / unit / stack / stack_type.h
1 //$$CDS-header$$
2
3 #ifndef __CDSUNIT_STACK_TYPES_H
4 #define __CDSUNIT_STACK_TYPES_H
5
6 #include <cds/container/treiber_stack.h>
7 #include <cds/container/fcstack.h>
8 #include <cds/container/fcdeque.h>
9
10 #include <cds/gc/hp.h>
11 #include <cds/gc/dhp.h>
12
13 #include <mutex>
14 #include <cds/lock/spinlock.h>
15 #include <stack>
16 #include <list>
17 #include <vector>
18
19 namespace stack {
20
21     namespace details {
22
23         template <typename T, typename Traits=cds::container::fcdeque::traits>
24         class FCDequeL: public cds::container::FCDeque<T, std::deque<T>, Traits >
25         {
26             typedef cds::container::FCDeque<T, std::deque<T>, Traits > base_class;
27         public:
28             FCDequeL()
29             {}
30
31             FCDequeL(
32                 unsigned int nCompactFactor     ///< Flat combining: publication list compacting factor
33                 ,unsigned int nCombinePassCount ///< Flat combining: number of combining passes for combiner thread
34                 )
35                 : base_class( nCompactFactor, nCombinePassCount )
36             {}
37
38             bool push( T const& v )
39             {
40                 return base_class::push_front( v );
41             }
42
43             bool pop( T& v )
44             {
45                 return base_class::pop_front( v );
46             }
47         };
48
49         template <typename T, typename Traits=cds::container::fcdeque::traits>
50         class FCDequeR: public cds::container::FCDeque<T, std::deque<T>, Traits >
51         {
52             typedef cds::container::FCDeque<T, std::deque<T>, Traits > base_class;
53         public:
54             FCDequeR()
55             {}
56
57             FCDequeR(
58                 unsigned int nCompactFactor     ///< Flat combining: publication list compacting factor
59                 ,unsigned int nCombinePassCount ///< Flat combining: number of combining passes for combiner thread
60                 )
61                 : base_class( nCompactFactor, nCombinePassCount )
62             {}
63
64             bool push( T const& v )
65             {
66                 return base_class::push_back( v );
67             }
68
69             bool pop( T& v )
70             {
71                 return base_class::pop_back( v );
72             }
73         };
74
75         template < typename T, typename Stack, typename Lock>
76         class StdStack
77         {
78             Stack   m_Impl;
79             mutable Lock    m_Lock;
80             cds::container::treiber_stack::empty_stat m_stat;
81
82             typedef std::unique_lock<Lock>  unique_lock;
83
84         public:
85             bool push( T const& v )
86             {
87                 unique_lock l( m_Lock );
88                 m_Impl.push( v );
89                 return true;
90             }
91
92             bool pop( T& v )
93             {
94                 unique_lock l( m_Lock );
95                 if ( !m_Impl.empty() ) {
96                     v = m_Impl.top();
97                     m_Impl.pop();
98                     return true;
99                 }
100                 return false;
101             }
102
103             bool empty() const
104             {
105                 unique_lock l( m_Lock );
106                 return m_Impl.empty();
107             }
108
109             cds::container::treiber_stack::empty_stat const& statistics() const
110             {
111                 return m_stat;
112             }
113         };
114     }
115
116     template <typename T>
117     struct Types {
118
119     // TreiberStack
120         typedef cds::container::TreiberStack< cds::gc::HP,  T > Treiber_HP;
121         typedef cds::container::TreiberStack< cds::gc::DHP, T > Treiber_DHP;
122
123         struct traits_Treiber_seqcst: public
124             cds::container::treiber_stack::make_traits<
125                 cds::opt::memory_model<cds::opt::v::sequential_consistent>
126             >::type
127         {};
128         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Treiber_seqcst > Treiber_HP_seqcst;
129         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Treiber_seqcst > Treiber_DHP_seqcst;
130
131         struct traits_Treiber_stat: public
132             cds::container::treiber_stack::make_traits<
133                 cds::opt::stat<cds::intrusive::treiber_stack::stat<> >
134             >::type
135         {};
136         typedef cds::container::TreiberStack< cds::gc::HP, T, traits_Treiber_stat > Treiber_HP_stat;
137         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Treiber_stat > Treiber_DHP_stat;
138
139         struct traits_Treiber_yield: public
140             cds::container::treiber_stack::make_traits<
141                 cds::opt::back_off<cds::backoff::yield>
142                 , cds::opt::memory_model<cds::opt::v::relaxed_ordering>
143             >::type
144         {};
145         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Treiber_yield > Treiber_HP_yield;
146         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Treiber_yield > Treiber_DHP_yield;
147
148         struct traits_Treiber_pause: public
149             cds::container::treiber_stack::make_traits<
150                 cds::opt::back_off<cds::backoff::pause>
151             >::type
152         {};
153         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Treiber_pause > Treiber_HP_pause;
154         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Treiber_pause > Treiber_DHP_pause;
155
156         struct traits_Treiber_exp: public
157             cds::container::treiber_stack::make_traits<
158                 cds::opt::back_off<
159                     cds::backoff::exponential<
160                         cds::backoff::pause,
161                         cds::backoff::yield
162                     >
163                 >
164             >::type
165         {};
166         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Treiber_exp > Treiber_HP_exp;
167         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Treiber_exp > Treiber_DHP_exp;
168
169
170     // Elimination stack
171         struct traits_Elimination_on : public
172             cds::container::treiber_stack::make_traits <
173                 cds::opt::enable_elimination<true>
174             > ::type
175         {};
176         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_on > Elimination_HP;
177         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_on > Elimination_DHP;
178
179         struct traits_Elimination_stat : public
180             cds::container::treiber_stack::make_traits <
181                 cds::opt::enable_elimination<true>
182                 ,cds::opt::stat<cds::intrusive::treiber_stack::stat<> >
183             > ::type
184         {};
185         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_stat > Elimination_HP_stat;
186         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_stat > Elimination_DHP_stat;
187
188         struct traits_Elimination_2ms: public
189             cds::container::treiber_stack::make_traits <
190                 cds::opt::enable_elimination<true>
191                 ,cds::opt::elimination_backoff< cds::backoff::delay_of<2> >
192             > ::type
193         {};
194         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_2ms >  Elimination_HP_2ms;
195         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_2ms >  Elimination_DHP_2ms;
196
197         struct traits_Elimination_2ms_stat : public
198             cds::container::treiber_stack::make_traits <
199                 cds::opt::enable_elimination<true>
200                 , cds::opt::elimination_backoff< cds::backoff::delay_of<2> >
201                 , cds::opt::stat<cds::intrusive::treiber_stack::stat<> >
202             > ::type
203         {};
204         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_2ms_stat > Elimination_HP_2ms_stat;
205         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_2ms_stat > Elimination_DHP_2ms_stat;
206
207         struct traits_Elimination_5ms : public
208             cds::container::treiber_stack::make_traits <
209                 cds::opt::enable_elimination<true>
210                 , cds::opt::elimination_backoff< cds::backoff::delay_of<5> >
211             > ::type
212         {};
213         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_5ms > Elimination_HP_5ms;
214         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_5ms > Elimination_DHP_5ms;
215
216         struct traits_Elimination_5ms_stat : public
217             cds::container::treiber_stack::make_traits <
218                 cds::opt::enable_elimination<true>
219                 , cds::opt::elimination_backoff< cds::backoff::delay_of<5> >
220                 , cds::opt::stat<cds::intrusive::treiber_stack::stat<> >
221             > ::type
222         {};
223         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_5ms_stat > Elimination_HP_5ms_stat;
224         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_5ms_stat > Elimination_DHP_5ms_stat;
225
226         struct traits_Elimination_10ms : public
227             cds::container::treiber_stack::make_traits <
228                 cds::opt::enable_elimination<true>
229                 , cds::opt::elimination_backoff< cds::backoff::delay_of<10> >
230             > ::type
231         {};
232         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_10ms > Elimination_HP_10ms;
233         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_10ms > Elimination_DHP_10ms;
234
235         struct traits_Elimination_10ms_stat : public
236             cds::container::treiber_stack::make_traits <
237                 cds::opt::enable_elimination<true>
238                 , cds::opt::elimination_backoff< cds::backoff::delay_of<10> >
239                 , cds::opt::stat<cds::intrusive::treiber_stack::stat<> >
240             > ::type
241         {};
242         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_10ms_stat > Elimination_HP_10ms_stat;
243         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_10ms_stat > Elimination_DHP_10ms_stat;
244
245         struct traits_Elimination_dyn: public
246             cds::container::treiber_stack::make_traits <
247                 cds::opt::enable_elimination<true>
248                 , cds::opt::buffer< cds::opt::v::dynamic_buffer<int> >
249             > ::type
250         {};
251         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_dyn > Elimination_HP_dyn;
252         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_dyn > Elimination_DHP_dyn;
253
254         struct traits_Elimination_seqcst: public
255             cds::container::treiber_stack::make_traits <
256                 cds::opt::enable_elimination<true>
257                 , cds::opt::memory_model<cds::opt::v::sequential_consistent>
258             > ::type
259         {};
260         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_seqcst > Elimination_HP_seqcst;
261         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_seqcst > Elimination_DHP_seqcst;
262
263         struct traits_Elimination_dyn_stat: public
264             cds::container::treiber_stack::make_traits <
265                 cds::opt::enable_elimination<true>
266                 , cds::opt::stat<cds::intrusive::treiber_stack::stat<> >
267                 , cds::opt::buffer< cds::opt::v::dynamic_buffer<int> >
268             > ::type
269         {};
270         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_dyn_stat > Elimination_HP_dyn_stat;
271         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_dyn_stat > Elimination_DHP_dyn_stat;
272
273         struct traits_Elimination_yield: public
274             cds::container::treiber_stack::make_traits <
275                 cds::opt::enable_elimination<true>
276                 , cds::opt::back_off<cds::backoff::yield>
277                 , cds::opt::memory_model<cds::opt::v::relaxed_ordering>
278             > ::type
279         {};
280         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_yield > Elimination_HP_yield;
281         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_yield > Elimination_DHP_yield;
282
283         struct traits_Elimination_pause: public
284             cds::container::treiber_stack::make_traits <
285                 cds::opt::enable_elimination<true>
286                 , cds::opt::back_off<cds::backoff::pause>
287             > ::type
288         {};
289         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_pause > Elimination_HP_pause;
290         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_pause > Elimination_DHP_pause;
291
292         struct traits_Elimination_exp: public
293             cds::container::treiber_stack::make_traits <
294                 cds::opt::enable_elimination<true>
295                 ,cds::opt::back_off<
296                     cds::backoff::exponential<
297                         cds::backoff::pause,
298                         cds::backoff::yield
299                     >
300                 >
301             > ::type
302         {};
303         typedef cds::container::TreiberStack< cds::gc::HP,  T, traits_Elimination_exp > Elimination_HP_exp;
304         typedef cds::container::TreiberStack< cds::gc::DHP, T, traits_Elimination_exp > Elimination_DHP_exp;
305
306
307     // FCStack
308         typedef cds::container::FCStack< T > FCStack_deque;
309
310         struct traits_FCStack_stat:
311             public cds::container::fcstack::make_traits<
312                 cds::opt::stat< cds::container::fcstack::stat<> >
313             >::type
314         {};
315         struct traits_FCStack_elimination:
316             public cds::container::fcstack::make_traits<
317             cds::opt::enable_elimination< true >
318             >::type
319         {};
320         struct traits_FCStack_elimination_stat:
321             public cds::container::fcstack::make_traits<
322                 cds::opt::stat< cds::container::fcstack::stat<> >,
323                 cds::opt::enable_elimination< true >
324             >::type
325         {};
326         struct traits_FCStack_mutex:
327             public cds::container::fcstack::make_traits<
328                 cds::opt::lock_type< std::mutex >
329             >::type
330         {};
331
332         typedef cds::container::FCStack< T, std::stack<T, std::deque<T> >, traits_FCStack_mutex > FCStack_deque_mutex;
333         typedef cds::container::FCStack< T, std::stack<T, std::deque<T> >, traits_FCStack_stat > FCStack_deque_stat;
334         typedef cds::container::FCStack< T, std::stack<T, std::deque<T> >, traits_FCStack_elimination > FCStack_deque_elimination;
335         typedef cds::container::FCStack< T, std::stack<T, std::deque<T> >, traits_FCStack_elimination_stat > FCStack_deque_elimination_stat;
336         typedef cds::container::FCStack< T, std::stack<T, std::vector<T> > > FCStack_vector;
337         typedef cds::container::FCStack< T, std::stack<T, std::vector<T> >, traits_FCStack_mutex > FCStack_vector_mutex;
338         typedef cds::container::FCStack< T, std::stack<T, std::vector<T> >, traits_FCStack_stat > FCStack_vector_stat;
339         typedef cds::container::FCStack< T, std::stack<T, std::vector<T> >, traits_FCStack_elimination > FCStack_vector_elimination;
340         typedef cds::container::FCStack< T, std::stack<T, std::vector<T> >, traits_FCStack_elimination_stat > FCStack_vector_elimination_stat;
341         typedef cds::container::FCStack< T, std::stack<T, std::list<T> > > FCStack_list;
342         typedef cds::container::FCStack< T, std::stack<T, std::list<T> >, traits_FCStack_mutex > FCStack_list_mutex;
343         typedef cds::container::FCStack< T, std::stack<T, std::list<T> >, traits_FCStack_stat > FCStack_list_stat;
344         typedef cds::container::FCStack< T, std::stack<T, std::list<T> >, traits_FCStack_elimination > FCStack_list_elimination;
345         typedef cds::container::FCStack< T, std::stack<T, std::list<T> >, traits_FCStack_elimination_stat > FCStack_list_elimination_stat;
346
347    // FCDeque
348         struct traits_FCDeque_stat:
349             public cds::container::fcdeque::make_traits<
350                 cds::opt::stat< cds::container::fcdeque::stat<> >
351             >::type
352         {};
353         struct traits_FCDeque_elimination:
354             public cds::container::fcdeque::make_traits<
355                 cds::opt::enable_elimination< true >
356             >::type
357         {};
358         struct traits_FCDeque_elimination_stat:
359             public cds::container::fcdeque::make_traits<
360                 cds::opt::stat< cds::container::fcdeque::stat<> >,
361                 cds::opt::enable_elimination< true >
362             >::type
363         {};
364         struct traits_FCDeque_mutex:
365             public cds::container::fcdeque::make_traits<
366                 cds::opt::lock_type< std::mutex >
367             >::type
368         {};
369
370
371         typedef details::FCDequeL< T > FCDequeL_default;
372         typedef details::FCDequeL< T, traits_FCDeque_mutex > FCDequeL_mutex;
373         typedef details::FCDequeL< T, traits_FCDeque_stat > FCDequeL_stat;
374         typedef details::FCDequeL< T, traits_FCDeque_elimination > FCDequeL_elimination;
375         typedef details::FCDequeL< T, traits_FCDeque_elimination_stat > FCDequeL_elimination_stat;
376
377         typedef details::FCDequeR< T > FCDequeR_default;
378         typedef details::FCDequeR< T, traits_FCDeque_mutex > FCDequeR_mutex;
379         typedef details::FCDequeR< T, traits_FCDeque_stat > FCDequeR_stat;
380         typedef details::FCDequeR< T, traits_FCDeque_elimination > FCDequeR_elimination;
381         typedef details::FCDequeR< T, traits_FCDeque_elimination_stat > FCDequeR_elimination_stat;
382
383
384         // std::stack
385         typedef details::StdStack< T, std::stack< T >, std::mutex >  StdStack_Deque_Mutex;
386         typedef details::StdStack< T, std::stack< T >, cds::lock::Spin > StdStack_Deque_Spin;
387         typedef details::StdStack< T, std::stack< T, std::vector<T> >, std::mutex >  StdStack_Vector_Mutex;
388         typedef details::StdStack< T, std::stack< T, std::vector<T> >, cds::lock::Spin > StdStack_Vector_Spin;
389         typedef details::StdStack< T, std::stack< T, std::list<T> >, std::mutex >  StdStack_List_Mutex;
390         typedef details::StdStack< T, std::stack< T, std::list<T> >, cds::lock::Spin > StdStack_List_Spin;
391
392     };
393 } // namespace stack
394
395 namespace std {
396     static inline ostream& operator <<( ostream& o, cds::container::treiber_stack::stat<> const& s )
397     {
398         return o << "\tStatistics:\n"
399             << "\t                    Push: " << s.m_PushCount.get()              << "\n"
400             << "\t                     Pop: " << s.m_PopCount.get()               << "\n"
401             << "\t         Push contention: " << s.m_PushRace.get()               << "\n"
402             << "\t          Pop contention: " << s.m_PopRace.get()                << "\n"
403             << "\t   m_ActivePushCollision: " << s.m_ActivePushCollision.get()    << "\n"
404             << "\t   m_PassivePopCollision: " << s.m_PassivePopCollision.get()    << "\n"
405             << "\t    m_ActivePopCollision: " << s.m_ActivePopCollision.get()     << "\n"
406             << "\t  m_PassivePushCollision: " << s.m_PassivePushCollision.get()   << "\n"
407             << "\t     m_EliminationFailed: " << s.m_EliminationFailed.get()      << "\n";
408     }
409
410     static inline ostream& operator <<(ostream& o, cds::container::treiber_stack::empty_stat const& /*s*/)
411     {
412         return o;
413     }
414
415     static inline ostream& operator <<( ostream& o, cds::container::fcstack::empty_stat const& /*s*/ )
416     {
417         return o;
418     }
419
420     static inline ostream& operator <<( ostream& o, cds::container::fcstack::stat<> const& s )
421     {
422         return o << "\tStatistics:\n"
423             << "\t                    Push: " << s.m_nPush.get()              << "\n"
424             << "\t                PushMove: " << s.m_nPushMove.get()          << "\n"
425             << "\t                     Pop: " << s.m_nPop.get()               << "\n"
426             << "\t               FailedPop: " << s.m_nFailedPop.get()         << "\n"
427             << "\t  Collided push/pop pair: " << s.m_nCollided.get()          << "\n"
428             << "\tFlat combining statistics:\n"
429             << "\t        Combining factor: " << s.combining_factor()         << "\n"
430             << "\t         Operation count: " << s.m_nOperationCount.get()    << "\n"
431             << "\t      Combine call count: " << s.m_nCombiningCount.get()    << "\n"
432             << "\t        Compact pub-list: " << s.m_nCompactPublicationList.get() << "\n"
433             << "\t   Deactivate pub-record: " << s.m_nDeactivatePubRecord.get()    << "\n"
434             << "\t     Activate pub-record: " << s.m_nActivatePubRecord.get() << "\n"
435             << "\t       Create pub-record: " << s.m_nPubRecordCreated.get()  << "\n"
436             << "\t       Delete pub-record: " << s.m_nPubRecordDeteted.get()  << "\n"
437             << "\t      Acquire pub-record: " << s.m_nAcquirePubRecCount.get()<< "\n"
438             << "\t      Release pub-record: " << s.m_nReleasePubRecCount.get()<< "\n";
439     }
440
441     static inline ostream& operator <<( ostream& o, cds::container::fcdeque::empty_stat const& /*s*/ )
442     {
443         return o;
444     }
445
446     static inline ostream& operator <<( ostream& o, cds::container::fcdeque::stat<> const& s )
447     {
448         return o << "\tStatistics:\n"
449             << "\t              Push front: " << s.m_nPushFront.get()         << "\n"
450             << "\t         Push front move: " << s.m_nPushFrontMove.get()     << "\n"
451             << "\t               Push back: " << s.m_nPushBack.get()          << "\n"
452             << "\t          Push back move: " << s.m_nPushBackMove.get()      << "\n"
453             << "\t               Pop front: " << s.m_nPopFront.get()          << "\n"
454             << "\t        Failed pop front: " << s.m_nFailedPopFront.get()    << "\n"
455             << "\t                Pop back: " << s.m_nPopBack.get()           << "\n"
456             << "\t         Failed pop back: " << s.m_nFailedPopBack.get()     << "\n"
457             << "\t  Collided push/pop pair: " << s.m_nCollided.get()          << "\n"
458             << "\tFlat combining statistics:\n"
459             << "\t        Combining factor: " << s.combining_factor()         << "\n"
460             << "\t         Operation count: " << s.m_nOperationCount.get()    << "\n"
461             << "\t      Combine call count: " << s.m_nCombiningCount.get()    << "\n"
462             << "\t        Compact pub-list: " << s.m_nCompactPublicationList.get() << "\n"
463             << "\t   Deactivate pub-record: " << s.m_nDeactivatePubRecord.get()    << "\n"
464             << "\t     Activate pub-record: " << s.m_nActivatePubRecord.get() << "\n"
465             << "\t       Create pub-record: " << s.m_nPubRecordCreated.get()  << "\n"
466             << "\t       Delete pub-record: " << s.m_nPubRecordDeteted.get()  << "\n"
467             << "\t      Acquire pub-record: " << s.m_nAcquirePubRecCount.get()<< "\n"
468             << "\t      Release pub-record: " << s.m_nReleasePubRecCount.get()<< "\n";
469     }
470
471 } // namespace std
472
473 #endif // #ifndef __CDSUNIT_STACK_TYPES_H