2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
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.
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.
31 #ifndef CDSLIB_INTRUSIVE_FREE_LIST_TAGGED_H
32 #define CDSLIB_INTRUSIVE_FREE_LIST_TAGGED_H
34 #include <cds/algo/atomic.h>
36 namespace cds { namespace intrusive {
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.
45 #include <cds/intrusive/free_list_tagged.h>
47 // Your struct should be derived from TaggedFreeList::node
48 struct Foo: public cds::intrusive::TaggedFreeList::node
53 // Simplified Foo allocator
57 // free-list clear() must be explicitly called before destroying the free-list object
60 m_FreeList.clear( []( freelist_node * p ) { delete static_cast<Foo *>( p ); });
65 freelist_node * p = m_FreeList.get();
67 return static_cast<Foo *>( p );
71 void dealloc( Foo * p )
73 m_FreeList.put( static_cast<freelist_node *>( p ));
77 typedef cds::intrusive::TaggedFreeList::node freelist_node;
78 cds::intrusive::TaggedFreeList m_FreeList;
87 atomics::atomic<node *> m_freeListNext;
90 : m_freeListNext( nullptr )
96 /// Creates empty free-list
99 // Your platform must support double-width CAS
100 assert( m_Head.is_lock_free());
103 /// Destroys the free list. Free-list must be empty.
105 @warning dtor does not free elements of the list.
106 To free elements you should manually call \p clear() with an appropriate disposer.
114 /// Puts \p pNode to the free list
115 void put( node * pNode )
117 tagged_ptr currentHead = m_Head.load( atomics::memory_order_relaxed );
118 tagged_ptr newHead = { pNode };
120 newHead.tag = currentHead.tag + 1;
121 pNode->m_freeListNext.store( currentHead.ptr, atomics::memory_order_relaxed );
122 } while ( cds_unlikely( !m_Head.compare_exchange_weak( currentHead, newHead, atomics::memory_order_release, atomics::memory_order_relaxed )));
125 /// Gets a node from the free list. If the list is empty, returns \p nullptr
128 tagged_ptr currentHead = m_Head.load( atomics::memory_order_acquire );
130 while ( currentHead.ptr != nullptr ) {
131 newHead.ptr = currentHead.ptr->m_freeListNext.load( atomics::memory_order_relaxed );
132 newHead.tag = currentHead.tag + 1;
133 if ( cds_likely( m_Head.compare_exchange_weak( currentHead, newHead, atomics::memory_order_release, atomics::memory_order_acquire )))
136 return currentHead.ptr;
139 /// Checks whether the free list is empty
142 return m_Head.load( atomics::memory_order_relaxed ).ptr == nullptr;
145 /// Clears the free list (not atomic)
147 For each element \p disp disposer is called to free memory.
148 The \p Disposer interface:
152 void operator()( FreeList::node * node );
156 This method must be explicitly called before the free list destructor.
158 template <typename Disposer>
159 void clear( Disposer disp )
161 node * head = m_Head.load( atomics::memory_order_relaxed ).ptr;
162 m_Head.store( { nullptr }, atomics::memory_order_relaxed );
164 node * next = head->m_freeListNext.load( atomics::memory_order_relaxed );
183 static_assert(sizeof( tagged_ptr ) == sizeof(void *) * 2, "sizeof( tagged_ptr ) violation" );
185 atomics::atomic<tagged_ptr> m_Head;
189 }} // namespace cds::intrusive
191 #endif // CDSLIB_INTRUSIVE_FREE_LIST_TAGGED_H