-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcallback_registry.cpp
More file actions
432 lines (373 loc) · 13.6 KB
/
Copy pathcallback_registry.cpp
File metadata and controls
432 lines (373 loc) · 13.6 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <vector>
#include "callback_registry.h"
#include "debug.h"
#if __cplusplus >= 201103L
#include <atomic>
std::atomic<uint64_t> nextCallbackId(1);
#else
// Fall back to boost::atomic if std::atomic isn't available. We want to
// avoid boost::atomic when possible because on ARM, it requires the
// -lboost_atomic linker flag. (https://github.com/r-lib/later/issues/73)
#include <boost/atomic.hpp>
boost::atomic<uint64_t> nextCallbackId(1);
#endif
// ============================================================================
// Invoke functions
// ============================================================================
enum InvokeResult {
INVOKE_IN_PROGRESS,
INVOKE_INTERRUPTED,
INVOKE_ERROR,
INVOKE_CPP_ERROR,
INVOKE_COMPLETED
};
// This is set by invoke_c(). I
InvokeResult last_invoke_result;
std::string last_invoke_message;
// A wrapper for calling R_CheckUserInterrupt via R_ToplevelExec.
void checkInterruptFn(void*) {
R_CheckUserInterrupt();
}
// The purpose of this function is to provide a plain C function to be called
// by R_ToplevelExec. Because it's called as a C function, it must not throw
// exceptions. Because this function returns void, the way for it to report
// the result to its caller is by setting last_invoke_result.
//
// This code needs to be able to handle interrupts, R errors, and C++
// exceptions. There are many ways these things can happen.
//
// * If the Callback object is a RcppFunctionCallback, then in the case of an
// interrupt or an R error, it will throw a C++ exception. These exceptions
// are the ones defined by Rcpp, and they will be caught by the try-catch in
// this function.
// * It could be a BoostFunctionCallback with C or C++ code.
// * If the function invokes an Rcpp::Function and an interrupt or R error
// happens within the Rcpp::Function, it will throw exceptions just like
// the RcppFunctionCallback case, and they will be caught.
// * If some other C++ exception occurs, it will be caught.
// * If an interrupt (Ctrl-C, or Esc in RStudio) is received (outside of an
// Rcpp::Function), this function will continue through to the end (and
// set the state to INVOKE_COMPLETED). Later, when the invoke_wrapper()
// function (which called this one) checks to see if the interrupt
// happened, it will set the state to INVOKE_INTERRUPTED. (Note that it is
// potentially possible for an interrupt and an exception to occur, in
// which case we set the state to INVOKE_ERROR.)
// * If the function calls R code with Rf_eval(), an interrupt or R error
// could occur. If it's an interrupt, then it will be detect as in the
// previous case. If an error occurs, then that error will be detected by
// the invoke_wrapper() function (which called this one) and the state
// will be set to INVOKE_ERROR.
//
// Note that the last case has one potentially problematic issue. If an error
// occurs in R code, then it will longjmp out of of this function, back to its
// caller, invoke_wrapped(). This will longjmp out of a try statement, which
// is generally not a good idea. We don't know ahead of time whether the
// Callback may longjmp or throw an exception -- some Callbacks could
// potentially do both.
//
// The alternative is to move the try-catch out of this function and into
// invoke_wrapped(), surrounding the `R_ToplevelExec(invoke_c, ...)`. However,
// if we do this, then exceptions would pass through the R_ToplevelExec, which
// is dangerous because it is plain C code. The current way of doing it is
// imperfect, but less dangerous.
//
// There does not seem to be a 100% safe way to call functions which could
// either longjmp or throw exceptions. If we do figure out a way to do that,
// it should be used here.
extern "C" void invoke_c(void* callback_p) {
ASSERT_MAIN_THREAD()
last_invoke_result = INVOKE_IN_PROGRESS;
last_invoke_message = "";
Callback* cb_p = (Callback*)callback_p;
try {
cb_p->invoke();
}
catch(Rcpp::internal::InterruptedException &e) {
// Reaches here if the callback is in Rcpp code and an interrupt occurs.
DEBUG_LOG("invoke_c: caught Rcpp::internal::InterruptedException", LOG_INFO);
last_invoke_result = INVOKE_INTERRUPTED;
return;
}
catch(Rcpp::eval_error &e) {
// Reaches here if an R-level error happens in an Rcpp::Function.
DEBUG_LOG("invoke_c: caught Rcpp::eval_error", LOG_INFO);
last_invoke_result = INVOKE_ERROR;
last_invoke_message = e.what();
return;
}
catch(Rcpp::exception& e) {
// Reaches here if an R-level error happens in an Rcpp::Function.
DEBUG_LOG("invoke_c: caught Rcpp::exception", LOG_INFO);
last_invoke_result = INVOKE_ERROR;
last_invoke_message = e.what();
return;
}
catch(std::exception& e) {
// Reaches here if some other (non-Rcpp) C++ exception is thrown.
DEBUG_LOG(std::string("invoke_c: caught std::exception: ") + typeid(e).name(),
LOG_INFO);
last_invoke_result = INVOKE_CPP_ERROR;
last_invoke_message = e.what();
return;
}
catch( ... ) {
// Reaches here if a non-exception C++ object is thrown.
DEBUG_LOG(std::string("invoke_c: caught unknown object: ") + typeid(std::current_exception()).name(),
LOG_INFO);
last_invoke_result = INVOKE_CPP_ERROR;
return;
}
// Reaches here if no exceptions are thrown. It's possible to get here if an
// interrupt was received outside of Rcpp code, or if an R error happened
// using Rf_eval().
DEBUG_LOG("invoke_c: COMPLETED", LOG_DEBUG);
last_invoke_result = INVOKE_COMPLETED;
}
// Wrapper method for invoking a callback. The Callback object has an invoke()
// method, but instead of invoking it directly, this method should be used
// instead. The purpose of this method is to call invoke(), but wrap it in a
// R_ToplevelExec, so that any LONGJMPs (due to errors in R functions) won't
// cross that barrier in the call stack. If interrupts, exceptions, or
// LONGJMPs do occur, this function throws a C++ exception.
void Callback::invoke_wrapped() const {
ASSERT_MAIN_THREAD()
Rboolean result = R_ToplevelExec(invoke_c, (void*)this);
if (!result) {
DEBUG_LOG("invoke_wrapped: R_ToplevelExec return is FALSE; error or interrupt occurred in R code", LOG_INFO);
last_invoke_result = INVOKE_ERROR;
}
if (R_ToplevelExec(checkInterruptFn, NULL) == FALSE) {
// Reaches here if the callback is C/C++ code and an interrupt occurs.
DEBUG_LOG("invoke_wrapped: interrupt (outside of R code) detected by R_CheckUserInterrupt", LOG_INFO);
last_invoke_result = INVOKE_INTERRUPTED;
}
switch (last_invoke_result) {
case INVOKE_INTERRUPTED:
DEBUG_LOG("invoke_wrapped: throwing Rcpp::internal::InterruptedException", LOG_INFO);
throw Rcpp::internal::InterruptedException();
case INVOKE_ERROR:
DEBUG_LOG("invoke_wrapped: throwing Rcpp::exception", LOG_INFO);
throw Rcpp::exception(last_invoke_message.c_str());
case INVOKE_CPP_ERROR:
throw std::runtime_error("invoke_wrapped: throwing std::runtime_error");
default:
return;
}
}
// ============================================================================
// BoostFunctionCallback
// ============================================================================
BoostFunctionCallback::BoostFunctionCallback(Timestamp when, boost::function<void(void)> func) :
Callback(when),
func(func)
{
this->callbackId = nextCallbackId++;
}
Rcpp::RObject BoostFunctionCallback::rRepresentation() const {
using namespace Rcpp;
ASSERT_MAIN_THREAD()
return List::create(
_["id"] = callbackId,
_["when"] = when.diff_secs(Timestamp()),
_["callback"] = Rcpp::CharacterVector::create("C/C++ function")
);
}
// ============================================================================
// RcppFunctionCallback
// ============================================================================
RcppFunctionCallback::RcppFunctionCallback(Timestamp when, Rcpp::Function func) :
Callback(when),
func(func)
{
ASSERT_MAIN_THREAD()
this->callbackId = nextCallbackId++;
}
Rcpp::RObject RcppFunctionCallback::rRepresentation() const {
using namespace Rcpp;
ASSERT_MAIN_THREAD()
return List::create(
_["id"] = callbackId,
_["when"] = when.diff_secs(Timestamp()),
_["callback"] = func
);
}
// ============================================================================
// CallbackRegistry
// ============================================================================
// [[Rcpp::export]]
void testCallbackOrdering() {
std::vector<BoostFunctionCallback> callbacks;
Timestamp ts;
boost::function<void(void)> func;
for (size_t i = 0; i < 100; i++) {
callbacks.push_back(BoostFunctionCallback(ts, func));
}
for (size_t i = 1; i < 100; i++) {
if (callbacks[i] < callbacks[i-1]) {
::Rf_error("Callback ordering is broken [1]");
}
if (!(callbacks[i] > callbacks[i-1])) {
::Rf_error("Callback ordering is broken [2]");
}
if (callbacks[i-1] > callbacks[i]) {
::Rf_error("Callback ordering is broken [3]");
}
if (!(callbacks[i-1] < callbacks[i])) {
::Rf_error("Callback ordering is broken [4]");
}
}
for (size_t i = 100; i > 1; i--) {
if (callbacks[i-1] < callbacks[i-2]) {
::Rf_error("Callback ordering is broken [2]");
}
}
}
CallbackRegistry::CallbackRegistry(int id, Mutex* mutex, ConditionVariable* condvar)
: id(id), mutex(mutex), condvar(condvar)
{
ASSERT_MAIN_THREAD()
}
CallbackRegistry::~CallbackRegistry() {
ASSERT_MAIN_THREAD()
}
int CallbackRegistry::getId() const {
return id;
}
uint64_t CallbackRegistry::add(Rcpp::Function func, double secs) {
// Copies of the Rcpp::Function should only be made on the main thread.
ASSERT_MAIN_THREAD()
Timestamp when(secs);
Callback_sp cb = boost::make_shared<RcppFunctionCallback>(when, func);
Guard guard(mutex);
queue.insert(cb);
condvar->signal();
return cb->getCallbackId();
}
uint64_t CallbackRegistry::add(void (*func)(void*), void* data, double secs) {
Timestamp when(secs);
Callback_sp cb = boost::make_shared<BoostFunctionCallback>(when, boost::bind(func, data));
Guard guard(mutex);
queue.insert(cb);
condvar->signal();
return cb->getCallbackId();
}
bool CallbackRegistry::cancel(uint64_t id) {
Guard guard(mutex);
cbSet::const_iterator it;
for (it = queue.begin(); it != queue.end(); ++it) {
if ((*it)->getCallbackId() == id) {
queue.erase(it);
return true;
}
}
return false;
}
// The smallest timestamp present in the registry, if any.
// Use this to determine the next time we need to pump events.
Optional<Timestamp> CallbackRegistry::nextTimestamp(bool recursive) const {
Guard guard(mutex);
Optional<Timestamp> minTimestamp;
if (! this->queue.empty()) {
cbSet::const_iterator it = queue.begin();
minTimestamp = Optional<Timestamp>((*it)->when);
}
// Now check children
if (recursive) {
for (std::vector<boost::shared_ptr<CallbackRegistry> >::const_iterator it = children.begin();
it != children.end();
++it)
{
Optional<Timestamp> childNextTimestamp = (*it)->nextTimestamp(recursive);
if (childNextTimestamp.has_value()) {
if (minTimestamp.has_value()) {
if (*childNextTimestamp < *minTimestamp) {
minTimestamp = childNextTimestamp;
}
} else {
minTimestamp = childNextTimestamp;
}
}
}
}
return minTimestamp;
}
bool CallbackRegistry::empty() const {
Guard guard(mutex);
return this->queue.empty();
}
size_t CallbackRegistry::queueLength() const {
Guard guard(mutex);
return this->queue.size();
}
// Returns true if the smallest timestamp exists and is not in the future.
bool CallbackRegistry::due(const Timestamp& time, bool recursive) const {
ASSERT_MAIN_THREAD()
Guard guard(mutex);
cbSet::const_iterator it = queue.begin();
if (!this->queue.empty() && !((*it)->when > time)) {
return true;
}
// Now check children
if (recursive) {
for (std::vector<boost::shared_ptr<CallbackRegistry> >::const_iterator it = children.begin();
it != children.end();
++it)
{
if ((*it)->due(time, true)) {
return true;
}
}
}
return false;
}
std::vector<Callback_sp> CallbackRegistry::take(size_t max, const Timestamp& time) {
ASSERT_MAIN_THREAD()
Guard guard(mutex);
std::vector<Callback_sp> results;
while (this->due(time, false) && (max <= 0 || results.size() < max)) {
cbSet::iterator it = queue.begin();
results.push_back(*it);
this->queue.erase(it);
}
return results;
}
bool CallbackRegistry::wait(double timeoutSecs, bool recursive) const {
ASSERT_MAIN_THREAD()
if (timeoutSecs < 0) {
// "1000 years ought to be enough for anybody" --Bill Gates
timeoutSecs = 3e10;
}
Timestamp expireTime(timeoutSecs);
Guard guard(mutex);
while (true) {
Timestamp end = expireTime;
Optional<Timestamp> next = nextTimestamp(recursive);
if (next.has_value() && *next < expireTime) {
end = *next;
}
double waitFor = end.diff_secs(Timestamp());
if (waitFor <= 0)
break;
// Don't wait for more than 2 seconds at a time, in order to keep us
// at least somewhat responsive to user interrupts
if (waitFor > 2) {
waitFor = 2;
}
condvar->timedwait(waitFor);
Rcpp::checkUserInterrupt();
}
return due();
}
Rcpp::List CallbackRegistry::list() const {
ASSERT_MAIN_THREAD()
Guard guard(mutex);
Rcpp::List results;
cbSet::const_iterator it;
for (it = queue.begin(); it != queue.end(); it++) {
results.push_back((*it)->rRepresentation());
}
return results;
}