Repository navigation
Expand file tree
/
Copy pathevent_request.cpp
More file actions
174 lines (159 loc) · 4.68 KB
/
Copy pathevent_request.cpp
File metadata and controls
174 lines (159 loc) · 4.68 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
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
#include "event_request.h"
#include "event_request_dispatch.h"
#include <functional>
#include <mutex>
#include <utility>
namespace nativeapi {
namespace {
struct RequestState {
mutable std::mutex mutex;
bool cancelable = false;
bool cancelled = false;
bool dispatching = true;
bool finished = false;
size_t pending_votes = 0;
std::function<void(bool)> completion;
};
struct Completion {
std::function<void(bool)> callback;
bool accepted = false;
void Run() {
// A continuation is producer code; keep destructor-driven cancellation
// noexcept even if that code unexpectedly throws.
if (callback) {
try {
callback(accepted);
} catch (...) {
}
}
}
};
// Called with state.mutex held. Move the continuation out so its execution and
// captured-object destruction both occur outside the lock.
Completion TakeCompletion(RequestState& state) {
if (state.finished || state.dispatching || (!state.cancelled && state.pending_votes != 0))
return {};
state.finished = true;
return {std::move(state.completion), !state.cancelled};
}
} // namespace
class EventRequest::Impl {
public:
std::shared_ptr<RequestState> state = std::make_shared<RequestState>();
};
class EventDecision::Impl {
public:
std::shared_ptr<RequestState> state;
bool pending = true;
bool Resolve(bool accept) {
Completion completion;
{
std::lock_guard<std::mutex> lock(state->mutex);
if (!pending)
return false;
pending = false;
if (state->finished)
return false;
--state->pending_votes;
if (!accept)
state->cancelled = true;
completion = TakeCompletion(*state);
}
completion.Run();
return true;
}
};
EventDecision::EventDecision() : pimpl_(std::make_unique<Impl>()) {}
EventDecision::~EventDecision() {
if (pimpl_->state)
pimpl_->Resolve(false);
}
bool EventDecision::Accept() {
return pimpl_->Resolve(true);
}
bool EventDecision::Cancel() {
return pimpl_->Resolve(false);
}
bool EventDecision::IsPending() const {
std::lock_guard<std::mutex> lock(pimpl_->state->mutex);
return pimpl_->pending && !pimpl_->state->finished;
}
EventRequest::EventRequest() : pimpl_(std::make_unique<Impl>()) {}
EventRequest::~EventRequest() = default;
bool EventRequest::IsCancelable() const {
std::lock_guard<std::mutex> lock(pimpl_->state->mutex);
return pimpl_->state->cancelable;
}
bool EventRequest::IsCancelled() const {
std::lock_guard<std::mutex> lock(pimpl_->state->mutex);
return pimpl_->state->cancelled;
}
bool EventRequest::IsPending() const {
std::lock_guard<std::mutex> lock(pimpl_->state->mutex);
return !pimpl_->state->finished;
}
bool EventRequest::Cancel() {
Completion completion;
{
std::lock_guard<std::mutex> lock(pimpl_->state->mutex);
auto& state = *pimpl_->state;
if (!state.cancelable || state.cancelled || state.finished)
return false;
state.cancelled = true;
completion = TakeCompletion(state);
}
completion.Run();
return true;
}
std::shared_ptr<EventDecision> EventRequest::Defer() {
Completion completion;
std::shared_ptr<EventDecision> decision;
{
std::lock_guard<std::mutex> lock(pimpl_->state->mutex);
const auto& state = pimpl_->state;
if (!state->cancelable || state->cancelled || state->finished)
return nullptr;
try {
decision = std::shared_ptr<EventDecision>(new EventDecision());
decision->pimpl_->state = state;
++state->pending_votes;
} catch (...) {
// Failure to allocate a confirmation cannot silently approve it.
state->cancelled = true;
completion = TakeCompletion(*state);
}
}
completion.Run();
return decision;
}
std::shared_ptr<EventRequest> detail::EventRequestDispatch::Create(
bool cancelable,
std::function<void(bool)> completion) {
auto request = std::shared_ptr<EventRequest>(new EventRequest());
request->pimpl_->state->cancelable = cancelable;
request->pimpl_->state->completion = std::move(completion);
return request;
}
void detail::EventRequestDispatch::Finish(const std::shared_ptr<EventRequest>& request) {
if (!request)
return;
Completion completion;
{
std::lock_guard<std::mutex> lock(request->pimpl_->state->mutex);
request->pimpl_->state->dispatching = false;
completion = TakeCompletion(*request->pimpl_->state);
}
completion.Run();
}
void detail::EventRequestDispatch::Invalidate(const std::shared_ptr<EventRequest>& request) {
if (!request)
return;
std::function<void(bool)> discarded;
{
std::lock_guard<std::mutex> lock(request->pimpl_->state->mutex);
auto& state = *request->pimpl_->state;
state.finished = true;
discarded = std::move(state.completion);
}
}
} // namespace nativeapi