Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the ability to check the callback queue length for a loop #124

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(exists_loop)
export(global_loop)
export(later)
export(loop_empty)
export(loop_queue_length)
export(next_op_secs)
export(run_now)
export(with_loop)
Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ ensureInitialized <- function() {
invisible(.Call('_later_ensureInitialized', PACKAGE = 'later'))
}

queueLength <- function(loop_id) {
.Call('_later_queueLength', PACKAGE = 'later', loop_id)
}

execLater <- function(callback, delaySecs, loop_id) {
.Call('_later_execLater', PACKAGE = 'later', callback, delaySecs, loop_id)
}
Expand Down
11 changes: 11 additions & 0 deletions R/later.R
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,17 @@ loop_empty <- function(loop = current_loop()) {
idle(loop$id)
}

#' Check how many callbacks are queued in a later loop
#'
#' Returns the number of callbacks that are scheduled to execute in the present
#' or future.
#'
#' @inheritParams create_loop
#' @export
loop_queue_length <- function(loop = current_loop()) {
queueLength(loop$id)
}

#' Relative time to next scheduled operation
#'
#' Returns the duration between now and the earliest operation that is currently
Expand Down
15 changes: 15 additions & 0 deletions man/loop_queue_length.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ BEGIN_RCPP
return R_NilValue;
END_RCPP
}
// queueLength
size_t queueLength(int loop_id);
RcppExport SEXP _later_queueLength(SEXP loop_idSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< int >::type loop_id(loop_idSEXP);
rcpp_result_gen = Rcpp::wrap(queueLength(loop_id));
return rcpp_result_gen;
END_RCPP
}
// execLater
std::string execLater(Rcpp::Function callback, double delaySecs, int loop_id);
RcppExport SEXP _later_execLater(SEXP callbackSEXP, SEXP delaySecsSEXP, SEXP loop_idSEXP) {
Expand Down
5 changes: 5 additions & 0 deletions src/callback_registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@ bool CallbackRegistry::empty() const {
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()
Expand Down
3 changes: 3 additions & 0 deletions src/callback_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class CallbackRegistry {
// Is the registry completely empty?
bool empty() const;

// How many callbacks are currently queued?
size_t queueLength() const;

// Is anything ready to execute?
bool due(const Timestamp& time = Timestamp(), bool recursive = true) const;

Expand Down
2 changes: 2 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Check these declarations against the C/Fortran source code.
extern SEXP _later_ensureInitialized();
extern SEXP _later_execCallbacks(SEXP, SEXP, SEXP);
extern SEXP _later_idle(SEXP);
extern SEXP _later_queueLength(SEXP);
extern SEXP _later_execLater(SEXP, SEXP, SEXP);
extern SEXP _later_cancel(SEXP, SEXP);
extern SEXP _later_nextOpSecs(SEXP);
Expand All @@ -29,6 +30,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_later_ensureInitialized", (DL_FUNC) &_later_ensureInitialized, 0},
{"_later_execCallbacks", (DL_FUNC) &_later_execCallbacks, 3},
{"_later_idle", (DL_FUNC) &_later_idle, 1},
{"_later_queueLength", (DL_FUNC) &_later_queueLength, 1},
{"_later_execLater", (DL_FUNC) &_later_execLater, 3},
{"_later_cancel", (DL_FUNC) &_later_cancel, 2},
{"_later_nextOpSecs", (DL_FUNC) &_later_nextOpSecs, 1},
Expand Down
10 changes: 10 additions & 0 deletions src/later.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ void ensureInitialized() {
initialized = true;
}

// [[Rcpp::export]]
size_t queueLength(int loop_id) {
ASSERT_MAIN_THREAD()
shared_ptr<CallbackRegistry> registry = callbackRegistryTable.getRegistry(loop_id);
if (registry == nullptr) {
Rf_error("CallbackRegistry does not exist.");
}
return registry->queueLength();
}

// [[Rcpp::export]]
std::string execLater(Rcpp::Function callback, double delaySecs, int loop_id) {
ASSERT_MAIN_THREAD()
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-private-loops.R
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,25 @@ test_that("list_queue", {
q <- list_queue(l)
expect_equal(length(q), 0)
})


describe("Queue length with nested loops", {
x <- 0
later(~{x <<- 1}, 0)
expect_equal(loop_queue_length(), 1)
with_temp_loop({
expect_equal(loop_queue_length(), 0)

later(~{x <<- 2})
expect_equal(loop_queue_length(), 1)

run_now()
expect_identical(x, 2)
expect_equal(loop_queue_length(), 0)
expect_equal(loop_queue_length(loop = global_loop()), 1)

run_now(loop = global_loop())
expect_equal(loop_queue_length(loop = global_loop()), 0)
expect_identical(x, 1)
})
})