lock_free_tree_value_pool-inl.h 9.05 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2015, 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
 *
 * 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_INTERNAL_LOCK_FREE_TREE_VALUE_POOL_INL_H_
#define EMBB_CONTAINERS_INTERNAL_LOCK_FREE_TREE_VALUE_POOL_INL_H_

namespace embb {
namespace containers {
32 33 34
template<typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
int LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
35 36 37 38 39 40
GetSmallestPowerByTwoValue(int value) {
  int result = 1;
  while (result < value) result <<= 1;
  return result;
}

41 42 43 44
template<typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
bool LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
IsLeaf(int node) {
45
  if (node >= size_ - 1 && node <= 2 * size_ - 1) {
46 47 48 49 50
    return true;
  }
  return false;
}

51 52 53
template<typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
bool LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
54
IsValid(int node) {
55
  return (node >= 0 && node <= 2 * size_ - 1);
56 57
}

58 59 60
template<typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
int LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
61 62 63 64 65 66
GetLeftChildIndex(int node) {
  int index = 2 * node + 1;
  assert(IsValid(index));
  return index;
}

67 68 69
template<typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
int LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
70 71 72 73 74 75 76 77 78 79
GetRightChildIndex(int node) {
  int index = 2 * node + 2;
  assert(IsValid(index));
  return index;
}

template<typename T, T Undefined, class PoolAllocator, class TreeAllocator >
int LockFreeTreeValuePool<T, Undefined, PoolAllocator, TreeAllocator>::
NodeIndexToPoolIndex(int node) {
  assert(IsLeaf(node));
80
  return(node - (size_ - 1));
81 82
}

83 84 85
template<typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
int LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
86
PoolIndexToNodeIndex(int index) {
87
  int node = index + (size_ - 1);
88 89 90 91
  assert(IsLeaf(node));
  return node;
}

92 93 94
template<typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
bool LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
95 96 97 98 99 100 101 102
IsRoot(int node) {
  return(0 == node);
}

template<typename T, T Undefined, class PoolAllocator, class TreeAllocator >
int LockFreeTreeValuePool<T, Undefined, PoolAllocator, TreeAllocator>::
GetParentNode(int node) {
  int parent = (node - 1) / 2;
103
  assert(parent >= 0 && parent < size_ - 1);
104 105 106
  return parent;
}

107 108 109 110
template<typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
int LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
allocate_rec(int node, Type& element) {
111 112 113 114
  // If we are a leaf, we try to allocate a cell using CAS.
  if (IsLeaf(node)) {
    int pool_index = NodeIndexToPoolIndex(node);

115
    Type expected = pool_[pool_index];
116 117 118
    if (expected == Undefined)
      return -1;

119
    if (pool_[pool_index].CompareAndSwap(expected, Undefined)) {
120 121 122 123 124 125 126 127 128 129 130
      element = expected;
      return pool_index;
    }

    return -1;
  }

  int current;
  int desired;
  // Try to decrement node value.
  // This is the point, where the algorithm becomes not wait-free. We have to
131
  // atomically decrement the value in the node if the result is greater than
132 133
  // or equal to zero. This cannot be done atomically.
  do {
134
    current = tree_[node];
135 136 137
    desired = current - 1;
    if (desired < 0)
      return -1;
138
  } while (!tree_[node].CompareAndSwap(current, desired));
139 140 141 142 143 144 145 146 147 148 149 150 151

  int leftResult = allocate_rec(GetLeftChildIndex(node), element);
  if (leftResult != -1) {
    return leftResult;
  }
  int rightResult = (allocate_rec(GetRightChildIndex(node), element));
  // We are guaranteed to be successful either in the left or the right branch.
  // It should not happen that we cannot allocate in either branch.
  assert(rightResult != -1);

  return rightResult;
}

152 153 154
template<typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
void LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
155 156 157 158
Fill(int node, int elementsToStore, int power2Value) {
  if (IsLeaf(node))
    return;

159
  tree_[node] = elementsToStore;
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

  int postPower2Value = power2Value >> 1;

  // Value fits in left cell, don't bother with right cells
  if (elementsToStore <= postPower2Value) {
    Fill(GetLeftChildIndex(node), elementsToStore, power2Value >> 1);
  } else {
    Fill(GetLeftChildIndex(node),
      postPower2Value,
      postPower2Value);

    Fill(GetRightChildIndex(node),
      elementsToStore - postPower2Value,
      postPower2Value);
  }
}

177 178 179 180
template<typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
int LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
Allocate(Type & element) {
181 182 183
  return allocate_rec(0, element);
}

184 185 186 187
template<typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
void LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
Free(Type element, int index) {
188 189 190
  assert(element != Undefined);

  // Put the element back
191
  pool_[index].Store(element);
192

193
  assert(index >= 0 && index < size_);
194 195 196 197
  int node = PoolIndexToNodeIndex(index);

  while (!IsRoot(node)) {
    node = GetParentNode(node);
198
    tree_[node].FetchAndAdd(1);
199 200 201
  }
}

202 203
template< typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
204
template< typename ForwardIterator >
205
LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
206 207
LockFreeTreeValuePool(ForwardIterator first, ForwardIterator last) {
  // Number of elements to store
208
  real_size_ = static_cast<int>(::std::distance(first, last));
209 210

  // Let k be smallest number so that real_size <= 2^k, size = 2^k
211
  size_ = GetSmallestPowerByTwoValue(real_size_);
212 213

  // Size of binary tree without the leaves
214
  tree_size_ = size_ - 1;
215

216
  // make sure, signed values are not negative
217 218
  assert(tree_size_ >= 0);
  assert(real_size_ >= 0);
219

220 221
  size_t tree_size_unsigned = static_cast<size_t>(tree_size_);
  size_t real_size_unsigned = static_cast<size_t>(real_size_);
222

223
  // Pool stores elements of type T
224
  pool_ = pool_allocator_.allocate(real_size_unsigned);
225 226 227

  // invoke inplace new for each pool element
  for (size_t i = 0; i != real_size_unsigned; ++i) {
228
    new (&pool_[i]) embb::base::Atomic<Type>();
229
  }
230 231

  // Tree holds the counter of not allocated elements
232
  tree_ = tree_allocator_.allocate(tree_size_unsigned);
233 234 235

  // invoke inplace new for each tree element
  for (size_t i = 0; i != tree_size_unsigned; ++i) {
236
    new (&tree_[i]) embb::base::Atomic<int>();
237
  }
238 239 240 241 242

  int i = 0;

  // Store the elements from the range
  for (ForwardIterator curIter(first); curIter != last; ++curIter) {
243
    pool_[i++] = *curIter;
244 245 246
  }

  // Initialize the binary tree without leaves (counters)
247
  Fill(0, static_cast<int>(::std::distance(first, last)), size_);
248 249
}

250 251 252
template<typename Type, Type Undefined, class PoolAllocator,
  class TreeAllocator >
LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
253
~LockFreeTreeValuePool() {
254 255
  size_t tree_size_unsigned = static_cast<size_t>(tree_size_);
  size_t real_size_unsigned = static_cast<size_t>(real_size_);
256

257
  pool_allocator_.deallocate(pool_, real_size_unsigned);
258 259 260

  // invoke destructor for each pool element
  for (size_t i = 0; i != real_size_unsigned; ++i) {
261
    pool_[i].~Atomic();
262 263
  }

264
  tree_allocator_.deallocate(tree_, tree_size_unsigned);
265 266 267

  // invoke destructor for each tree element
  for (size_t i = 0; i != tree_size_unsigned; ++i) {
268
    tree_[i].~Atomic();
269
  }
270
}
271

272 273 274 275 276 277 278 279
template<typename Type, Type Undefined, class PoolAllocator,
class TreeAllocator >
size_t LockFreeTreeValuePool<Type, Undefined, PoolAllocator, TreeAllocator>::
GetMinimumElementCountForGuaranteedCapacity(size_t capacity) {
  // for this value pool, this is just capacity...
  return capacity;
}

280 281 282 283
} // namespace containers
} // namespace embb

#endif  // EMBB_CONTAINERS_INTERNAL_LOCK_FREE_TREE_VALUE_POOL_INL_H_