-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_queue.hpp
More file actions
142 lines (120 loc) · 3.77 KB
/
sync_queue.hpp
File metadata and controls
142 lines (120 loc) · 3.77 KB
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
#pragma once
#include <deque>
#include "memory.hpp"
#include "monitor.hpp"
#include "optional.hpp"
#include "semaphore.hpp"
namespace ift630 {
/// Unbounded queue that never blocks the callers of push() and pop() for longer than the time of
/// insertion into the queue
template <typename T>
class AsyncQueue {
Monitor<std::deque<T>> monitor{};
public:
AsyncQueue() noexcept = default;
AsyncQueue(AsyncQueue&&) noexcept = default;
AsyncQueue(const AsyncQueue&) = delete;
~AsyncQueue() noexcept = default;
auto operator=(AsyncQueue&&) noexcept -> AsyncQueue& = default;
auto operator=(const AsyncQueue&) -> AsyncQueue& = delete;
void push(const T& item) {
constexpr auto push_fn = [&item](auto& q) { q.push_back(item); };
monitor(push_fn);
}
void push(T&& item) {
const auto push_fn = [&item](auto& q) { q.push_back(std::forward<T>(item)); };
monitor(push_fn);
}
template <typename... Args>
void emplace(Args&&... args) {
const auto emplace_fn = [&args...](auto& q) { q.emplace_back(std::forward<Args>(args)...); };
monitor(emplace_fn);
}
auto pop() noexcept -> optional<T> {
constexpr auto pop_front_fn = [](auto& q) -> optional<T> {
if (q.empty()) {
return none;
}
const auto item = T{q.front()};
q.pop_front();
return item;
};
return monitor(pop_front_fn);
}
[[nodiscard]] auto empty() const noexcept -> bool {
constexpr auto is_empty_fn = [](const auto& q) { return q.empty(); };
return monitor(is_empty_fn);
}
};
/// Bounded queue which blocks the callers of pop() if the queue is empty, and the callers of push()
/// when the queue is full
template <typename T>
class SyncQueue {
Monitor<std::deque<T>> monitor{};
MoveSemaphore full_semaphore;
MoveSemaphore empty_semaphore;
const size_t capacity;
public:
explicit SyncQueue(size_t size) noexcept : full_semaphore{size}, capacity{size} {}
SyncQueue(const SyncQueue&) = delete;
SyncQueue(SyncQueue&&) noexcept = default;
~SyncQueue() noexcept = default;
auto operator=(const SyncQueue&) -> SyncQueue& = delete;
auto operator=(SyncQueue&&) noexcept -> SyncQueue& = default;
void push(const T& item) {
full_semaphore.wait();
const auto push_fn = [&item](auto& q) { q.push_back(item); };
monitor(push_fn);
empty_semaphore.signal();
}
void push(T&& item) {
full_semaphore.wait();
const auto push_fn = [&item](auto& q) { q.push_back(std::forward<T>(item)); };
monitor(push_fn);
empty_semaphore.signal();
}
template <typename... Args>
void emplace(Args&&... args) {
full_semaphore.wait();
const auto emplace_fn = [&args...](auto& q) { q.emplace_back(std::forward<Args>(args)...); };
monitor(emplace_fn);
empty_semaphore.signal();
}
auto pop() noexcept -> T {
empty_semaphore.wait();
constexpr auto pop_front_fn = [](auto& q) {
const auto item = T{q.front()};
q.pop_front();
return item;
};
const auto item = monitor(pop_front_fn);
full_semaphore.signal();
return item;
}
auto try_pop() noexcept -> optional<T> {
constexpr auto pop_front_fn = [](auto& q) -> optional<T> {
if (q.empty()) {
return none;
}
const auto item = T{q.front()};
q.pop_front();
return item;
};
const auto item = monitor(pop_front_fn);
if (item.has_value()) {
full_semaphore.signal();
}
return item;
}
[[nodiscard]] auto empty() const noexcept -> bool {
constexpr auto is_empty_fn = [](const auto& q) { return q.empty(); };
return monitor(is_empty_fn);
}
[[nodiscard]] auto full() const noexcept -> bool {
constexpr auto is_full_fn = [capacity = this->capacity](const auto& q) {
return q.size() == capacity;
};
return monitor(is_full_fn);
}
};
} // namespace ift630