File: task_queue.hpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (388 lines) | stat: -rw-r--r-- 10,714 bytes parent folder | download | duplicates (4)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright (C) 2011 - 2017                                                  *
 * Dominik Charousset <dominik.charousset (at) haw-hamburg.de>                *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#pragma once

#include <utility>

#include "caf/config.hpp"

#include "caf/intrusive/forward_iterator.hpp"

namespace caf {
namespace intrusive {

/// A singly-linked FIFO queue for storing tasks of varying size. This queue is
/// used as a base type for concrete task abstractions such as `drr_queue` and
/// therefore has no dequeue functions.
template <class Policy>
class task_queue {
public:
  // -- member types ----------------------------------------------------------

  using policy_type = Policy;

  using value_type = typename policy_type::mapped_type;

  using node_type = typename value_type::node_type;

  using node_pointer = node_type*;

  using pointer = value_type*;

  using const_pointer = const value_type*;

  using reference = value_type&;

  using const_reference = const value_type&;

  using unique_pointer = typename policy_type::unique_pointer;

  using task_size_type = typename policy_type::task_size_type;

  using iterator = forward_iterator<value_type>;

  using const_iterator = forward_iterator<const value_type>;

  // -- static utility functions -----------------------------------------------

  /// Casts a node type to its value type.
  static pointer promote(node_pointer ptr) noexcept {
    return static_cast<pointer>(ptr);
  }

  // -- constructors, destructors, and assignment operators -------------------

  task_queue(policy_type p)
      : old_last_(nullptr),
        new_head_(nullptr),
        policy_(std::move(p)) {
    init();
  }

  task_queue(task_queue&& other) : task_queue(other.policy()) {
    if (other.empty()) {
      init();
    } else {
      head_.next = other.head_.next;
      tail_.next = other.tail_.next;
      tail_.next->next = &tail_;
      total_task_size_ = other.total_task_size_;
      other.init();
    }
  }

  task_queue& operator=(task_queue&& other) {
    deinit();
    if (other.empty()) {
      init();
    } else {
      head_.next = other.head_.next;
      tail_.next = other.tail_.next;
      tail_.next->next = &tail_;
      total_task_size_ = other.total_task_size_;
      other.init();
    }
    return *this;
  }

  virtual ~task_queue() {
    deinit();
  }

  // -- observers -------------------------------------------------------------

  /// Returns the policy object.
  policy_type& policy() noexcept {
    return policy_;
  }

  /// Returns the policy object.
  const policy_type& policy() const noexcept {
    return policy_;
  }

  /// Returns the accumulated size of all stored tasks.
  task_size_type total_task_size() const noexcept {
    return total_task_size_;
  }

  /// Returns whether the queue has no elements.
  bool empty() const noexcept {
    return total_task_size() == 0;
  }

  /// Peeks at the first element in the queue. Returns `nullptr` if the queue is
  /// empty.
  pointer peek() noexcept {
    auto ptr = head_.next;
    return ptr != &tail_ ? promote(ptr) : nullptr;
  }

  /// Applies `f` to each element in the queue.
  template <class F>
  void peek_all(F f) const {
    for (auto i = begin(); i != end(); ++i)
      f(*i);
  }

  // -- modifiers -------------------------------------------------------------

  /// Removes all elements from the queue.
  void clear() {
    deinit();
    init();
  }

  /// @private
  void inc_total_task_size(task_size_type x) noexcept {
    CAF_ASSERT(x > 0);
    total_task_size_ += x;
  }

  /// @private
  void inc_total_task_size(const value_type& x) noexcept {
    inc_total_task_size(policy_.task_size(x));
  }

  /// @private
  void dec_total_task_size(task_size_type x) noexcept {
    CAF_ASSERT(x > 0);
    total_task_size_ -= x;
  }

  /// @private
  void dec_total_task_size(const value_type& x) noexcept {
    dec_total_task_size(policy_.task_size(x));
  }

  /// @private
  task_size_type next_task_size() const noexcept {
    if (head_.next == nullptr)
      return 0;
    auto ptr = promote(head_.next);
    return policy_.task_size(*ptr);
  }

  /// @private
  unique_pointer next(task_size_type& deficit) noexcept {
    unique_pointer result;
    if (!empty()) {
      auto ptr = promote(head_.next);
      auto ts = policy_.task_size(*ptr);
      CAF_ASSERT(ts > 0);
      if (ts <= deficit) {
        deficit -= ts;
        total_task_size_ -= ts;
        head_.next = ptr->next;
        if (total_task_size_ == 0) {
          CAF_ASSERT(head_.next == &(tail_));
          deficit = 0;
          tail_.next = &(head_);
        }
        result.reset(ptr);
      }
    }
    return result;
  }

  // -- iterator access --------------------------------------------------------

  /// Returns an iterator to the dummy before the first element.
  iterator before_begin() noexcept {
    return &head_;
  }

  /// Returns an iterator to the dummy before the first element.
  const_iterator before_begin() const noexcept {
    return &head_;
  }

  /// Returns an iterator to the dummy before the first element.
  iterator begin() noexcept {
    return head_.next;
  }

  /// Returns an iterator to the dummy before the first element.
  const_iterator begin() const noexcept {
    return head_.next;
  }

  /// Returns an iterator to the dummy before the first element.
  const_iterator cbegin() const noexcept {
    return head_.next;
  }

  /// Returns a pointer to the dummy past the last element.
  iterator end() noexcept {
    return &tail_;
  }

  /// Returns a pointer to the dummy past the last element.
  const_iterator end() const noexcept {
    return &tail_;
  }

  /// Returns a pointer to the dummy past the last element.
  const_iterator cend() const noexcept {
    return &tail_;
  }

  // -- element access ---------------------------------------------------------

  /// Returns a pointer to the first element.
  pointer front() noexcept {
    return promote(head_.next);
  }

  /// Returns a pointer to the last element.
  pointer back() noexcept {
    return promote(tail_.next);
  }

  // -- insertion --------------------------------------------------------------

  /// Appends `ptr` to the queue.
  /// @pre `ptr != nullptr`
  bool push_back(pointer ptr) noexcept {
    CAF_ASSERT(ptr != nullptr);
    tail_.next->next = ptr;
    tail_.next = ptr;
    ptr->next = &tail_;
    inc_total_task_size(*ptr);
    return true;
  }

  /// Appends `ptr` to the queue.
  /// @pre `ptr != nullptr`
  bool push_back(unique_pointer ptr) noexcept {
    return push_back(ptr.release());
  }

  /// Creates a new element from `xs...` and appends it.
  template <class... Ts>
  bool emplace_back(Ts&&... xs) {
    return push_back(new value_type(std::forward<Ts>(xs)...));
  }

  /// Transfers all element from `other` to the front of this queue.
  template <class Container>
  void prepend(Container& other) {
    if (other.empty())
      return;
    if (empty()) {
      *this = std::move(other);
      return;
    }
    other.back()->next = head_.next;
    head_.next = other.front();
    inc_total_task_size(other.total_task_size());
    other.init();
  }

  /// Transfers all element from `other` to the back of this queue.
  template <class Container>
  void append(Container& other) {
    if (other.empty())
      return;
    if (empty()) {
      *this = std::move(other);
      return;
    }
    back()->next = other.front();
    other.back()->next = &tail_;
    tail_.next = other.back();
    inc_total_task_size(other.total_task_size());
    other.init();
  }

  /// Allows to insert a LIFO-ordered sequence at the end of the queue by
  /// calling this member function multiple times. Converts the order to FIFO
  /// on the fly.
  /// @warning leaves the queue in an invalid state until calling
  ///          `stop_lifo_append`.
  /// @private
  void lifo_append(node_pointer ptr) {
    if (old_last_ == nullptr) {
      old_last_ = back();
      push_back(promote(ptr));
    } else {
      ptr->next = new_head_;
      inc_total_task_size(*promote(ptr));
    }
    new_head_ = ptr;
  }

  /// Restores a consistent state of the queue after calling `lifo_append`.
  /// @private
  void stop_lifo_append() {
    if (old_last_ != nullptr) {
      CAF_ASSERT(new_head_ != nullptr);
      old_last_->next = new_head_;
      old_last_ = nullptr;
    }
  }

  // -- construction and destruction helper -----------------------------------

  /// Restores a consistent, empty state.
  /// @private
  void init() noexcept {
    head_.next = &tail_;
    tail_.next = &head_;
    total_task_size_ = 0;
  }

  /// Deletes all elements.
  /// @warning leaves the queue in an invalid state until calling `init` again.
  /// @private
  void deinit() noexcept {
    for (auto i = head_.next; i != &tail_;) {
      auto ptr = i;
      i = i->next;
      typename unique_pointer::deleter_type d;
      d(promote(ptr));
    }
  }

protected:
  // -- member variables ------------------------------------------------------

  /// node element pointing to the first element.
  node_type head_;

  /// node element pointing past the last element.
  node_type tail_;

  /// Stores the total size of all items in the queue.
  task_size_type total_task_size_;

  /// Used for LIFO -> FIFO insertion.
  node_pointer old_last_;

  /// Used for LIFO -> FIFO insertion.
  node_pointer new_head_;

  /// Manipulates instances of T.
  policy_type policy_;
};

} // namespace intrusive
} // namespace caf