| 1 |
#ifndef _CALLBACK_REGISTRY_TABLE_H_ |
| 2 |
#define _CALLBACK_REGISTRY_TABLE_H_ |
| 3 |
|
| 4 |
#include <Rcpp.h> |
| 5 |
#include <memory> |
| 6 |
#include "threadutils.h" |
| 7 |
#include "debug.h" |
| 8 |
#include "callback_registry.h" |
| 9 |
#include "later.h" |
| 10 |
|
| 11 |
using std::shared_ptr; |
| 12 |
using std::make_shared; |
| 13 |
|
| 14 |
class CallbackRegistryTable; |
| 15 |
extern CallbackRegistryTable callbackRegistryTable; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class CallbackRegistryTable { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class RegistryHandle { |
| 34 |
public: |
| 35 |
RegistryHandle(std::shared_ptr<CallbackRegistry> registry, bool r_ref_exists) |
| 36 |
: registry(registry), r_ref_exists(r_ref_exists) { |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
RegistryHandle() = default; |
| 41 |
|
| 42 |
std::shared_ptr<CallbackRegistry> registry; |
| 43 |
bool r_ref_exists; |
| 44 |
}; |
| 45 |
|
| 46 |
public: |
| 47 |
CallbackRegistryTable() : mutex(tct_mtx_plain | tct_mtx_recursive), condvar(mutex) { |
| 48 |
} |
| 49 |
|
| 50 |
bool exists(int id) { |
| 51 |
Guard guard(&mutex); |
| 52 |
return (registries.find(id) != registries.end()); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
void create(int id, int parent_id) { |
| 57 |
ASSERT_MAIN_THREAD() |
| 58 |
Guard guard(&mutex); |
| 59 |
|
| 60 |
if (exists(id)) { |
| 61 |
Rcpp::stop("Can't create event loop %d because it already exists.", id); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
shared_ptr<CallbackRegistry> registry = make_shared<CallbackRegistry>(id, &mutex, &condvar); |
| 72 |
|
| 73 |
if (parent_id != -1) { |
| 74 |
shared_ptr<CallbackRegistry> parent = getRegistry(parent_id); |
| 75 |
if (parent == nullptr) { |
| 76 |
Rcpp::stop("Can't create registry. Parent with id %d does not exist.", parent_id); |
| 77 |
} |
| 78 |
registry->parent = parent; |
| 79 |
parent->children.push_back(registry); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
registries[id] = RegistryHandle(registry, true); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
shared_ptr<CallbackRegistry> getRegistry(int id) { |
| 91 |
Guard guard(&mutex); |
| 92 |
if (!exists(id)) { |
| 93 |
return shared_ptr<CallbackRegistry>(); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
return registries[id].registry; |
| 98 |
} |
| 99 |
|
| 100 |
uint64_t scheduleCallback(void (*func)(void*), void* data, double delaySecs, int loop_id) { |
| 101 |
|
| 102 |
Guard guard(&mutex); |
| 103 |
|
| 104 |
shared_ptr<CallbackRegistry> registry = getRegistry(loop_id); |
| 105 |
if (registry == nullptr) { |
| 106 |
return 0; |
| 107 |
} |
| 108 |
return doExecLater(registry, func, data, delaySecs, true); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
bool notifyRRefDeleted(int id) { |
| 115 |
ASSERT_MAIN_THREAD() |
| 116 |
Guard guard(&mutex); |
| 117 |
|
| 118 |
if (!exists(id)) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
if (registries[id].r_ref_exists) { |
| 123 |
registries[id].r_ref_exists = false; |
| 124 |
this->pruneRegistries(); |
| 125 |
return true; |
| 126 |
} else { |
| 127 |
return false; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
void pruneRegistries() { |
| 139 |
ASSERT_MAIN_THREAD() |
| 140 |
Guard guard(&mutex); |
| 141 |
|
| 142 |
std::map<int, RegistryHandle>::iterator it = registries.begin(); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
while (it != registries.end()) { |
| 155 |
if (!it->second.r_ref_exists && |
| 156 |
(it->second.registry->empty() || it->second.registry->parent == nullptr)) |
| 157 |
{ |
| 158 |
|
| 159 |
|
| 160 |
int id = it->first; |
| 161 |
it++; |
| 162 |
remove(id); |
| 163 |
} else { |
| 164 |
it++; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
bool remove(int id) { |
| 171 |
|
| 172 |
ASSERT_MAIN_THREAD() |
| 173 |
Guard guard(&mutex); |
| 174 |
|
| 175 |
shared_ptr<CallbackRegistry> registry = getRegistry(id); |
| 176 |
if (registry == nullptr) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
shared_ptr<CallbackRegistry> parent = registry->parent; |
| 189 |
if (parent != nullptr) { |
| 190 |
|
| 191 |
for (std::vector<shared_ptr<CallbackRegistry> >::iterator it = parent->children.begin(); |
| 192 |
it != parent->children.end(); |
| 193 |
) |
| 194 |
{ |
| 195 |
if (*it == registry) { |
| 196 |
parent->children.erase(it); |
| 197 |
break; |
| 198 |
} else { |
| 199 |
++it; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
for (std::vector<std::shared_ptr<CallbackRegistry> >::iterator it = registry->children.begin(); |
| 206 |
it != registry->children.end(); |
| 207 |
++it) |
| 208 |
{ |
| 209 |
(*it)->parent.reset(); |
| 210 |
} |
| 211 |
|
| 212 |
registries.erase(id); |
| 213 |
|
| 214 |
return true; |
| 215 |
} |
| 216 |
|
| 217 |
private: |
| 218 |
std::map<int, RegistryHandle> registries; |
| 219 |
Mutex mutex; |
| 220 |
ConditionVariable condvar; |
| 221 |
|
| 222 |
}; |
| 223 |
|
| 224 |
|
| 225 |
#endif |