lock_free_stack-inl.h 5.1 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
 *
 * 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_STACK_INL_H_
#define EMBB_CONTAINERS_INTERNAL_LOCK_FREE_STACK_INL_H_

30 31
#include <embb/base/internal/config.h>

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
/*
 * The following algorithm uses hazard pointers and a lock-free value pool for
 * memory management. For a description of the algorithm, see
 * Maged M. Michael. "Hazard pointers: Safe memory reclamation for lock-free
 * objects". IEEE Transactions on Parallel and Distributed Systems, 15.6 (2004):
 * 491-504.
 */

namespace embb {
namespace containers {
namespace internal {
  template< typename T >
  LockFreeStackNode< T >::LockFreeStackNode(T const& element) :
    element(element) {
  }

  template< typename T >
  void LockFreeStackNode< T >::SetNext(LockFreeStackNode< T >* next) {
    this->next = next;
  }

  template< typename T >
  LockFreeStackNode< T >* LockFreeStackNode< T >::GetNext() {
    return next;
  }

  template< typename T >
  T LockFreeStackNode< T >::GetElement() {
    return element;
  }
} // namespace internal

64 65 66
template< typename Type, typename ValuePool >
void LockFreeStack< Type, ValuePool >::
DeletePointerCallback(internal::LockFreeStackNode<Type>* to_delete) {
67 68 69
  objectPool.Free(to_delete);
}

70 71
template< typename Type, typename ValuePool >
LockFreeStack< Type, ValuePool >::LockFreeStack(size_t capacity) :
72 73 74
capacity(capacity),
// Disable "this is used in base member initializer" warning.
// We explicitly want this.
75
#ifdef EMBB_PLATFORM_COMPILER_MSVC
76 77 78 79
#pragma warning(push)
#pragma warning(disable:4355)
#endif
  delete_pointer_callback(*this,
80
    &LockFreeStack<Type>::DeletePointerCallback),
81
#ifdef EMBB_PLATFORM_COMPILER_MSVC
82 83 84 85 86
#pragma warning(pop)
#endif
  // Object pool, size with respect to the maximum number of retired nodes not
  // eligible for reuse:
  objectPool(
87 88
  StackNodeHazardPointer_t::ComputeMaximumRetiredObjectCount(1) +
  capacity),
89
  hazardPointer(delete_pointer_callback, NULL, 1) {
90 91
}

92 93
template< typename Type, typename ValuePool >
size_t LockFreeStack< Type, ValuePool >::GetCapacity() {
94 95 96
  return capacity;
}

97 98
template< typename Type, typename ValuePool >
LockFreeStack< Type, ValuePool >::~LockFreeStack() {
99 100 101
  // Nothing to do here, did not allocate anything.
}

102 103 104
template< typename Type, typename ValuePool >
bool LockFreeStack< Type, ValuePool >::TryPush(Type const& element) {
  internal::LockFreeStackNode<Type>* newNode =
105 106 107 108 109 110 111
    objectPool.Allocate(element);

  // Stack full, cannot push
  if (newNode == NULL)
    return false;

  for (;;) {
112
    internal::LockFreeStackNode<Type>* top_cached = top;
113 114 115 116 117 118
    newNode->SetNext(top_cached);
    if (top.CompareAndSwap(top_cached, newNode))
      return true;
  }
}

119 120 121
template< typename Type, typename ValuePool >
bool LockFreeStack< Type, ValuePool >::TryPop(Type & element) {
  internal::LockFreeStackNode<Type>* top_cached = top;
122 123 124 125 126 127 128 129
  for (;;) {
    top_cached = top;

    // Stack empty, cannot pop
    if (top_cached == NULL)
      return false;

    // Guard top_cached
130
    hazardPointer.Guard(0, top_cached);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

    // Check if top is still top. If this is the case, it has not been
    // retired yet (because before retiring that thing, the retiring thread
    // would first have to update the top pointer). If not, we wait for the
    // next round.
    if (top != top_cached)
      continue;

    bool compare_and_swap_suc = top.CompareAndSwap(top_cached,
      top_cached->GetNext());

    if (compare_and_swap_suc) {
      break;
    } else {
      // We continue with the next and can unguard top_cached
146
      hazardPointer.Guard(0, NULL);
147 148 149
    }
  }

150
  Type data = top_cached->GetElement();
151 152

  // We don't need to read from this reference anymore, unguard it
153
  hazardPointer.Guard(0, NULL);
154

155
  hazardPointer.EnqueueForDeletion(top_cached);
156 157 158 159 160 161 162 163

  element = data;
  return true;
}
} // namespace containers
} // namespace embb

#endif  // EMBB_CONTAINERS_INTERNAL_LOCK_FREE_STACK_INL_H_