11#ifndef _later_later_api_h
22#define _later_later_api_h
33
4+ // ---- Platform prerequisites ------------------------------------------------
5+ // System and R headers required by the later API.
6+
47#ifndef R_NO_REMAP
58#define R_NO_REMAP
69#endif
2831
2932#include < Rinternals.h>
3033
31- // Needed for R_GetCCallable on R 3.3 and older; in more recent versions, this
32- // is included via Rinternals.h.
33- #include < R_ext/Rdynload.h>
34-
34+ // ---- Public API ------------------------------------------------------------
35+ // The later C++ interface. See the "Using later from C++" vignette for usage.
3536
3637namespace later {
3738
@@ -58,6 +59,10 @@ static int apiVersionRuntime() {
5859}
5960// # nocov end
6061
62+ // ---- later() ---------------------------------------------------------------
63+ // Schedule a C function to execute on the main R thread after a delay.
64+ // Safe to call from any thread.
65+
6166inline void later (void (*func)(void *), void* data, double secs, int loop_id) {
6267 // This function works by retrieving the later::execLaterNative2 function
6368 // pointer using R_GetCCallable the first time it's called (per compilation
@@ -99,6 +104,10 @@ inline void later(void (*func)(void*), void* data, double secs) {
99104 later (func, data, secs, GLOBAL_LOOP);
100105}
101106
107+ // ---- later_fd() ------------------------------------------------------------
108+ // Schedule a C function with file descriptor polling. Safe to call from any
109+ // thread. Requires later >= 1.4.1 (API version 3).
110+
102111// # nocov start
103112// tested by cpp-version-mismatch job on CI
104113static void later_fd_version_error (void (*func)(int *, void *), void *data, int num_fds, struct pollfd *fds, double secs, int loop_id) {
@@ -144,6 +153,10 @@ inline void later_fd(void (*func)(int *, void *), void *data, int num_fds, struc
144153}
145154
146155
156+ // ---- BackgroundTask --------------------------------------------------------
157+ // Helper class for running work on a background thread and returning results
158+ // on the main R thread. Subclass and implement execute() and complete().
159+
147160class BackgroundTask {
148161
149162public:
@@ -210,6 +223,10 @@ class BackgroundTask {
210223
211224} // namespace later
212225
226+ // ---- Static initialization -------------------------------------------------
227+ // Ensures later() and later_fd() are initialized on the main R thread before
228+ // any user code can call them from a background thread.
229+
213230namespace {
214231
215232class LaterInitializer {
0 commit comments