| 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 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 47 |
|
| 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 |
|
| 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 |
|
| 99 |
class CallbackRegistry { |
| 100 |
private: |
| 101 |
int id; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
typedef std::set<Callback_sp, pointer_less_than<Callback_sp> > cbSet; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
cbSet queue; |
| 113 |
std::atomic<int> fd_waits{}; |
| 114 |
Mutex* mutex; |
| 115 |
ConditionVariable* condvar; |
| 116 |
|
| 117 |
public: |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
CallbackRegistry(int id, Mutex* mutex, ConditionVariable* condvar); |
| 123 |
~CallbackRegistry(); |
| 124 |
|
| 125 |
int getId() const; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
uint64_t add(const Rcpp::Function& func, double secs); |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
uint64_t add(void (*func)(void*), void* data, double secs); |
| 134 |
|
| 135 |
bool cancel(uint64_t id); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
Optional<Timestamp> nextTimestamp(bool recursive = true) const; |
| 140 |
|
| 141 |
|
| 142 |
bool empty() const; |
| 143 |
|
| 144 |
|
| 145 |
bool due(const Timestamp& time = Timestamp(), bool recursive = true) const; |
| 146 |
|
| 147 |
|
| 148 |
Callback_sp pop(const Timestamp& time = Timestamp()); |
| 149 |
|
| 150 |
|
| 151 |
bool wait(double timeoutSecs, bool recursive) const; |
| 152 |
|
| 153 |
|
| 154 |
Rcpp::List list() const; |
| 155 |
|
| 156 |
|
| 157 |
void fd_waits_incr(); |
| 158 |
void fd_waits_decr(); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
std::shared_ptr<CallbackRegistry> parent; |
| 164 |
std::vector<std::shared_ptr<CallbackRegistry> > children; |
| 165 |
}; |
| 166 |
|
| 167 |
#endif |