-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.h
157 lines (127 loc) · 4.11 KB
/
channel.h
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
// Copyright 2016 Regents of the University of Michigan
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef DADRIAN_CHANNEL_H
#define DADRIAN_CHANNEL_H
#include <atomic>
#include <cassert>
#include <condition_variable>
#include <queue>
#include <mutex>
namespace dadrian {
template <typename T>
class Channel {
private:
std::queue<T> m_queue;
mutable std::mutex m_queue_mutex;
mutable std::condition_variable m_queue_not_full;
mutable std::condition_variable m_queue_not_empty;
size_t m_max_size;
std::atomic<bool> m_closed;
friend class ChannelIterator;
public:
class ChannelIterator {
public:
using value_type = T;
private:
bool m_valid;
Channel& m_channel;
value_type m_value;
friend class Channel;
ChannelIterator(Channel& channel) : m_channel(channel), m_valid(true) {
m_channel.recv(*this);
}
public:
ChannelIterator(const ChannelIterator& other) = delete;
ChannelIterator(ChannelIterator&& other)
: m_channel(other.m_channel),
m_value(std::move(other.m_value)),
m_valid(other.m_valid) {
other.m_valid = false;
}
bool operator==(const ChannelIterator& other) const {
return this == &other;
}
bool operator!=(const ChannelIterator& other) const {
return !(*this == other);
}
const value_type& operator*() const { return m_value; }
value_type& operator*() { return m_value; }
value_type* operator->() { return &m_value; }
const value_type* operator->() const { return &m_value; }
ChannelIterator& operator++() {
m_channel.recv(*this);
return *this;
}
ChannelIterator operator++(int) = delete;
inline bool valid() const { return m_valid; }
};
using iterator = ChannelIterator;
Channel() {
m_max_size = 1024;
m_closed.store(false);
}
void close() {
std::lock_guard<std::mutex> lock(m_queue_mutex);
m_closed.store(true);
m_queue_not_full.notify_all();
m_queue_not_empty.notify_all();
}
void send(T&& elt) {
std::unique_lock<std::mutex> lock(m_queue_mutex);
m_queue_not_full.wait(lock, [&]() {
return (m_queue.size() < m_max_size) || m_closed;
});
std::lock_guard<std::mutex> guard(*lock.release(), std::adopt_lock);
if (m_closed) {
throw std::bad_function_call();
}
m_queue.push(std::move(elt));
m_queue_not_empty.notify_one();
}
iterator range() {
ChannelIterator it(*this);
return it;
}
private:
void recv(ChannelIterator& it) {
std::unique_lock<std::mutex> lock(m_queue_mutex);
m_queue_not_empty.wait(
lock, [&]() { return (m_queue.size() > 0) || m_closed; });
std::lock_guard<std::mutex> guard(*lock.release(), std::adopt_lock);
if (m_queue.size() > 0) {
it.m_value = std::move(m_queue.front());
m_queue.pop();
m_queue_not_full.notify_one();
} else if (m_closed) {
it.m_valid = false;
} else {
assert(false);
}
}
};
class WaitGroup {
private:
std::atomic_uint_fast64_t m_counter;
std::mutex m_mutex;
std::condition_variable m_cv;
public:
WaitGroup();
WaitGroup(const WaitGroup&) = delete;
WaitGroup(WaitGroup&&) = delete;
void add(size_t delta);
void done();
void wait();
};
} // namespace dadrian
#endif /* DADRIAN_CHANNEL_H */