lock_free_stack.h 8.17 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_CONTAINERS_LOCK_FREE_STACK_H_
#define EMBB_CONTAINERS_LOCK_FREE_STACK_H_

#include <embb/containers/object_pool.h>
#include <embb/base/atomic.h>
#include <embb/base/function.h>
#include <embb/containers/internal/hazard_pointer.h>
#include <embb/containers/lock_free_tree_value_pool.h>

/**
 * \defgroup CPP_CONCEPTS_STACK Stack Concept
 * Concept for thread-safe stacks
 *
 * \ingroup CPP_CONCEPT
 * \{
 * \par Description
 * A stack is an abstract data type holding a collection of elements of some
 * predetermined type. A stack provides two operations: \c TryPush and
 * \c TryPop. \c TryPush tries to add an element to the collection, and
 * \c TryPop tries to remove an element from the collection. A stack has LIFO
 * (Last-In, First-out) semantics, i.e., the last element added to the
 * collection (\c TryPush) is removed first (\c TryPop). The capacity \c cap of
 * a stack defines the number of elements it can store (depending on the
 * implementation, a stack might store more than \c cap elements, since for
 * thread-safe memory management, more memory than necessary for holding \c cap
 * elements has to be provided).
 *
 * \par Requirements
 * - Let \c Stack be the stack class
56
 * - Let \c Type be the element type of the stack
57
 * - Let \c capacity be a value of type \c size_t
58
 * - Let \c element be a reference to an element of type \c Type
59 60 61 62 63 64 65 66 67
 *
 * \par Valid Expressions
 * <table>
 *   <tr>
 *     <th>Expression</th>
 *     <th>Return type</th>
 *     <th>Description</th>
 *   </tr>
 *   <tr>
68
 *     <td>\code{.cpp} Stack<Type>(capacity) \endcode</td>
69 70 71
 *     <td>Nothing</td>
 *     <td>
 *      Constructs a stack with capacity \c capacity that holds elements of
72
 *      type \c Type.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
 *     </td>
 *   </tr>
 *   <tr>
 *     <td>\code{.cpp} TryPush(element) \endcode</td>
 *     <td>\code{.cpp} bool \endcode</td>
 *     <td>
 *      Tries to push \c element onto the stack. Returns \c false if the stack
 *      is full, otherwise \c true.
 *     </td>
 *   </tr>
 *   <tr>
 *     <td>\code{.cpp} TryPop(element) \endcode</td>
 *     <td>\code{.cpp} bool \endcode</td>
 *     <td>
 *      Tries to pop an element from the stack. Returns \c false if the stack is
 *      empty, otherwise \c true. In the latter case, the popped element is 
 *      stored in \c element which must be passed by reference.
 *     </td>
 *   </tr>
 * </table>
 *
 * \}
 */

/**
 * \defgroup CPP_CONTAINERS_STACKS Stacks
 * Concurrent stacks
 *
 * \ingroup CPP_CONTAINERS
 *
 * \see CPP_CONCEPTS_STACK
 */

namespace embb {
namespace containers {
namespace internal {
/**
 * Stack node
 *
 * Single linked list, contains the element (\c element) and a pointer to the
 * next node (\c next).
 *
 * \tparam T Element type
 */
template< typename T >
class LockFreeStackNode {
 private:
  /**
   * Pointer to the next node
   */
  LockFreeStackNode< T >* next;

  /**
   * The stored element
   */
  T element;

 public:
  /**
   * Creates a stack node
   */
  LockFreeStackNode(
    T const& element
    /**< [IN] The element of this stack node */
  );

  /**
   * Sets the next node
   */
  void SetNext(
    LockFreeStackNode< T >* next
    /**< [IN] Pointer to the next node */
  );

  /**
   * Returns the next pointer
   *
   * \return The next pointer
   */
  LockFreeStackNode< T >* GetNext();

  /**
   * Returns the element held by this node
   */
  T GetElement();
};
} // namespace internal

/**
 * Lock-free stack
 *
 * \concept{CPP_CONCEPTS_STACK}
 *
 * \ingroup CPP_CONTAINERS_STACKS
 *
168
 * \tparam Type Type of the stack elements
169 170 171
 * \tparam ValuePool Type of the value pool used as basis for the ObjectPool
 *         which stores the elements.
 */
172
template< typename Type,
173
typename ValuePool = embb::containers::LockFreeTreeValuePool < bool, false > >
174 175 176 177 178 179 180 181 182 183 184 185
class LockFreeStack {
 private:
  /**
   * The capacity of the stack. It is guaranteed that the stack can hold at
   * least as many elements, maybe more.
   */
  size_t capacity;

  /**
   * Callback to the method that is called by hazard pointers if a pointer is
   * not hazardous anymore, i.e., can safely be reused.
   */
186
  embb::base::Function<void, internal::LockFreeStackNode<Type>*>
187 188 189 190 191 192
    delete_pointer_callback;

  /**
   * The callback function, used to cleanup non-hazardous pointers.
   * \see delete_pointer_callback
   */
193
  void DeletePointerCallback(internal::LockFreeStackNode<Type>* to_delete);
194 195 196

  /**
   * The object pool, used for lock-free memory allocation.
197 198 199 200 201 202
   *
   * Warning: the objectPool has to be initialized before the hazardPointer
   * object, to be sure that the hazardPointer object is destructed before the
   * Pool as the hazardPointer object might return elements to the pool in its
   * destructor. So the ordering of the members objectPool and hazardPointer is
   * important here!
203
   */
204
  ObjectPool< internal::LockFreeStackNode<Type>, ValuePool > objectPool;
205 206

  /**
207 208 209 210 211 212 213 214 215 216 217
   * Definition of the used hazard pointer type
   */
  typedef internal::HazardPointer < internal::LockFreeStackNode<Type>* >
    StackNodeHazardPointer_t;

  /**
   * The hazard pointer object, used for memory management.
   */
  StackNodeHazardPointer_t hazardPointer;

  /**
218 219
   * Atomic pointer to the top node of the stack (element that is popped next)
   */
220
  embb::base::Atomic<internal::LockFreeStackNode<Type>*> top;
221 222 223 224 225 226 227 228

 public:
  /**
   * Creates a stack with the specified capacity.
   *
   * \memory
   * Let \c t be the maximum number of threads and \c x be <tt>1.25*t+1</tt>.
   * Then, <tt>x*(3*t+1)</tt> elements of size <tt>sizeof(void*)</tt>, \c x
229 230
   * elements of size <tt>sizeof(Type)</tt>, and \c capacity elements of size
   * <tt>sizeof(Type)</tt> are allocated.
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
   *
   * \notthreadsafe
   *
   * \see CPP_CONCEPTS_STACK
   */
  LockFreeStack(
    size_t capacity
    /**< [IN] Capacity of the stack */
  );

  /**
   * Returns the capacity of the stack.
   *
   * \return Number of elements the stack can hold.
   *
   * \waitfree
   */
  size_t GetCapacity();

  /**
   * Destroys the stack.
   *
   * \notthreadsafe
   */
  ~LockFreeStack();

  /**
   * Tries to push an element onto the stack.
   *
   * \return \c true if the element could be pushed, \c false if the stack is
   * full.
   *
   * \lockfree
   *
   * \note It might be possible to push more elements onto the stack than its
   * capacity permits.
   *
   * \see CPP_CONCEPTS_STACK
   */
  bool TryPush(
271
    Type const& element
272 273 274 275 276 277 278 279 280 281 282 283 284 285
    /**< [IN] Const reference to the element that shall be pushed */
  );

  /**
   * Tries to pop an element from the stack.
   *
   * \return \c true if an element could be popped, \c false if the stack is
   * empty.
   *
   * \lockfree
   *
   * \see CPP_CONCEPTS_STACK
   */
  bool TryPop(
286
    Type & element
287 288 289 290 291 292 293 294 295 296 297
    /**< [IN,OUT] Reference to the popped element. Unchanged, if the operation
                  was not successful. */
  );
};

} // namespace containers
} // namespace embb

#include <embb/containers/internal/lock_free_stack-inl.h>

#endif  // EMBB_CONTAINERS_LOCK_FREE_STACK_H_