hololink::module::ReactorV1
hololink::module::ReactorV1
Reactor provides an event loop that runs deferred, timed and file-descriptor-driven callbacks in a single thread.
Reactor is a singleton object, owned by the host and shared to every module. All callbacks it dispatches run one sequentially in a dedicated thread, so callbacks never run concurrently with each other and need no locking against one another. Callbacks must return promptly, since each one delays all the others, and must not let an exception escape.
Every method may be called from any thread, including from within a callback. Pending callbacks and alarms are discarded when the host shuts down.
Inherits from: hololink::module::Service< ReactorV1 > (public)
Methods
now
The current time on the reactor’s clock.
This is a monotonic clock (not wall-clock time); it is the clock add_alarm() interprets its when argument against.
Returns: The current time.
add_callback
Queues a callback on the reactor thread for execution as soon as possible.
Callbacks queued this way run in the order they were added.
Parameters
The function to run. The reactor keeps it alive until it has run.
add_fd_callback
Call a function whenever a file descriptor has events of interest.
Readiness is level-triggered, as for poll(2): the callback keeps being called for as long as the condition persists, so it should consume or clear it. There can be only one callback per file descriptor; adding another for the same fd replaces the earlier one. Events the reactor saw for an earlier registration of fd are never passed to a later one, even of the same callback. The reactor does not own or close fd.
Parameters
The file descriptor to watch.
The function to call. The reactor keeps it alive until it is removed or replaced and the reactor is done with it: a callback removed while the reactor waits for events, or runs another callback, is released on the reactor thread once that round of callbacks has run.
The poll(2) events to wait for, e.g. POLLIN.
remove_fd_callback
Stop watching a file descriptor.
After this returns no new calls to the file descriptor’s callback will be scheduled. If called from a thread other than the reactor thread, a call that is already running may still be finishing. Does nothing if fd is not being watched. Remove the callback before closing fd: a callback whose fd is closed while it is registered is dropped, with a warning, when the reactor next polls it.
Parameters
The file descriptor passed to add_fd_callback().
add_alarm_s
Run a callback once, after a delay.
Returns: A handle for cancel_alarm().
Parameters
Delay from now, in seconds.
The function to run. The reactor keeps it alive until it has run or the alarm is cancelled.
add_alarm
Run a callback once, at a given time.
Alarms that are due run in order of their when. A when that has already passed runs as soon as possible.
Returns: A handle for cancel_alarm().
Parameters
Time to run at, on the clock reported by now().
The function to run. The reactor keeps it alive until it has run or the alarm is cancelled.
cancel_alarm
Cancel a scheduled alarm.
Does nothing if handle is null, or if the alarm has already run or been cancelled.
Parameters
The handle returned when the alarm was added.
is_current_thread
Determine if the caller is running in the reactor thread.
Returns: true when called in the reactor thread.
module
The Module that this service was published from.
The service holds no strong reference to its Module, so this does not extend the Module’s lifetime.
Returns: The owning Module, or an empty pointer if the service has not been published or its Module no longer exists.
Static methods
get_service
Fetch the process-wide reactor from a module
Overload 2
Fetch the process-wide reactor from a module.
There is only one reactor, so no instance identifier is needed.
Returns: A shared pointer to the reactor.
Parameters
The module to query.
If false (the default), throws std::runtime_error when the module doesn’t provide a reactor (or module is null). If true, returns nullptr instead.
for_each_type_id
Iterate over every type_id supported by this instance.
The callback signature is bool(const char*) — returning false halts the walk; true continues. Iteration is depth-first, so you’ll see the most specific implementation types first.
Types
Typedefs
Inner classes
AlarmEntry
Opaque identifier of a pending alarm.
Obtained from add_alarm() or add_alarm_s(), and passed to cancel_alarm(). Applications neither create nor derive from it.