Debian Codegraph @

src/callback_registry.h

raw symbols SCIP
1 #ifndef _CALLBACK_REGISTRY_H_
2 #define _CALLBACK_REGISTRY_H_
3
4 #include <Rcpp.h>
5 #include <atomic>
6 #include <functional>
7 #include <memory>
8 #include "timestamp.h"
9 #include "optional.h"
10 #include "threadutils.h"
11
12 // Callback is an abstract class with two subclasses. The reason that there
13 // are two subclasses is because one of them is for C++ (std::function)
14 // callbacks, and the other is for R (Rcpp::Function) callbacks. Because
15 // Callbacks can be created from either the main thread or a background
16 // thread, the top-level Callback class cannot contain any Rcpp objects --
17 // otherwise R objects could be allocated on a background thread, which will
18 // cause memory corruption.
19
20 class Callback {
21
22 public:
23 virtual ~Callback() {};
24 Callback(Timestamp when) : when(when) {};
25
26 bool operator<(const Callback& other) const {
27 return this->when < other.when ||
28 (!(this->when > other.when) && this->callbackId < other.callbackId);
29 }
30
31 bool operator>(const Callback& other) const {
32 return other < *this;
33 }
34
35 uint64_t getCallbackId() const {
36 return callbackId;
37 };
38
39 virtual void invoke() const = 0;
40
41 virtual Rcpp::RObject rRepresentation() const = 0;
42
43 Timestamp when;
44
45 protected:
46 // Used to break ties when comparing to a callback that has precisely the same
47 // timestamp
48 uint64_t callbackId;
49 };
50
51
52 class StdFunctionCallback : public Callback {
53 public:
54 StdFunctionCallback(Timestamp when, std::function<void (void)> func);
55
56 void invoke() const {
57 // See https://github.com/r-lib/later/issues/191 and https://github.com/r-lib/later/pull/241
58 Rcpp::unwindProtect([this]() {
59 BEGIN_RCPP
60 func();
61 END_RCPP
62 });
63 }
64
65 Rcpp::RObject rRepresentation() const;
66
67 private:
68 std::function<void (void)> func;
69 };
70
71
72 class RcppFunctionCallback : public Callback {
73 public:
74 RcppFunctionCallback(Timestamp when, const Rcpp::Function& func);
75
76 void invoke() const {
77 func();
78 }
79
80 Rcpp::RObject rRepresentation() const;
81
82 private:
83 Rcpp::Function func;
84 };
85
86
87
88 typedef std::shared_ptr<Callback> Callback_sp;
89
90 template <typename T>
91 struct pointer_less_than {
92 const bool operator()(const T a, const T b) const {
93 return *a < *b;
94 }
95 };
96
97
98 // Stores R function callbacks, ordered by timestamp.
99 class CallbackRegistry {
100 private:
101 int id;
102
103 // Most of the behavior of the registry is like a priority queue. However, a
104 // std::priority_queue only allows access to the top element, and when we
105 // cancel a callback or get an Rcpp::List representation, we need random
106 // access, so we'll use a std::set.
107 typedef std::set<Callback_sp, pointer_less_than<Callback_sp> > cbSet;
108 // This is a priority queue of shared pointers to Callback objects. The
109 // reason it is not a priority_queue<Callback> is because that can cause
110 // objects to be copied on the wrong thread, and even trigger an R GC event
111 // on the wrong thread. https://github.com/r-lib/later/issues/39
112 cbSet queue;
113 std::atomic<int> fd_waits{};
114 Mutex* mutex;
115 ConditionVariable* condvar;
116
117 public:
118 // The CallbackRegistry must be given a Mutex and ConditionVariable when
119 // initialized, because they are shared among the CallbackRegistry objects
120 // and the CallbackRegistryTable; they serve as a global lock. Note that the
121 // lifetime of these objects must be longer than the CallbackRegistry.
122 CallbackRegistry(int id, Mutex* mutex, ConditionVariable* condvar);
123 ~CallbackRegistry();
124
125 int getId() const;
126
127 // Add a function to the registry, to be executed at `secs` seconds in
128 // the future (i.e. relative to the current time).
129 uint64_t add(const Rcpp::Function& func, double secs);
130
131 // Add a C function to the registry, to be executed at `secs` seconds in
132 // the future (i.e. relative to the current time).
133 uint64_t add(void (*func)(void*), void* data, double secs);
134
135 bool cancel(uint64_t id);
136
137 // The smallest timestamp present in the registry, if any.
138 // Use this to determine the next time we need to pump events.
139 Optional<Timestamp> nextTimestamp(bool recursive = true) const;
140
141 // Is the registry completely empty? (including later_fd waits)
142 bool empty() const;
143
144 // Is anything ready to execute?
145 bool due(const Timestamp& time = Timestamp(), bool recursive = true) const;
146
147 // Pop and return a function to execute now.
148 Callback_sp pop(const Timestamp& time = Timestamp());
149
150 // Wait until the next available callback is ready to execute.
151 bool wait(double timeoutSecs, bool recursive) const;
152
153 // Return a List of items in the queue.
154 Rcpp::List list() const;
155
156 // Increment and decrement the number of active later_fd waits
157 void fd_waits_incr();
158 void fd_waits_decr();
159
160 // References to parent and children registries. These are used for
161 // automatically running child loops. They should only be accessed and
162 // modified from the main thread.
163 std::shared_ptr<CallbackRegistry> parent;
164 std::vector<std::shared_ptr<CallbackRegistry> > children;
165 };
166
167 #endif // _CALLBACK_REGISTRY_H_