Skip to content

Commit

Permalink
have cancellation function return invisibly for consistency with later()
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Oct 25, 2024
1 parent 3a46f35 commit 903b305
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/fd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ inline SEXP R_mkClosure(SEXP formals, SEXP body, SEXP env) {

extern CallbackRegistryTable callbackRegistryTable;
extern SEXP later_fdcancel;
extern SEXP later_invisibleSymbol;

#ifdef _WIN32
#define POLL_FUNC WSAPoll
Expand Down Expand Up @@ -205,9 +206,8 @@ Rcpp::RObject execLater_fd(Rcpp::Function callback, Rcpp::IntegerVector readfds,
tct_thrd_detach(thr);

Rcpp::XPtr<std::shared_ptr<std::atomic<bool>>> xptr(new std::shared_ptr<std::atomic<bool>>(flag), true);

SEXP body, func;
PROTECT(body = Rf_lcons(later_fdcancel, Rf_cons(xptr, R_NilValue)));
PROTECT(body = Rf_lang2(later_invisibleSymbol, Rf_lang2(later_fdcancel, xptr)));
func = R_mkClosure(R_NilValue, body, R_BaseEnv);
UNPROTECT(1);

Expand Down
4 changes: 3 additions & 1 deletion src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include <stdint.h> // for uint64_t

// Symbols and preserved objects defined here.
SEXP later_laterSymbol;
SEXP later_fdcancel;
SEXP later_invisibleSymbol;
SEXP later_laterSymbol;

/* FIXME:
Check these declarations against the C/Fortran source code.
Expand Down Expand Up @@ -63,6 +64,7 @@ uint64_t execLaterNative2(void (*func)(void*), void* data, double secs, int loop
int apiVersion(void);

void PreserveObjects(void) {
later_invisibleSymbol = Rf_install("invisible");
later_laterSymbol = Rf_install("later");
R_PreserveObject(later_fdcancel = Rf_lang3(R_TripleColonSymbol, later_laterSymbol, Rf_install("fd_cancel")));
}
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-later-fd.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ test_that("later_fd", {
expect_true(cancel())
Sys.sleep(0.25)
expect_false(cancel())
expect_invisible(cancel())
run_now()
expect_null(result)

Expand Down

0 comments on commit 903b305

Please sign in to comment.