forked from infinit/elle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.hh
97 lines (92 loc) · 2.59 KB
/
signal.hh
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
#pragma once
#include <elle/reactor/Waitable.hh>
namespace elle
{
namespace reactor
{
/// Signal is the most basic Waitable.
///
/// It offers public methods for signal (Waitable::_signal) and signal_one
/// (Waitable::_signal_one).
///
/// elle::With<elle::reactor::Scope>() << [&](elle::reactor::Scope &s)
/// {
/// elle::reactor::Signal s;
/// s.run_background("one",
/// [&]()
/// {
/// std::cout << "1"
/// s.wait();
/// std::cout << "2"
/// s.signal();
/// std::cout << "3"
/// });
/// s.run_background("two",
/// [&]()
/// {
/// std::cout << "A";
/// s.signal();
/// std::cout << "B";
/// s.wait();
/// std::cout << "C";
/// });
/// s.wait();
/// };
/// // Result: 1AB23C
///
/// \endcode
class Signal
: public Waitable
{
public:
using Self = elle::reactor::Signal;
using Super = elle::reactor::Waitable;
/// Construct a signal.
///
/// \param name A descriptive name of the Waitable, for debugging purpose.
Signal(const std::string& name = std::string());
virtual
~Signal();
Signal(Signal&&) = default;
/// Signal Waiters to wake up.
///
/// @see Waitable::_signal.
bool
signal();
/// Signal a Waiter to wake up.
///
/// @see Waitable::_signal_one.
bool
signal_one();
/*----------.
| Printable |
`----------*/
public:
void
print(std::ostream& stream) const override;
};
/// VSignal works just like Signal but can carry a value.
///
/// After being awake by a VSignal, a Thread can get the last value emitted
/// by the Signal. It's necessarily the value emitted that woke you up.
template <typename V>
class VSignal
: public Waitable
{
public:
using Self = elle::reactor::Signal;
using Super = elle::reactor::Waitable;
VSignal(std::string const& name = std::string());
/// Set the value and signal.
bool
emit(V const& val);
/// Set the value and signal_one.
bool
emit_one(V const& val);
private:
/// The last value emitted.
ELLE_ATTRIBUTE_R(V, value);
};
}
}
#include <elle/reactor/signal.hxx>