/usr/include/flext/lockfree/fifo.hpp is in pd-flext-dev 0.6.0+git20161101.1.01318a94-3.
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 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 | // $Id$
//
// lock-free fifo queue from
// Michael, M. M. and Scott, M. L.,
// "simple, fast and practical non-blocking and blocking concurrent queue algorithms"
//
// intrusive implementation for c++
//
// 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_FIFO_HPP
#define __LOCKFREE_FIFO_HPP
#include "cas.hpp"
#include "atomic_ptr.hpp"
#include "branch_hints.hpp"
#ifdef HAVE_BOOST
# include <boost/type_traits.hpp>
# include <boost/static_assert.hpp>
#else /* HAVE_BOOST */
# ifdef BOOST_STATIC_ASSERT
# undef BOOST_STATIC_ASSERT
# endif
# define BOOST_STATIC_ASSERT(x)
#endif /* HAVE_BOOST */
#include <memory>
namespace lockfree
{
struct intrusive_fifo_node;
typedef atomic_ptr<intrusive_fifo_node> intrusive_fifo_ptr_t;
struct intrusive_fifo_node
{
intrusive_fifo_ptr_t next;
struct fifo_node * data;
};
struct fifo_node
{
intrusive_fifo_node *volatile node;
protected:
fifo_node(void)
{
node = new intrusive_fifo_node();
}
~fifo_node(void)
{
delete node;
}
template <class T> friend class intrusive_fifo;
};
template <typename T>
class intrusive_fifo
{
BOOST_STATIC_ASSERT((boost::is_base_of<fifo_node,T>::value));
public:
intrusive_fifo(void)
{
/* dummy pointer for head/tail */
intrusive_fifo_node * dummy = new intrusive_fifo_node();
dummy->next(NULL,0);
head_(dummy,0);
tail_(dummy,0);
}
~intrusive_fifo(void)
{
/* client must have freed all members already */
assert (empty());
delete head_.getPtr();
}
bool empty() const
{
return head_.getPtr() == tail_.getPtr() || (!tail_.getPtr());
}
void enqueue(T * instance)
{
/* volatile */ intrusive_fifo_node * node = static_cast<fifo_node*>(instance)->node;
node->next.setPtr(NULL);
node->data = static_cast<fifo_node*>(instance);
for (;;)
{
intrusive_fifo_ptr_t tail(tail_);
memory_barrier();
intrusive_fifo_ptr_t next(tail.getPtr()->next);
memory_barrier();
if (likely(tail == tail_))
{
if (next.getPtr() == 0)
{
if (tail.getPtr()->next.CAS(next,node))
{
tail_.CAS(tail,node);
return;
}
}
else
tail_.CAS(tail,next);
}
}
}
T* dequeue (void)
{
T * ret;
for (;;)
{
intrusive_fifo_ptr_t head(head_);
memory_barrier();
intrusive_fifo_ptr_t tail(tail_);
/* volatile */ intrusive_fifo_node * next = head.getPtr()->next.getPtr();
memory_barrier();
if (likely(head == head_))
{
if (head.getPtr() == tail.getPtr())
{
if (next == 0)
return 0;
tail_.CAS(tail,next);
}
else
{
ret = static_cast<T*>(next->data);
if (head_.CAS(head,next))
{
ret->node = head.getPtr();
return ret;
}
}
}
}
}
private:
intrusive_fifo_ptr_t head_,tail_;
};
template <typename T>
class fifo_value_node:
public fifo_node
{
public:
fifo_value_node(T const & v): value(v) {}
T value;
};
template <typename T, class Alloc = std::allocator<T> >
class fifo
: intrusive_fifo<fifo_value_node<T> >
{
public:
~fifo()
{
fifo_value_node<T> * node;
while((node = intrusive_fifo<fifo_value_node<T> >::dequeue()) != NULL)
free(node);
}
void enqueue(T const & v)
{
intrusive_fifo<fifo_value_node<T> >::enqueue(alloc(v));
}
bool dequeue (T & v)
{
fifo_value_node<T> * node = intrusive_fifo<fifo_value_node<T> >::dequeue();
if(!node)
return false;
v = node->value;
free(node);
return true;
}
private:
#if 0
inline fifo_value_node<T> *alloc(const T &k)
{
fifo_value_node<T> *node = allocator.allocate(1);
allocator.construct(node,k);
return node;
}
inline void free(fifo_value_node<T> *n)
{
assert(n);
allocator.destroy(n);
allocator.deallocate(n,1);
}
#else
// hmmm... static keyword brings 10% speedup...
static inline fifo_value_node<T> *alloc(const T &k)
{
return new fifo_value_node<T>(k);
}
static inline void free(fifo_value_node<T> *n)
{
assert(n);
delete n;
}
#endif
typename Alloc::template rebind<fifo_value_node<T> >::other allocator;
};
}
#endif /* __LOCKFREE_FIFO_HPP */
|