Uses different pass count for different parallel queue test cases
[libcds.git] / cds / urcu / details / gp.h
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 #ifndef CDSLIB_URCU_DETAILS_GP_H
32 #define CDSLIB_URCU_DETAILS_GP_H
33
34 #include <cds/urcu/details/gp_decl.h>
35 #include <cds/threading/model.h>
36
37 //@cond
38 namespace cds { namespace urcu { namespace details {
39
40     // Inlines
41
42     // gp_thread_gc
43     template <typename RCUtag>
44     inline gp_thread_gc<RCUtag>::gp_thread_gc()
45     {
46         if ( !threading::Manager::isThreadAttached())
47             cds::threading::Manager::attachThread();
48     }
49
50     template <typename RCUtag>
51     inline gp_thread_gc<RCUtag>::~gp_thread_gc()
52     {
53         cds::threading::Manager::detachThread();
54     }
55
56     template <typename RCUtag>
57     inline typename gp_thread_gc<RCUtag>::thread_record * gp_thread_gc<RCUtag>::get_thread_record()
58     {
59         return cds::threading::getRCU<RCUtag>();
60     }
61
62     template <typename RCUtag>
63     inline void gp_thread_gc<RCUtag>::access_lock()
64     {
65         thread_record * pRec = get_thread_record();
66         assert( pRec != nullptr );
67
68         uint32_t tmp = pRec->m_nAccessControl.load( atomics::memory_order_relaxed );
69         if ( (tmp & rcu_class::c_nNestMask) == 0 ) {
70             pRec->m_nAccessControl.store( gp_singleton<RCUtag>::instance()->global_control_word(atomics::memory_order_relaxed),
71                 atomics::memory_order_relaxed );
72
73             atomics::atomic_thread_fence( atomics::memory_order_seq_cst );
74         }
75         else {
76             // nested lock
77             pRec->m_nAccessControl.store( tmp + 1, atomics::memory_order_relaxed );
78         }
79     }
80
81     template <typename RCUtag>
82     inline void gp_thread_gc<RCUtag>::access_unlock()
83     {
84         thread_record * pRec = get_thread_record();
85         assert( pRec != nullptr );
86
87         uint32_t tmp = pRec->m_nAccessControl.load( atomics::memory_order_relaxed );
88         assert( (tmp & rcu_class::c_nNestMask) > 0 );
89
90 #if CDS_COMPILER == CDS_COMPILER_CLANG && CDS_COMPILER_VERSION < 30800
91         // CLang 3.6-3.7: some tests of intrusive::FeldmanHashSet based on general-purpose RCU
92         // are failed even in single-threaded mode (unit tests) without magic compiler barrier below
93         CDS_COMPILER_RW_BARRIER;
94 #endif
95         pRec->m_nAccessControl.store( tmp - 1, atomics::memory_order_release );
96     }
97
98     template <typename RCUtag>
99     inline bool gp_thread_gc<RCUtag>::is_locked()
100     {
101         thread_record * pRec = get_thread_record();
102         assert( pRec != nullptr );
103
104         return (pRec->m_nAccessControl.load( atomics::memory_order_relaxed ) & rcu_class::c_nNestMask) != 0;
105     }
106
107
108     // gp_singleton
109     template <typename RCUtag>
110     inline bool gp_singleton<RCUtag>::check_grace_period( typename gp_singleton<RCUtag>::thread_record * pRec ) const
111     {
112         uint32_t const v = pRec->m_nAccessControl.load( atomics::memory_order_acquire );
113         return (v & general_purpose_rcu::c_nNestMask)
114             && (( v ^ m_nGlobalControl.load( atomics::memory_order_relaxed )) & ~general_purpose_rcu::c_nNestMask );
115     }
116
117     template <typename RCUtag>
118     template <class Backoff>
119     inline void gp_singleton<RCUtag>::flip_and_wait( Backoff& bkoff )
120     {
121         OS::ThreadId const nullThreadId = OS::c_NullThreadId;
122         m_nGlobalControl.fetch_xor( general_purpose_rcu::c_nControlBit, atomics::memory_order_seq_cst );
123
124         for ( thread_record * pRec = m_ThreadList.head( atomics::memory_order_acquire ); pRec; pRec = pRec->m_list.m_pNext ) {
125             while ( pRec->m_list.m_idOwner.load( atomics::memory_order_acquire ) != nullThreadId && check_grace_period( pRec )) {
126                 bkoff();
127                 CDS_COMPILER_RW_BARRIER;
128             }
129             bkoff.reset();
130         }
131     }
132
133
134 }}} // namespace cds:urcu::details
135 //@endcond
136
137 #endif // #ifndef CDSLIB_URCU_DETAILS_GP_H