Skip to navigation

hololink::module::Watchdog

View as Markdown

Detects when an expected event stops happening.

A Watchdog is a deadline timer that runs on the ReactorV1. While it is armed, each call to tap() restarts the countdown. If the countdown reaches zero before the next tap, the Watchdog calls its on_timeout function once, on the reactor thread, and then waits until it is armed again. Call tap() each time the event being watched occurs (for example, a frame arrives) so that a gap longer than the timeout is reported as a loss.

Every method may be called from any thread. on_timeout runs on the reactor thread, with no Watchdog lock held, so it may itself call arm(), tap() or cancel(); a typical on_timeout re-arms in order to keep watching.

A Watchdog cannot be copied.

#include <hololink/module/watchdog.hpp>

Constructors

Watchdog

hololink::module::Watchdog::Watchdog(
std::shared_ptr<ReactorV1> reactor,
double timeout_s,
std::function<void()> on_timeout
)

Create a Watchdog.

It is idle until arm() or tap() is called.

Parameters

reactor
std::shared_ptr<ReactorV1>

The reactor that supplies the timer and runs on_timeout. If null, the Watchdog never times out.

timeout_s
double

Seconds without a tap before on_timeout is called. If zero or negative, the Watchdog is disabled and never times out.

on_timeout
std::function<void()>

Function to call when the timeout elapses. It must remain valid for as long as a timeout can still occur, so if it refers to an object that is to be destroyed, call cancel() (or destroy the Watchdog) first.

Destructor

~Watchdog

hololink::module::Watchdog::~Watchdog()

Cancel any pending timeout and destroy the Watchdog.

No timeout starts after this returns. A call to on_timeout that is already running on the reactor thread is not waited for.


Methods

arm

void hololink::module::Watchdog::arm()

Start watching, or restart the countdown from the full timeout.

Replaces any countdown already in progress. Does nothing if the Watchdog has no reactor or is disabled by a timeout of zero or less.

tap

void hololink::module::Watchdog::tap()

Report that the watched event happened.

Restarts the countdown, exactly as arm() does. It also starts watching if the Watchdog was idle, including after a timeout or cancel().

If a timeout was about to be delivered at the same moment, this prevents it, so an event arriving right at the deadline does not cause a spurious timeout.

cancel

void hololink::module::Watchdog::cancel()

Stop watching.

A timeout that has not yet begun is prevented; it is not delivered even if it was about to fire. The Watchdog can be started again with arm() or tap(). A call to on_timeout that is already running on the reactor thread is not waited for.