| 1 | #ifdef _WIN32 |
| 2 | |
| 3 | #include "later.h" |
| 4 | |
| 5 | #include <Rcpp.h> |
| 6 | #include <queue> |
| 7 | #define WIN32_LEAN_AND_MEAN |
| 8 | #include <windows.h> |
| 9 | #include "debug.h" |
| 10 | |
| 11 | using namespace Rcpp; |
| 12 | |
| 13 | // Whether we have initialized the message-only window. |
| 14 | static int initialized = 0; |
| 15 | |
| 16 | // The handle to the message-only window |
| 17 | static HWND hwnd; |
| 18 | |
| 19 | // The ID of the timer |
| 20 | static UINT_PTR TIMER_ID = 1; |
| 21 | |
| 22 | // The window message we use to run SetTimer on the main thread |
| 23 | static const UINT WM_SETUPTIMER = WM_USER + 101; |
| 24 | |
| 25 | static void setupTimer() { |
| 26 | if (!SetTimer(hwnd, TIMER_ID, USER_TIMER_MINIMUM, NULL)) { |
| 27 | (Rf_error)("Failed to schedule callback timer"); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | static bool executeHandlers() { |
| 32 | if (!at_top_level()) { |
| 33 | // It's not safe to run arbitrary callbacks when other R code |
| 34 | // is already running. Wait until we're back at the top level. |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | // This try-catch is meant to be similar to the BEGIN_RCPP and VOID_END_RCPP |
| 39 | // macros. They are needed for two reasons: first, if an exception occurs in |
| 40 | // any of the callbacks, destructors will still execute; and second, if an |
| 41 | // exception (including R-level error) occurs in a callback and it reaches |
| 42 | // the top level in an R input handler, R appears to be unable to handle it |
| 43 | // properly. |
| 44 | // https://github.com/r-lib/later/issues/12 |
| 45 | // https://github.com/RcppCore/Rcpp/issues/753 |
| 46 | // https://github.com/r-lib/later/issues/31 |
| 47 | try { |
| 48 | execCallbacksForTopLevel(); |
| 49 | } |
| 50 | catch(Rcpp::internal::InterruptedException &e) { |
| 51 | REprintf("later: interrupt occurred while executing callback.\n"); |
| 52 | } |
| 53 | catch(Rcpp::LongjumpException& e){ |
| 54 | REprintf("later: exception occurred while executing callback.\n"); |
| 55 | } |
| 56 | catch(std::exception& e){ |
| 57 | std::string msg = "later: exception occurred while executing callback: \n"; |
| 58 | msg += e.what(); |
| 59 | msg += "\n"; |
| 60 | REprintf("%s", msg.c_str()); |
| 61 | } |
| 62 | catch( ... ){ |
| 63 | REprintf("later: c++ exception (unknown reason) occurred while executing callback.\n"); |
| 64 | } |
| 65 | |
| 66 | return idle(GLOBAL_LOOP); |
| 67 | } |
| 68 | |
| 69 | LRESULT CALLBACK callbackWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { |
| 70 | switch (message) { |
| 71 | case WM_TIMER: |
| 72 | if (executeHandlers()) { |
| 73 | KillTimer(hwnd, TIMER_ID); |
| 74 | } |
| 75 | break; |
| 76 | case WM_SETUPTIMER: |
| 77 | setupTimer(); |
| 78 | break; |
| 79 | default: |
| 80 | return DefWindowProc(hWnd, message, wParam, lParam); |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | void ensureAutorunnerInitialized() { |
| 86 | if (!initialized) { |
| 87 | static const char* class_name = "R_LATER_WINDOW_CLASS"; |
| 88 | WNDCLASSEX wc = {}; |
| 89 | wc.cbSize = sizeof(WNDCLASSEX); |
| 90 | wc.lpfnWndProc = callbackWndProc; |
| 91 | wc.hInstance = NULL; |
| 92 | wc.lpszClassName = class_name; |
| 93 | if (!RegisterClassEx(&wc)) { |
| 94 | Rcpp::stop("Failed to register window class"); |
| 95 | } |
| 96 | |
| 97 | hwnd = CreateWindowEx(0, class_name, "dummy_name", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL); |
| 98 | if (!hwnd) { |
| 99 | Rcpp::stop("Failed to create message-only window"); |
| 100 | } |
| 101 | |
| 102 | initialized = 1; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | uint64_t doExecLater(std::shared_ptr<CallbackRegistry> callbackRegistry, Rcpp::Function callback, double delaySecs, bool resetTimer) { |
| 107 | uint64_t callback_id = callbackRegistry->add(callback, delaySecs); |
| 108 | |
| 109 | if (resetTimer) |
| 110 | setupTimer(); |
| 111 | |
| 112 | return callback_id; |
| 113 | } |
| 114 | |
| 115 | uint64_t doExecLater(std::shared_ptr<CallbackRegistry> callbackRegistry, void (*func)(void*), void* data, double delaySecs, bool resetTimer) { |
| 116 | uint64_t callback_id = callbackRegistry->add(func, data, delaySecs); |
| 117 | |
| 118 | if (resetTimer) { |
| 119 | if (GetCurrentThreadId() == GetWindowThreadProcessId(hwnd, NULL)) { |
| 120 | setupTimer(); |
| 121 | } else { |
| 122 | // Not safe to setup the timer from this thread. Instead, send a |
| 123 | // message to the main thread that the timer should be set up. |
| 124 | PostMessage(hwnd, WM_SETUPTIMER, 0, 0); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return callback_id; |
| 129 | } |
| 130 | |
| 131 | #endif // ifdef _WIN32 |