Added free-list implementations
[libcds.git] / cds / intrusive / free_list.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-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 #ifndef CDSLIB_INTRUSIVE_FREE_LIST_H
32 #define CDSLIB_INTRUSIVE_FREE_LIST_H
33
34 #include <cds/algo/atomic.h>
35
36 namespace cds { namespace intrusive {
37
38     /// Lock-free free list
39     /** @ingroup cds_intrusive_helper
40
41         Free list is a helper class intended for reusing objects instead of freeing them completely; 
42         this avoids the overhead of \p malloc(), and also avoids its worst-case behaviour of taking an operating system lock.
43         So, the free list can be considered as a specialized allocator for objects of some type.
44
45         The algorithm is taken from <a href="http://moodycamel.com/blog/2014/solving-the-aba-problem-for-lock-free-free-lists">this article</a>.
46         The algo does not require any SMR like Hazard Pointer to prevent ABA problem.
47
48         There is \ref TaggedFreeList "tagged pointers" variant of free list for processors which support double-width CAS.
49
50         \b How to use
51         \code
52         #include <cds/intrusive/free_list.h>
53
54         // Your struct should be derived from FreeList::node
55         struct Foo: public cds::intrusive::FreeList::node
56         {
57             // Foo fields
58         };
59
60         // Simplified Foo allocator
61         class FooAllocator
62         {
63         public:
64             // free-list clear() must be explicitly called before destroying the free-list object
65             ~FooAllocator()
66             {
67                 m_FreeList.clear( []( freelist_node * p ) { delete static_cast<Foo *>( p ); });
68             }
69
70             Foo * alloc()
71             {
72                 freelist_node * p = m_FreeList.get();
73                 if ( p )
74                     return static_cast<Foo *>( p );
75                 return new Foo;
76             };
77
78             void dealloc( Foo * p )
79             {
80                 m_FreeList.put( static_cast<freelist_node *>( p ));
81             };
82
83         private:
84             typedef cds::intrusive::FreeList::node freelist_node;
85             cds::intrusive::FreeList m_FreeList;
86         };
87         \endcode
88     */
89     class FreeList
90     {
91     public:
92         /// Free list node
93         struct node {
94             //@cond
95             atomics::atomic<uint32_t>   m_freeListRefs;
96             atomics::atomic<node *>     m_freeListNext;
97
98             node()
99                 : m_freeListRefs( 0 )
100                 , m_freeListNext( nullptr )
101             {}
102             //@endcond
103         };
104
105     public:
106         /// Creates empty free list
107         FreeList()
108             : m_Head( nullptr )
109         {}
110
111         /// Destroys the free list. Free-list must be empty.
112         /**
113             @warning dtor does not free elements of the list.
114             To free elements you should manually call \p clear() with an appropriate disposer.
115         */
116         ~FreeList()
117         {
118             assert( empty() );
119         }
120
121         /// Puts \p pNode to the free list
122         void put( node * pNode )
123         {
124             // We know that the should-be-on-freelist bit is 0 at this point, so it's safe to
125             // set it using a fetch_add
126             if ( pNode->m_freeListRefs.fetch_add( c_ShouldBeOnFreeList, atomics::memory_order_release ) == 0 ) {
127                 // Oh look! We were the last ones referencing this node, and we know
128                 // we want to add it to the free list, so let's do it!
129                 add_knowing_refcount_is_zero( pNode );
130             }
131         }
132
133         /// Gets a node from the free list. If the list is empty, returns \p nullptr
134         node * get()
135         {
136             auto head = m_Head.load( atomics::memory_order_acquire );
137             while ( head != nullptr ) {
138                 auto prevHead = head;
139                 auto refs = head->m_freeListRefs.load( atomics::memory_order_relaxed );
140                 if ( (refs & c_RefsMask) == 0 || !head->m_freeListRefs.compare_exchange_strong( refs, refs + 1,
141                     atomics::memory_order_acquire, atomics::memory_order_relaxed )) 
142                 {
143                     head = m_Head.load( atomics::memory_order_acquire );
144                     continue;
145                 }
146
147                 // Good, reference count has been incremented (it wasn't at zero), which means
148                 // we can read the next and not worry about it changing between now and the time
149                 // we do the CAS
150                 node * next = head->m_freeListNext.load( atomics::memory_order_relaxed );
151                 if ( m_Head.compare_exchange_strong( head, next, atomics::memory_order_acquire, atomics::memory_order_relaxed )) {
152                     // Yay, got the node. This means it was on the list, which means
153                     // shouldBeOnFreeList must be false no matter the refcount (because
154                     // nobody else knows it's been taken off yet, it can't have been put back on).
155                     assert( (head->m_freeListRefs.load( atomics::memory_order_relaxed ) & c_ShouldBeOnFreeList) == 0 );
156
157                     // Decrease refcount twice, once for our ref, and once for the list's ref
158                     head->m_freeListRefs.fetch_sub( 2, atomics::memory_order_relaxed );
159
160                     return head;
161                 }
162
163                 // OK, the head must have changed on us, but we still need to decrease the refcount we
164                 // increased
165                 refs = prevHead->m_freeListRefs.fetch_sub( 1, atomics::memory_order_acq_rel );
166                 if ( refs == c_ShouldBeOnFreeList + 1 )
167                     add_knowing_refcount_is_zero( prevHead );
168             }
169
170             return nullptr;
171         }
172
173         /// Checks whether the free list is empty
174         bool empty() const
175         {
176             return m_Head.load( atomics::memory_order_relaxed ) == nullptr;
177         }
178
179         /// Clears the free list (not atomic)
180         /**
181             For each element \p disp disposer is called to free memory.
182             The \p Disposer interface:
183             \code
184             struct disposer
185             {
186                 void operator()( FreeList::node * node );
187             };
188             \endcode
189
190             This method must be explicitly called before the free list destructor.
191         */
192         template <typename Disposer>
193         void clear( Disposer disp )
194         {
195             node * head = m_Head.load( atomics::memory_order_relaxed );
196             m_Head.store( nullptr, atomics::memory_order_relaxed );
197             while ( head ) {
198                 node * next = head->m_freeListNext.load( atomics::memory_order_relaxed );
199                 disp( head );
200                 head = next;
201             }
202         }
203
204     private:
205         //@cond
206         void add_knowing_refcount_is_zero( node * pNode )
207         {
208             // Since the refcount is zero, and nobody can increase it once it's zero (except us, and we
209             // run only one copy of this method per node at a time, i.e. the single thread case), then we
210             // know we can safely change the next pointer of the node; however, once the refcount is back
211             // above zero, then other threads could increase it (happens under heavy contention, when the
212             // refcount goes to zero in between a load and a refcount increment of a node in try_get, then
213             // back up to something non-zero, then the refcount increment is done by the other thread) --
214             // so, if the CAS to add the node to the actual list fails, decrease the refcount and leave
215             // the add operation to the next thread who puts the refcount back at zero (which could be us,
216             // hence the loop).
217             node * head = m_Head.load( atomics::memory_order_relaxed );
218             while ( true ) {
219                 pNode->m_freeListNext.store( head, atomics::memory_order_relaxed );
220                 pNode->m_freeListRefs.store( 1, atomics::memory_order_release );
221                 if ( !m_Head.compare_exchange_strong( head, pNode, atomics::memory_order_release, atomics::memory_order_relaxed )) {
222                     // Hmm, the add failed, but we can only try again when the refcount goes back to zero
223                     if ( pNode->m_freeListRefs.fetch_add( c_ShouldBeOnFreeList - 1, atomics::memory_order_release ) == 1 )
224                         continue;
225                 }
226                 return;
227             }
228         }
229         //@endcond
230
231     private:
232         //@cond
233         static CDS_CONSTEXPR uint32_t const c_RefsMask = 0x7FFFFFFF;
234         static CDS_CONSTEXPR uint32_t const c_ShouldBeOnFreeList = 0x80000000;
235
236         // Implemented like a stack, but where node order doesn't matter (nodes are
237         // inserted out of order under contention)
238         atomics::atomic<node *>  m_Head;
239         //@endcond
240     };
241
242 }} // namespace cds::intrusive
243
244 #endif // CDSLIB_INTRUSIVE_FREE_LIST_H