Improved management of SkipList auxiliary nodes: now aux nodes are allocated from...
[libcds.git] / cds / intrusive / free_list_tagged.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_TAGGED_H
32 #define CDSLIB_INTRUSIVE_FREE_LIST_TAGGED_H
33
34 #include <cds/algo/atomic.h>
35
36 namespace cds { namespace intrusive {
37
38     /// Lock-free free list based on tagged pointers (required double-width CAS)
39     /** @ingroup cds_intrusive_helper
40         This variant of \p FreeList is intended for processor architectures that support double-width CAS.
41         It uses <a href="https://en.wikipedia.org/wiki/Tagged_pointer">tagged pointer</a> technique to solve ABA problem.
42
43         \b How to use
44         \code
45         #include <cds/intrusive/free_list_tagged.h>
46
47         // Your struct should be derived from TaggedFreeList::node
48         struct Foo: public cds::intrusive::TaggedFreeList::node
49         {
50             // Foo fields
51         };
52
53         // Simplified Foo allocator
54         class FooAllocator
55         {
56         public:
57             // free-list clear() must be explicitly called before destroying the free-list object
58             ~FooAllocator()
59             {
60                 m_FreeList.clear( []( freelist_node * p ) { delete static_cast<Foo *>( p ); });
61             }
62
63             Foo * alloc()
64             {
65                 freelist_node * p = m_FreeList.get();
66                 if ( p )
67                     return static_cast<Foo *>( p );
68                 return new Foo;
69             };
70
71             void dealloc( Foo * p )
72             {
73                 m_FreeList.put( static_cast<freelist_node *>( p ));
74             };
75
76         private:
77             typedef cds::intrusive::TaggedFreeList::node freelist_node;
78             cds::intrusive::TaggedFreeList m_FreeList;
79         };
80         \endcode
81     */
82     class TaggedFreeList
83     {
84     public:
85         struct node {
86             //@cond
87             atomics::atomic<node *> m_freeListNext;
88
89             node()
90                 : m_freeListNext( nullptr )
91             {}
92             //@endcond
93         };
94
95     private:
96         //@cond
97         struct tagged_ptr
98         {
99             node *    ptr;
100             uintptr_t tag;
101
102             tagged_ptr()
103                 : ptr( nullptr )
104                 , tag( 0 )
105             {}
106
107             tagged_ptr( node* p )
108                 : ptr( p )
109                 , tag( 0 )
110             {}
111         };
112
113         static_assert(sizeof( tagged_ptr ) == sizeof( void * ) * 2, "sizeof( tagged_ptr ) violation");
114         //@endcond
115
116     public:
117         /// Creates empty free-list
118         TaggedFreeList()
119             : m_Head( tagged_ptr())
120         {
121             // Your platform must support double-width CAS
122             assert( m_Head.is_lock_free());
123         }
124
125         /// Destroys the free list. Free-list must be empty.
126         /**
127             @warning dtor does not free elements of the list.
128             To free elements you should manually call \p clear() with an appropriate disposer.
129         */
130         ~TaggedFreeList()
131         {
132             assert( empty() );
133         }
134
135         /// Puts \p pNode to the free list
136         void put( node * pNode )
137         {
138             assert( m_Head.is_lock_free());
139
140             tagged_ptr currentHead = m_Head.load( atomics::memory_order_relaxed );
141             tagged_ptr newHead = { pNode };
142             do {
143                 newHead.tag = currentHead.tag + 1;
144                 pNode->m_freeListNext.store( currentHead.ptr, atomics::memory_order_relaxed );
145             } while ( cds_unlikely( !m_Head.compare_exchange_weak( currentHead, newHead, atomics::memory_order_release, atomics::memory_order_relaxed )));
146         }
147
148         /// Gets a node from the free list. If the list is empty, returns \p nullptr
149         node * get()
150         {
151             tagged_ptr currentHead = m_Head.load( atomics::memory_order_acquire );
152             tagged_ptr newHead;
153             while ( currentHead.ptr != nullptr ) {
154                 newHead.ptr = currentHead.ptr->m_freeListNext.load( atomics::memory_order_relaxed );
155                 newHead.tag = currentHead.tag + 1;
156                 if ( cds_likely( m_Head.compare_exchange_weak( currentHead, newHead, atomics::memory_order_release, atomics::memory_order_acquire )))
157                     break;
158             }
159             return currentHead.ptr;
160         }
161
162         /// Checks whether the free list is empty
163         bool empty() const
164         {
165             return m_Head.load( atomics::memory_order_relaxed ).ptr == nullptr;
166         }
167
168         /// Clears the free list (not atomic)
169         /**
170             For each element \p disp disposer is called to free memory.
171             The \p Disposer interface:
172             \code
173             struct disposer
174             {
175                 void operator()( FreeList::node * node );
176             };
177             \endcode
178
179             This method must be explicitly called before the free list destructor.
180         */
181         template <typename Disposer>
182         void clear( Disposer disp )
183         {
184             node * head = m_Head.load( atomics::memory_order_relaxed ).ptr;
185             m_Head.store( { nullptr }, atomics::memory_order_relaxed );
186             while ( head ) {
187                 node * next = head->m_freeListNext.load( atomics::memory_order_relaxed );
188                 disp( head );
189                 head = next;
190             }
191         }
192
193     private:
194         //@cond
195         atomics::atomic<tagged_ptr> m_Head;
196         //@endcond    
197     };
198
199 }} // namespace cds::intrusive
200
201 #endif // CDSLIB_INTRUSIVE_FREE_LIST_TAGGED_H