| 1 | #include <later_api.h> |
| 2 | #include <unistd.h> |
| 3 | |
| 4 | // Used in tests/testthat/test-run_now.R |
| 5 | |
| 6 | class TestTask : public later::BackgroundTask { |
| 7 | int _timeoutSecs; |
| 8 | |
| 9 | public: |
| 10 | TestTask(int timeoutSecs) : _timeoutSecs(timeoutSecs) {} |
| 11 | |
| 12 | protected: |
| 13 | // The task to be executed on the background thread. |
| 14 | // Neither the R runtime nor any R data structures may be |
| 15 | // touched from the background thread; any values that need |
| 16 | // to be passed into or out of the Execute method must be |
| 17 | // included as fields on the Task subclass object. |
| 18 | void execute() { |
| 19 | sleep(_timeoutSecs); |
| 20 | } |
| 21 | |
| 22 | // A short task that runs on the main R thread after the |
| 23 | // background task has completed. It's safe to access the |
| 24 | // R runtime and R data structures from here. |
| 25 | void complete() {} |
| 26 | }; |
| 27 | |
| 28 | |
| 29 | // [[Rcpp::depends(later)]] |
| 30 | // [[Rcpp::export]] |
| 31 | void launchBgTask(int secsToSleep) { |
| 32 | (new TestTask(secsToSleep))->begin(); |
| 33 | } |