lock_free_tree_value_pool.h 9.64 KB
Newer Older
1 2 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
/*
 * Copyright (c) 2014, Siemens AG. All rights reserved.
 *
 * 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_TREE_VALUE_POOL_H_
#define EMBB_CONTAINERS_LOCK_FREE_TREE_VALUE_POOL_H_

#include <embb/base/atomic.h>
#include <embb/base/memory_allocation.h>

namespace embb {
namespace containers {
/**
 * Lock-free value pool using binary tree construction.
 *
 * \concept{CPP_CONCEPTS_VALUE_POOL}
 *
 * \ingroup CPP_CONTAINERS_POOLS
 *
 * \see WaitFreeArrayValuePool
 *
 * \tparam T Element type (must support atomic operations such as \c int).
 * \tparam Undefined Bottom element (cannot be stored in the pool)
 * \tparam PoolAllocator Allocator used to allocate the pool array
 * \tparam TreeAllocator Allocator used to allocate the array representing the
 *         binary tree.
 */
template<typename T,
  T Undefined,
class PoolAllocator = embb::base::Allocator< embb::base::Atomic<T> >,
class TreeAllocator = embb::base::Allocator < embb::base::Atomic<int> >
>
class LockFreeTreeValuePool {
  /*
   * Description of the algorithm:
   *
   * The binary tree is split into two parts, the leaves and the binary tree
   * above the leaves. Example:
   *
   *                   b
   *               b       b
   *             b   b   b   b
   *            l l l l l l l l
   *
   * The elements b are the elements "above", the leaves l are the pool
   * elements. The elements b are represented by the array tree, the elements l
   * be the array pool.
   *
   * A binary tree for storing n elements has k = 2^j leaves, where j is the
   * smallest number such that n <= 2^j holds. The variable with name size
   * stores k. The variable tree_size holds the number of elements b, the tree
   * above. It is size - 1. The array pool is not constructed with size k, but
   * with the real size n, as only the first n elements from the pool array are
   * accessed. The tree is used as if elements l and b form a binary tree
   * together and would be stored in one array. That makes the algorithm
   * easier.
   *
   * The elements b store the number of not allocated cells below. The pool
   * elements l are either not allocated, and store the respective element, or
   * are allocated and contain the element Undefined.
   *
   * A tree storing the elements a,b,c,d,e would therefore look like this:
   *
   *                   8
   *               4       1
   *             2   2   1   0
   *  pool[]:   a b c d e
   *
   * tree = {8,4,1,2,2,1,0}
   * pool = {'a','b','c','d','e'}
   * size = 8
   * tree_size = 7
   *
   * The algorithm for allocating an element starts at the root node and
   * recursively traverses the tree. It tries to decrement a node (a decrement
   * is actually a conditional decrement, i.e., a node is decremented iff the
   * result is not less than 0. This is the place, where the algorithm is not
   * wait-free anymore, as this cannot be implemented atomically.) and if
   * successful, calls itself on the left child, if not successful, on the right
   * child. If the algorithm encounters a leaf, it tries to reserve it by doing
   * a CAS to Undefined. If that is successful, the element together with its
   * pool index are returned. Otherwise, no element is returned.
   *
   * The algorithm for freeing an element is much more simple. The element is
   * stored at the pool position it was allocated from and then, the algorithm
   * walks the tree towards the root, thereby increasing the value of each
   * visited node.
   *
   * For future work, the memory consumption could be further reduced by
   * "stripping" away unused cells in the binary tree. In the example above,
   * that would be cell 0 in the row "2   2   1   0".
   */
 private:
  // Private constructor
  LockFreeTreeValuePool();

  // Prevent copy-construction
  LockFreeTreeValuePool(const LockFreeTreeValuePool&);

  // Prevent assignment
  LockFreeTreeValuePool& operator=(const LockFreeTreeValuePool&);

  // See algorithm description above
  int size;

  // See algorithm description above
  int tree_size;

  // See algorithm description above
  int real_size;

  // The tree above the pool
  embb::base::Atomic<int>* tree;

  // The actual pool
  embb::base::Atomic<T>* pool;

  PoolAllocator poolAllocator;
  TreeAllocator treeAllocator;

  /**
   * Computes smallest power of two fitting the specified value
   *
   * \return Let k be the smallest number so that value <= 2^k. Return 2^k.
   */
  static int GetSmallestPowerByTwoValue(int value);

  /**
   * Checks if a given node is a leaf
   *
   * \return \c true if node is a leaf, otherwise \c false
   */
  bool IsLeaf(
    int node
    /**< [IN] Node index */
  );

  /**
   * Checks if node is valid
   *
   * \return \c true if node is valid, otherwise \c false.
   */
  bool IsValid(
    int node
    /**< [IN] Node index */
  );

  /**
   * Gets the index of the left child.
   *
   * \pre The node has to be valid
   * \return Index of the left child
   */
  int GetLeftChildIndex(
    int node
    /**< [IN] Node index */
  );

  /**
   * Gets the index of the right child.
   *
   * \pre The node has to be valid
   * \return Index of the right child
   */
  int GetRightChildIndex(
    int node
    /**< [IN] Node index */
  );

  /**
   * The leaves represent the pool. They are indexed from 0 to pool_size. If
   * a node is a leaf, this index is returned.
   */
  int NodeIndexToPoolIndex(
    int node
    /**< [IN] Node index */
  );

  /**
   * Gets the leaf node index for a pool index.
   * Inverse function for NodeIndexToPoolIndex.
   */
  int PoolIndexToNodeIndex(int index);

  /**
   * Checks if node index is root
   *
   * \return \c true if node index is root element, otherwise \c false
   */
  bool IsRoot(
    int node
    /**< [IN] Node index */
  );

  /**
   * Get the parent node.
   *
   * \return Index of parent node
   */
  int GetParentNode(
    int node
    /**< [IN] Node index */
  );

  /**
   * Tries to allocate an element starting from node \c node. If successful,
   * the index of the node is returned and the allocated element is written to
   * \c element. If the allocation is not successful, -1 is returned.
   *
   * \return Index of allocated element, or -1 if not successful.
   *
   */
  int allocate_rec(
    int node,
    /**< [IN] Node index */
    T& element
    /**< [IN,OUT] Allocated element, if there is any */
  );

  /**
   * Instead of "freeing" each element for initializing the tree, we do this in
   * one pass, as this is much faster. Supposed to be called recursively from
   * the root node. Does not initialize the pool elements, only the managing
   * binary tree.
   */
  void Fill(
    int node,
    /**< [IN] Node index */
    int elementsToStore,
    /**< [IN] Number of elements to be stored in the current branch */
    int power2Value
    /**< [IN] Maximum number of elements this branch can hold */
  );

 public:
  /**
   * Constructs a pool and fills it with the elements in the specified range.
   *
   * \memory Let <tt>n = \c std::distance(first, last))</tt> and \c k be the
   * minimum number such that <tt>n <= 2^k holds</tt>. Then,
   * <tt>((2^k)-1) * sizeof(embb::Atomic<int>) + n*sizeof(embb::Atomic<T>)</tt>
   * bytes of memory are allocated.
   *
   * \notthreadsafe
   *
   * \see CPP_CONCEPTS_VALUE_POOL
   */
  template<typename ForwardIterator>
  LockFreeTreeValuePool(
    ForwardIterator first,
    /**< [IN] Iterator pointing to the first element of the range the pool is
              filled with */
    ForwardIterator last
    /**< [IN] Iterator pointing to the last plus one element of the range the
              pool is filled with */
  );

  /**
   * Destructs the pool.
   *
   * \notthreadsafe
   */
  ~LockFreeTreeValuePool();

  /**
   * Allocates an element from the pool.
   *
   * \return Index of the element if the pool is not empty, otherwise \c -1.
   *
   * \lockfree
   *
   * \see CPP_CONCEPTS_VALUE_POOL
   */
  int Allocate(
    T & element
    /**< [IN,OUT] Reference to the allocated element. Unchanged, if the
                  operation was not successful. */
  );

  /**
   * Returns an element to the pool.
   *
   * \note The element must have been allocated with Allocate().
   *
   * \lockfree
   *
   * \see CPP_CONCEPTS_VALUE_POOL
   */
  void Free(
    T element,
    /**< [IN] Element to be returned to the pool */
    int index
    /**< [IN] Index of the element as obtained by Allocate() */
  );
};
} // namespace containers
} // namespace embb

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

#endif  // EMBB_CONTAINERS_LOCK_FREE_TREE_VALUE_POOL_H_