Cpp-Taskflow  2.2.0
spmc_queue.hpp
1 // 2019/05/15 - created by Tsung-Wei Huang
2 // - isolated from the original workstealing executor
3 
4 #pragma once
5 
6 #include <atomic>
7 #include <vector>
8 #include <optional>
9 
10 namespace tf {
11 
28 template <typename T>
30 
31  //constexpr static int64_t cacheline_size = 64;
32 
33  //using storage_type = std::aligned_storage_t<sizeof(T), cacheline_size>;
34 
35  struct Array {
36 
37  int64_t C;
38  int64_t M;
39  //storage_type* S;
40  T* S;
41 
42  explicit Array(int64_t c) :
43  C {c},
44  M {c-1},
45  //S {new storage_type[C]} {
46  S {new T[static_cast<size_t>(C)]} {
47  //for(int64_t i=0; i<C; ++i) {
48  // ::new (std::addressof(S[i])) T();
49  //}
50  }
51 
52  ~Array() {
53  //for(int64_t i=0; i<C; ++i) {
54  // reinterpret_cast<T*>(std::addressof(S[i]))->~T();
55  //}
56  delete [] S;
57  }
58 
59  int64_t capacity() const noexcept {
60  return C;
61  }
62 
63  template <typename O>
64  void push(int64_t i, O&& o) noexcept {
65  //T* ptr = reinterpret_cast<T*>(std::addressof(S[i & M]));
66  //*ptr = std::forward<O>(o);
67  S[i & M] = std::forward<O>(o);
68  }
69 
70  T pop(int64_t i) noexcept {
71  //return *reinterpret_cast<T*>(std::addressof(S[i & M]));
72  return S[i & M];
73  }
74 
75  Array* resize(int64_t b, int64_t t) {
76  Array* ptr = new Array {2*C};
77  for(int64_t i=t; i!=b; ++i) {
78  ptr->push(i, pop(i));
79  }
80  return ptr;
81  }
82 
83  };
84 
86  std::atomic<int64_t> _bottom;
87  std::atomic<Array*> _array;
88  std::vector<Array*> _garbage;
89  //char _padding[cacheline_size];
90 
91  public:
92 
98  explicit WorkStealingQueue(int64_t capacity = 1024);
99 
104 
108  bool empty() const noexcept;
109 
113  size_t size() const noexcept;
114 
118  int64_t capacity() const noexcept;
119 
131  template <typename O>
132  void push(O&& item);
133 
140  std::optional<T> pop();
141 
148  std::optional<T> steal();
149 };
150 
151 // Constructor
152 template <typename T>
154  assert(c && (!(c & (c-1))));
155  _top.store(0, std::memory_order_relaxed);
156  _bottom.store(0, std::memory_order_relaxed);
157  _array.store(new Array{c}, std::memory_order_relaxed);
158  _garbage.reserve(32);
159 }
160 
161 // Destructor
162 template <typename T>
164  for(auto a : _garbage) {
165  delete a;
166  }
167  delete _array.load();
168 }
169 
170 // Function: empty
171 template <typename T>
172 bool WorkStealingQueue<T>::empty() const noexcept {
173  int64_t b = _bottom.load(std::memory_order_relaxed);
174  int64_t t = _top.load(std::memory_order_relaxed);
175  return b <= t;
176 }
177 
178 // Function: size
179 template <typename T>
180 size_t WorkStealingQueue<T>::size() const noexcept {
181  int64_t b = _bottom.load(std::memory_order_relaxed);
182  int64_t t = _top.load(std::memory_order_relaxed);
183  return static_cast<size_t>(b >= t ? b - t : 0);
184 }
185 
186 // Function: push
187 template <typename T>
188 template <typename O>
190  int64_t b = _bottom.load(std::memory_order_relaxed);
191  int64_t t = _top.load(std::memory_order_acquire);
192  Array* a = _array.load(std::memory_order_relaxed);
193 
194  // queue is full
195  if(a->capacity() - 1 < (b - t)) {
196  Array* tmp = a->resize(b, t);
197  _garbage.push_back(a);
198  std::swap(a, tmp);
199  _array.store(a, std::memory_order_relaxed);
200  }
201 
202  a->push(b, std::forward<O>(o));
203  std::atomic_thread_fence(std::memory_order_release);
204  _bottom.store(b + 1, std::memory_order_relaxed);
205 }
206 
207 // Function: pop
208 template <typename T>
209 std::optional<T> WorkStealingQueue<T>::pop() {
210  int64_t b = _bottom.load(std::memory_order_relaxed) - 1;
211  Array* a = _array.load(std::memory_order_relaxed);
212  _bottom.store(b, std::memory_order_relaxed);
213  std::atomic_thread_fence(std::memory_order_seq_cst);
214  int64_t t = _top.load(std::memory_order_relaxed);
215 
216  std::optional<T> item;
217 
218  if(t <= b) {
219  item = a->pop(b);
220  if(t == b) {
221  // the last item just got stolen
222  if(!_top.compare_exchange_strong(t, t+1,
223  std::memory_order_seq_cst,
224  std::memory_order_relaxed)) {
225  item = std::nullopt;
226  }
227  _bottom.store(b + 1, std::memory_order_relaxed);
228  }
229  }
230  else {
231  _bottom.store(b + 1, std::memory_order_relaxed);
232  }
233 
234  return item;
235 }
236 
237 // Function: steal
238 template <typename T>
239 std::optional<T> WorkStealingQueue<T>::steal() {
240  int64_t t = _top.load(std::memory_order_acquire);
241  std::atomic_thread_fence(std::memory_order_seq_cst);
242  int64_t b = _bottom.load(std::memory_order_acquire);
243 
244  std::optional<T> item;
245 
246  if(t < b) {
247  Array* a = _array.load(std::memory_order_consume);
248  item = a->pop(t);
249  if(!_top.compare_exchange_strong(t, t+1,
250  std::memory_order_seq_cst,
251  std::memory_order_relaxed)) {
252  return std::nullopt;
253  }
254  }
255 
256  return item;
257 }
258 
259 // Function: capacity
260 template <typename T>
261 int64_t WorkStealingQueue<T>::capacity() const noexcept {
262  return _array.load(std::memory_order_relaxed)->capacity();
263 }
264 
265 } // end of namespace tf -----------------------------------------------------
bool empty() const noexcept
queries if the queue is empty at the time of this call
Definition: spmc_queue.hpp:172
~WorkStealingQueue()
destructs the queue
Definition: spmc_queue.hpp:163
void push(O &&item)
inserts an item to the queue
Definition: spmc_queue.hpp:189
Definition: taskflow.hpp:5
size_t size() const noexcept
queries the number of items at the time of this call
Definition: spmc_queue.hpp:180
int64_t capacity() const noexcept
queries the capacity of the queue
Definition: spmc_queue.hpp:261
std::optional< T > pop()
pops out an item from the queue
Definition: spmc_queue.hpp:209
Lock-free unbounded single-producer multiple-consumer queue.
Definition: spmc_queue.hpp:29
std::optional< T > steal()
steals an item from the queue
Definition: spmc_queue.hpp:239
WorkStealingQueue(int64_t capacity=1024)
constructs the queue with a given capacity
Definition: spmc_queue.hpp:153