-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.h
71 lines (55 loc) · 1.26 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
// -*- c++ -*-
#ifndef CHANNEL_H
#define CHANNEL_H
#include <cassert>
#include <mutex>
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>
using boost::interprocess::interprocess_condition;
using boost::interprocess::interprocess_mutex;
template<typename T> class Channel
{
public:
void put(T&& val);
T take();
private:
interprocess_mutex m;
interprocess_condition cond_put_ready;
interprocess_condition cond_take_ready;
bool put_ready;
bool take_ready;
T xfer_val;
void reset();
};
template<typename T>
void Channel<T>::put(T&& val)
{
std::unique_lock<interprocess_mutex> l(m);
assert(! put_ready);
put_ready = true;
xfer_val = std::move(val);
while (! take_ready) {
cond_take_ready.wait(l);
}
cond_put_ready.notify_one();
reset();
}
template<typename T>
T Channel<T>::take()
{
std::unique_lock<interprocess_mutex> l(m);
assert(! take_ready);
take_ready = true;
while (! put_ready) {
cond_put_ready.wait(l);
}
cond_take_ready.notify_one();
return std::move(xfer_val);
}
template<typename T>
void Channel<T>::reset()
{
put_ready = false;
take_ready = false;
}
#endif /* CHANNEL_H */