Removed TSan annotations, tuned memory ordering
[libcds.git] / cds / urcu / details / gpi.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_URCU_DETAILS_GPI_H
4 #define CDSLIB_URCU_DETAILS_GPI_H
5
6 #include <mutex>
7 #include <cds/urcu/details/gp.h>
8 #include <cds/algo/backoff_strategy.h>
9
10 namespace cds { namespace urcu {
11
12     /// User-space general-purpose RCU with immediate reclamation
13     /**
14         @headerfile cds/urcu/general_instant.h
15
16         This is simplest general-purpose RCU implementation. When a thread calls \ref retire_ptr function
17         the RCU \p synchronize function is called that waits until all reader/updater threads end up
18         their read-side critical sections, i.e. until the RCU quiescent state will come.
19         After that the retired object is freed immediately.
20         Thus, the implementation blocks for any retired object
21
22         There is a wrapper \ref cds_urcu_general_instant_gc "gc<general_instant>" for \p %general_instant class
23         that provides unified RCU interface. You should use this wrapper class instead \p %general_instant
24
25         Template arguments:
26         - \p Lock - mutex type, default is \p std::mutex
27         - \p Backoff - back-off schema, default is cds::backoff::Default
28     */
29     template <
30         class Lock = std::mutex
31        ,class Backoff = cds::backoff::Default
32     >
33     class general_instant: public details::gp_singleton< general_instant_tag >
34     {
35         //@cond
36         typedef details::gp_singleton< general_instant_tag > base_class;
37         //@endcond
38
39     public:
40         typedef general_instant_tag rcu_tag ;   ///< RCU tag
41         typedef Lock    lock_type   ;           ///< Lock type
42         typedef Backoff back_off    ;           ///< Back-off schema type
43
44         typedef typename base_class::thread_gc  thread_gc ;   ///< Thread-side RCU part
45         typedef typename thread_gc::scoped_lock scoped_lock ; ///< Access lock class
46
47         static bool const c_bBuffered = false ; ///< This RCU does not buffer disposed elements
48
49     protected:
50         //@cond
51         typedef details::gp_singleton_instance< rcu_tag >    singleton_ptr;
52         //@endcond
53
54     protected:
55         //@cond
56         lock_type   m_Lock;
57         //@endcond
58
59     public:
60         /// Returns singleton instance
61         static general_instant * instance()
62         {
63             return static_cast<general_instant *>( base_class::instance() );
64         }
65
66         /// Checks if the singleton is created and ready to use
67         static bool isUsed()
68         {
69             return singleton_ptr::s_pRCU != nullptr;
70         }
71
72     protected:
73         //@cond
74         general_instant()
75         {}
76         ~general_instant()
77         {}
78
79         void flip_and_wait()
80         {
81             back_off bkoff;
82             base_class::flip_and_wait( bkoff );
83         }
84         //@endcond
85
86     public:
87         /// Creates singleton object
88         static void Construct()
89         {
90             if ( !singleton_ptr::s_pRCU )
91                 singleton_ptr::s_pRCU = new general_instant();
92         }
93
94         /// Destroys singleton object
95         static void Destruct( bool bDetachAll = false )
96         {
97             if ( isUsed() ) {
98                 if ( bDetachAll )
99                     instance()->m_ThreadList.detach_all();
100                 delete instance();
101                 singleton_ptr::s_pRCU = nullptr;
102             }
103         }
104
105     public:
106         /// Retires \p p pointer
107         /**
108             The method calls \ref synchronize to wait for the end of grace period
109             and calls \p p disposer.
110         */
111         virtual void retire_ptr( retired_ptr& p )
112         {
113             synchronize();
114             if ( p.m_p ) {
115                 // TSan ignores atomic_thread_fence in synchronize()
116                 //CDS_TSAN_ANNOTATE_HAPPENS_BEFORE( p.m_p );
117                 p.free();
118             }
119         }
120
121         /// Retires the pointer chain [\p itFirst, \p itLast)
122         template <typename ForwardIterator>
123         void batch_retire( ForwardIterator itFirst, ForwardIterator itLast )
124         {
125             if ( itFirst != itLast ) {
126                 synchronize();
127                 while ( itFirst != itLast ) {
128                     retired_ptr p( *itFirst );
129                     ++itFirst;
130                     if ( p.m_p ) {
131                         // TSan ignores atomic_thread_fence in synchronize()
132                         //CDS_TSAN_ANNOTATE_HAPPENS_BEFORE( p.m_p );
133                         p.free();
134                     }
135                 }
136             }
137         }
138
139         /// Waits to finish a grace period
140         void synchronize()
141         {
142             atomics::atomic_thread_fence( atomics::memory_order_acquire );
143             {
144                 std::unique_lock<lock_type> sl( m_Lock );
145                 flip_and_wait();
146                 flip_and_wait();
147             }
148             atomics::atomic_thread_fence( atomics::memory_order_release );
149         }
150
151         //@cond
152         // Added for uniformity
153         size_t CDS_CONSTEXPR capacity() const
154         {
155             return 1;
156         }
157         //@endcond
158     };
159
160 }} // namespace cds::urcu
161
162 #endif // #ifndef CDSLIB_URCU_DETAILS_GPI_H