/usr/include/flext/lockfree/stack.hpp is in pd-flext-dev 0.6.0+git20161101.1.01318a94-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // $Id$
//
// lock-free stack
//
// Copyright (C) 2007 Tim Blechmann & Thomas Grill
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// $Revision$
// $LastChangedRevision$
// $LastChangedDate$
// $LastChangedBy$
#ifndef __LOCKFREE_STACK_HPP
#define __LOCKFREE_STACK_HPP
#include "cas.hpp"
#include "atomic_ptr.hpp"
#include "branch_hints.hpp"
#include <memory> // for std::allocator
#if HAVE_BOOST
# include <boost/type_traits.hpp>
# include <boost/static_assert.hpp>
#else
# define BOOST_STATIC_ASSERT(x)
#endif
namespace lockfree
{
//! nodes for the intrusive_stack must be derived from that
class stack_node
{
template <class T> friend class intrusive_stack;
public:
stack_node(): next(NULL) {}
private:
atomic_ptr<stack_node> next;
};
//! intrusive lock-free stack implementation with T being the node type (inherited from stack_node)
template <typename T>
class intrusive_stack
{
BOOST_STATIC_ASSERT((boost::is_base_of<stack_node,T>::value));
public:
intrusive_stack(): head(NULL) {}
~intrusive_stack()
{
assert(empty());
}
bool empty() const { return !head.getPtr(); }
void push(T *node)
{
assert(!node->next.getPtr());
while(unlikely(!head.CAS(node->next = head,node)));
}
T *pop()
{
for(;;) {
atomic_ptr<stack_node> current(head);
T *node = static_cast<T *>(current.getPtr());
if(!node || likely(head.CAS(current,node->next.getPtr()))) {
if(node) node->next.setPtr(NULL);
return node;
}
}
}
private:
atomic_ptr<stack_node> head;
};
//! node type used by non-intrusive stack
template <typename T>
class stack_value_node
: public stack_node
{
public:
stack_value_node(T const &v): value(v) {}
T value;
};
//! non-intrusive lock-free stack
template <typename T,class Alloc = std::allocator<T> >
class stack
: intrusive_stack<stack_value_node<T> >
{
public:
~stack()
{
// delete remaining elements
stack_value_node<T> * node;
while((node = intrusive_stack<stack_value_node<T> >::pop()) != NULL)
free(node);
}
void push(T const &v)
{
intrusive_stack<stack_value_node<T> >::push(alloc(v));
}
bool pop(T &v)
{
stack_value_node<T> *node = intrusive_stack<stack_value_node<T> >::pop();
if(!node)
return false;
v = node->value;
free(node);
return true;
}
private:
inline stack_value_node<T> *alloc(const T &k)
{
stack_value_node<T> *node = allocator.allocate(1);
allocator.construct(node,k);
return node;
}
inline void free(stack_value_node<T> *n)
{
assert(n);
allocator.destroy(n);
allocator.deallocate(n,1);
}
typename Alloc::template rebind<stack_value_node<T> >::other allocator;
};
} // namespace
#endif
|