morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
timeout_scheduler.hpp File Reference
#include <atomic>
#include <chrono>
#include <core/net/EventLoop.hpp>
#include <core/platform/Clock.hpp>
#include <cstdint>
#include <exception>
#include <functional>
#include <memory>
#include <unordered_map>
#include <utility>
#include "../attributes.hpp"
#include "detail/owner_probe.hpp"
#include "io_loop.hpp"
#include "logger.hpp"

Go to the source code of this file.

Detailed Description

TimeoutScheduler — "run this callback once, in N milliseconds, unless cancelled first" — as timers on an exec::IoLoop.

One owner
Every pending callback, and the loop timer that fires it, lives on the loop and is touched only in the loop's own tasks. schedule() and cancel() post to the loop and return; callbacks run on the loop's thread (the main thread under single-threaded WebAssembly, where the browser's timer pumps the loop). A callback therefore runs concurrently with any caller that is not on the loop, and must not block the loop: every socket and every other timer built on the same IoLoop waits while it runs.
What a caller can rely on
  • cancel() stops a callback that has not started. The callback, and everything it captured, is released on the loop, shortly after cancel() returns — not before.
  • cancel() does not wait for a callback that has already started.
  • The destructor drops every pending callback without firing it, and returns only once no callback of this scheduler is running.
  • A callback that throws is logged through morph::log and swallowed.

Definition in file timeout_scheduler.hpp.