Skip to content

Commit 0a4125f

Browse files
committed
Adds an API for checking the callback queue length for a loop.
This introduces loop_queue_length(), which is similar to loop_empty() but provides a more granular measure of what remains to be executed. Unlike list_queue(), it is an exported function meant to be used externally. Includes documentation and tests.
1 parent 0fb877a commit 0a4125f

File tree

11 files changed

+82
-0
lines changed

11 files changed

+82
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export(exists_loop)
99
export(global_loop)
1010
export(later)
1111
export(loop_empty)
12+
export(loop_queue_length)
1213
export(next_op_secs)
1314
export(run_now)
1415
export(with_loop)

R/RcppExports.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ idle <- function(loop) {
3737
.Call('_later_idle', PACKAGE = 'later', loop)
3838
}
3939

40+
queueLength <- function(loop) {
41+
.Call('_later_queueLength', PACKAGE = 'later', loop)
42+
}
43+
4044
execLater <- function(callback, delaySecs, loop) {
4145
.Call('_later_execLater', PACKAGE = 'later', callback, delaySecs, loop)
4246
}

R/later.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,17 @@ loop_empty <- function(loop = current_loop()) {
253253
idle(loop$id)
254254
}
255255

256+
#' Check how many callbacks are queued in a later loop
257+
#'
258+
#' Returns the number of callbacks that are scheduled to execute in the present
259+
#' or future.
260+
#'
261+
#' @inheritParams create_loop
262+
#' @export
263+
loop_queue_length <- function(loop = current_loop()) {
264+
queueLength(loop$id)
265+
}
266+
256267
#' Relative time to next scheduled operation
257268
#'
258269
#' Returns the duration between now and the earliest operation that is currently

man/loop_queue_length.Rd

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/RcppExports.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ BEGIN_RCPP
103103
return rcpp_result_gen;
104104
END_RCPP
105105
}
106+
// queueLength
107+
size_t queueLength(int loop);
108+
RcppExport SEXP _later_queueLength(SEXP loopSEXP) {
109+
BEGIN_RCPP
110+
Rcpp::RObject rcpp_result_gen;
111+
Rcpp::RNGScope rcpp_rngScope_gen;
112+
Rcpp::traits::input_parameter< int >::type loop(loopSEXP);
113+
rcpp_result_gen = Rcpp::wrap(queueLength(loop));
114+
return rcpp_result_gen;
115+
END_RCPP
116+
}
106117
// execLater
107118
std::string execLater(Rcpp::Function callback, double delaySecs, int loop);
108119
RcppExport SEXP _later_execLater(SEXP callbackSEXP, SEXP delaySecsSEXP, SEXP loopSEXP) {

src/callback_registry.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@ bool CallbackRegistry::empty() const {
307307
return this->queue.empty();
308308
}
309309

310+
size_t CallbackRegistry::queueLength() const {
311+
Guard guard(mutex);
312+
return this->queue.size();
313+
}
314+
310315
// Returns true if the smallest timestamp exists and is not in the future.
311316
bool CallbackRegistry::due(const Timestamp& time) const {
312317
Guard guard(mutex);

src/callback_registry.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ class CallbackRegistry {
129129
// Is the registry completely empty?
130130
bool empty() const;
131131

132+
// How many callbacks are currently queued?
133+
size_t queueLength() const;
134+
132135
// Is anything ready to execute?
133136
bool due(const Timestamp& time = Timestamp()) const;
134137

src/init.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Check these declarations against the C/Fortran source code.
1212
extern SEXP _later_ensureInitialized();
1313
extern SEXP _later_execCallbacks(SEXP, SEXP, SEXP);
1414
extern SEXP _later_idle(SEXP);
15+
extern SEXP _later_queueLength(SEXP);
1516
extern SEXP _later_execLater(SEXP, SEXP, SEXP);
1617
extern SEXP _later_cancel(SEXP, SEXP);
1718
extern SEXP _later_nextOpSecs(SEXP);
@@ -26,6 +27,7 @@ static const R_CallMethodDef CallEntries[] = {
2627
{"_later_ensureInitialized", (DL_FUNC) &_later_ensureInitialized, 0},
2728
{"_later_execCallbacks", (DL_FUNC) &_later_execCallbacks, 3},
2829
{"_later_idle", (DL_FUNC) &_later_idle, 1},
30+
{"_later_queueLength", (DL_FUNC) &_later_queueLength, 1},
2931
{"_later_execLater", (DL_FUNC) &_later_execLater, 3},
3032
{"_later_cancel", (DL_FUNC) &_later_cancel, 2},
3133
{"_later_nextOpSecs", (DL_FUNC) &_later_nextOpSecs, 1},

src/later.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ bool idle(int loop) {
215215
return getCallbackRegistry(loop)->empty();
216216
}
217217

218+
// [[Rcpp::export]]
219+
size_t queueLength(int loop) {
220+
ASSERT_MAIN_THREAD()
221+
Guard guard(callbackRegistriesMutex);
222+
return getCallbackRegistry(loop)->queueLength();
223+
}
224+
218225
// [[Rcpp::export]]
219226
std::string execLater(Rcpp::Function callback, double delaySecs, int loop) {
220227
ASSERT_MAIN_THREAD()

src/later.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ bool at_top_level();
1919

2020
bool execCallbacks(double timeoutSecs = 0, bool runAll = true, int loop = GLOBAL_LOOP);
2121
bool idle(int loop);
22+
size_t queueLength(int loop);
2223

2324
extern "C" uint64_t execLaterNative(void (*func)(void*), void* data, double secs);
2425
extern "C" uint64_t execLaterNative2(void (*func)(void*), void* data, double secs, int loop);

0 commit comments

Comments
 (0)