Embedded Multicore Building Blocks V1.0.0
lock_free_stack.h
1 /*
2  * Copyright (c) 2014-2017, Siemens AG. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef EMBB_CONTAINERS_LOCK_FREE_STACK_H_
28 #define EMBB_CONTAINERS_LOCK_FREE_STACK_H_
29 
30 #include <embb/containers/object_pool.h>
31 #include <embb/base/atomic.h>
32 #include <embb/base/function.h>
33 #include <embb/containers/internal/hazard_pointer.h>
34 #include <embb/containers/lock_free_tree_value_pool.h>
35 
106 namespace embb {
107 namespace containers {
108 namespace internal {
117 template< typename T >
118 class LockFreeStackNode {
119  private:
123  LockFreeStackNode< T >* next;
124 
128  T element;
129 
130  public:
134  LockFreeStackNode(
135  T const& element
137  );
138 
142  void SetNext(
143  LockFreeStackNode< T >* next
145  );
146 
152  LockFreeStackNode< T >* GetNext();
153 
157  T GetElement();
158 };
159 } // namespace internal
160 
172 template< typename Type,
175  private:
180  size_t capacity;
181 
187  delete_pointer_callback;
188 
193  void DeletePointerCallback(internal::LockFreeStackNode<Type>* to_delete);
194 
204  ObjectPool< internal::LockFreeStackNode<Type>, ValuePool > objectPool;
205 
209  typedef internal::HazardPointer < internal::LockFreeStackNode<Type>* >
210  StackNodeHazardPointer_t;
211 
215  StackNodeHazardPointer_t hazardPointer;
216 
221 
222  public:
237  size_t capacity
239  );
240 
248  size_t GetCapacity();
249 
255  ~LockFreeStack();
256 
270  bool TryPush(
271  Type const& element
273  );
274 
285  bool TryPop(
286  Type & element
289  );
290 };
291 
292 } // namespace containers
293 } // namespace embb
294 
295 #include <embb/containers/internal/lock_free_stack-inl.h>
296 
297 #endif // EMBB_CONTAINERS_LOCK_FREE_STACK_H_
Definition: lock_free_mpmc_queue.h:40
Class representing atomic variables.
Definition: atomic.h:60
Lock-free value pool using binary tree construction.
Definition: lock_free_tree_value_pool.h:57
Pool for thread-safe management of arbitrary objects.
Definition: object_pool.h:59
Wraps function pointers, member function pointers, and functors with up to five arguments.
Definition: function.h:94
Lock-free stack.
Definition: lock_free_stack.h:174