Skip to content

Commit

Permalink
fix: support non-unique handles for timers (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored Jun 26, 2024
1 parent af6c533 commit d7c6348
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
47 changes: 31 additions & 16 deletions builtins/web/timers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@
#define S_TO_NS(s) ((s) * 1000000000)
#define NS_TO_MS(ns) ((ns) / 1000000)

namespace {

class TimersMap {
public:
std::map<int32_t, api::AsyncTask*> timers_ = {};
int32_t next_timer_id = 1;
void trace(JSTracer *trc) {
for (auto& [id, timer] : timers_) {
timer->trace(trc);
}
}
};

}

static PersistentRooted<js::UniquePtr<TimersMap>> TIMERS_MAP;
static api::Engine *ENGINE;

class TimerTask final : public api::AsyncTask {
using TimerArgumentsVector = std::vector<JS::Heap<JS::Value>>;

static std::map<int32_t, PollableHandle> timer_ids_;
static int32_t next_timer_id;

int32_t timer_id_;
int64_t delay_;
int64_t deadline_;
Expand All @@ -40,8 +53,8 @@ class TimerTask final : public api::AsyncTask {
}

handle_ = host_api::MonotonicClock::subscribe(deadline_, true);
timer_id_ = next_timer_id++;
timer_ids_.emplace(timer_id_, handle_);
timer_id_ = TIMERS_MAP->next_timer_id++;
TIMERS_MAP->timers_.emplace(timer_id_, this);
}

[[nodiscard]] bool run(api::Engine *engine) override {
Expand All @@ -68,17 +81,21 @@ class TimerTask final : public api::AsyncTask {
host_api::MonotonicClock::unsubscribe(handle_);
}

if (repeat_ && timer_ids_.contains(timer_id_)) {
deadline_ = host_api::MonotonicClock::now() + delay_;
handle_ = host_api::MonotonicClock::subscribe(deadline_, true);
engine->queue_async_task(this);
if (TIMERS_MAP->timers_.contains(timer_id_)) {
if (repeat_) {
deadline_ = host_api::MonotonicClock::now() + delay_;
handle_ = host_api::MonotonicClock::subscribe(deadline_, true);
engine->queue_async_task(this);
} else {
TIMERS_MAP->timers_.erase(timer_id_);
}
}

return true;
}

[[nodiscard]] bool cancel(api::Engine *engine) override {
if (!timer_ids_.contains(timer_id_)) {
if (!TIMERS_MAP->timers_.contains(timer_id_)) {
return false;
}

Expand All @@ -103,19 +120,16 @@ class TimerTask final : public api::AsyncTask {
}

static bool clear(int32_t timer_id) {
if (!timer_ids_.contains(timer_id)) {
if (!TIMERS_MAP->timers_.contains(timer_id)) {
return false;
}

ENGINE->cancel_async_task(timer_ids_[timer_id]);
timer_ids_.erase(timer_id);
ENGINE->cancel_async_task(TIMERS_MAP->timers_[timer_id]);
TIMERS_MAP->timers_.erase(timer_id);
return true;
}
};

std::map<int32_t, PollableHandle> TimerTask::timer_ids_ = {};
int32_t TimerTask::next_timer_id = 1;

namespace builtins::web::timers {

/**
Expand Down Expand Up @@ -193,6 +207,7 @@ constexpr JSFunctionSpec methods[] = {

bool install(api::Engine *engine) {
ENGINE = engine;
TIMERS_MAP.init(engine->cx(), js::MakeUnique<TimersMap>());
return JS_DefineFunctions(engine->cx(), engine->global(), methods);
}

Expand Down
2 changes: 1 addition & 1 deletion include/extension-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Engine {

bool has_pending_async_tasks();
void queue_async_task(AsyncTask *task);
bool cancel_async_task(int32_t id);
bool cancel_async_task(AsyncTask *task);

void abort(const char *reason);

Expand Down
4 changes: 2 additions & 2 deletions runtime/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,6 @@ bool api::Engine::debug_logging_enabled() { return ::debug_logging_enabled(); }
bool api::Engine::has_pending_async_tasks() { return core::EventLoop::has_pending_async_tasks(); }

void api::Engine::queue_async_task(AsyncTask *task) { core::EventLoop::queue_async_task(task); }
bool api::Engine::cancel_async_task(int32_t id) {
return core::EventLoop::cancel_async_task(this, id);
bool api::Engine::cancel_async_task(AsyncTask *task) {
return core::EventLoop::cancel_async_task(this, task);
}
7 changes: 3 additions & 4 deletions runtime/event_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ void EventLoop::queue_async_task(api::AsyncTask *task) {
queue.get().tasks.emplace_back(task);
}

bool EventLoop::cancel_async_task(api::Engine *engine, const int32_t id) {
bool EventLoop::cancel_async_task(api::Engine *engine, api::AsyncTask *task) {
const auto tasks = &queue.get().tasks;
for (auto it = tasks->begin(); it != tasks->end(); ++it) {
const auto task = *it;
if (task->id() == id) {
task->cancel(engine);
if (*it == task) {
tasks->erase(it);
task->cancel(engine);
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/event_loop.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class EventLoop {
/**
* Remove a queued async task.
*/
static bool cancel_async_task(api::Engine *engine, int32_t id);
static bool cancel_async_task(api::Engine *engine, api::AsyncTask *task);
};

} // namespace core
Expand Down

0 comments on commit d7c6348

Please sign in to comment.