Home
Base module

Timer

Asynchronous event based timer.

Timer

#include <icy/timer.h>
class Timer

Defined in src/base/include/icy/timer.h:27

Inherits: Runner

Asynchronous event based timer.

List of all members

NameKindOwner
TimeoutvariableDeclared here
TimerfunctionDeclared here
TimerfunctionDeclared here
TimerfunctionDeclared here
~TimerfunctionDeclared here
startfunctionDeclared here
startfunctionDeclared here
stopfunctionDeclared here
restartfunctionDeclared here
againfunctionDeclared here
setTimeoutfunctionDeclared here
setIntervalfunctionDeclared here
activefunctionDeclared here
timeoutfunctionDeclared here
intervalfunctionDeclared here
countfunctionDeclared here
handlefunctionDeclared here
asyncfunctionDeclared here
_handlevariableDeclared here
_timeoutvariableDeclared here
_intervalvariableDeclared here
_countvariableDeclared here
TimerfunctionDeclared here
operator=functionDeclared here
TimerfunctionDeclared here
operator=functionDeclared here
initfunctionDeclared here
RunnerfunctionInherited from Runner
~RunnerfunctionInherited from Runner
startfunctionInherited from Runner
runningfunctionInherited from Runner
cancelfunctionInherited from Runner
cancelledfunctionInherited from Runner
repeatingfunctionInherited from Runner
setRepeatingfunctionInherited from Runner
asyncfunctionInherited from Runner
tidfunctionInherited from Runner
waitForExitfunctionInherited from Runner
_contextvariableInherited from Runner
RunnerfunctionInherited from Runner
operator=functionInherited from Runner
RunnerfunctionInherited from Runner
operator=functionInherited from Runner

Inherited from Runner

KindNameDescription
functionRunner
function~Runner virtual
functionstart virtualStarts the asynchronous context with the given callback. The callback must remain valid for the lifetime of the [Runner](icy-Runner.html#runner).
functionrunning constReturns true if the async context is currently running.
functioncancelSignals the async context to stop at the earliest opportunity.
functioncancelled constReturns true if the context has been cancelled. The implementation is responsible for exiting as soon as possible after cancellation.
functionrepeating constReturns true if the runner is in repeating mode.
functionsetRepeatingEnables or disables repeating mode. When repeating, the target function is invoked repeatedly until [cancel()](icy-Runner.html#cancel-2) is called. This normalizes behaviour across thread-based and event-loop-based [Runner](icy-Runner.html#runner) implementations. Must be called before [start()](icy-Runner.html#start-3).
functionasync virtual constReturns true if the implementation is thread-based.
functiontid constReturns the native thread ID of the thread running the async context.
functionwaitForExitBlocks until the async context exits or the timeout elapses. The context should be cancelled before calling this method. Must be called from outside the runner's own thread to avoid deadlock.
variable_contextShared pointer to the internal Context.
functionRunnerNonCopyable and NonMovable.
functionoperator=Deleted assignment operator.
functionRunnerDeleted constructor.
functionoperator=Deleted assignment operator.

Public Attributes

ReturnNameDescription
NullSignalTimeoutSignal that gets triggered on timeout.

Timeout

NullSignal Timeout

Defined in src/base/include/icy/timer.h:130

Signal that gets triggered on timeout.

Public Methods

ReturnNameDescription
TimerCreate a timer without a timeout or interval; values must be set before [start()](#start-12).
TimerCreate a one-shot timer that fires after timeout milliseconds.
TimerCreate a repeating timer with an initial delay and a repeat period.
~Timer virtualDestructor.
voidstartStart the timer using the previously configured timeout and interval.
voidstart virtual overrideConnect func to the [Timeout](icy-Timeout.html#timeout-1) signal and start the timer.
voidstopStop the timer. Resets the fire count to zero.
voidrestartRestart the timer.
voidagainReset the countdown without stopping and restarting the timer.
voidsetTimeoutSet the initial delay before the first timeout event.
voidsetIntervalSet the repeat interval (calls uv_timer_set_repeat).
boolactive const
std::int64_ttimeout const
std::int64_tinterval const
std::int64_tcount
uv::Handle< uv_timer_t > &handle
boolasync virtual const override

Timer

Timer(uv::Loop * loop = uv::defaultLoop())

Defined in src/base/include/icy/timer.h:33

Create a timer without a timeout or interval; values must be set before [start()](#start-12).

Parameters

  • loop Event loop to associate with. Defaults to the process-wide default loop.

Timer

Timer(std::int64_t timeout, uv::Loop * loop = uv::defaultLoop(), std::function< void()> func = nullptr)

Defined in src/base/include/icy/timer.h:44

Create a one-shot timer that fires after timeout milliseconds.

If func is provided the timer starts immediately; otherwise call [start()](#start-12) manually.

Parameters

  • timeout Delay in milliseconds before the first (and only) fire.

  • loop Event loop to associate with.

  • func Optional callback connected to the [Timeout](icy-Timeout.html#timeout-1) signal and used to start the timer immediately.


Timer

Timer(std::int64_t timeout, std::int64_t interval, uv::Loop * loop = uv::defaultLoop(), std::function< void()> func = nullptr)

Defined in src/base/include/icy/timer.h:57

Create a repeating timer with an initial delay and a repeat period.

Fires once after timeout milliseconds, then repeatedly every interval milliseconds. If func is provided the timer starts immediately; otherwise call [start()](#start-12) manually.

Parameters

  • timeout Initial delay in milliseconds.

  • interval Repeat period in milliseconds.

  • loop Event loop to associate with.

  • func Optional callback connected to the [Timeout](icy-Timeout.html#timeout-1) signal and used to start the timer immediately.


~Timer

virtual

virtual ~Timer()

Defined in src/base/include/icy/timer.h:60

Destructor.


start

void start()

Defined in src/base/include/icy/timer.h:67

Start the timer using the previously configured timeout and interval.

Throws std::logic_error if neither a timeout nor an interval has been set, or if the handle has not been allocated. Has no effect if already active.


start

virtual override

virtual void start(std::function< void()> func) override

Defined in src/base/include/icy/timer.h:72

Connect func to the [Timeout](icy-Timeout.html#timeout-1) signal and start the timer.

Parameters

  • func Callback invoked on each timeout event.
Reimplements

stop

void stop()

Defined in src/base/include/icy/timer.h:77

Stop the timer. Resets the fire count to zero.

Has no effect if the timer is not active.


restart

void restart()

Defined in src/base/include/icy/timer.h:85

Restart the timer.

If the timer is not currently active, behaves like [start()](#start-12). If it is active, calls [again()](#again) to reset the countdown using the repeat value. A timeout or interval must be set beforehand, otherwise [start()](#start-12) will throw std::logic_error.


again

void again()

Defined in src/base/include/icy/timer.h:93

Reset the countdown without stopping and restarting the timer.

For repeating timers this restarts the repeat interval from now. For non-repeating timers this has the same effect as stopping the timer. Throws on error if uv_timer_again fails (e.g. the timer was never started). Resets the fire count to zero.


setTimeout

void setTimeout(std::int64_t timeout)

Defined in src/base/include/icy/timer.h:100

Set the initial delay before the first timeout event.

Has no effect if the timer is currently active.

Parameters

  • timeout Delay in milliseconds.

setInterval

void setInterval(std::int64_t interval)

Defined in src/base/include/icy/timer.h:109

Set the repeat interval (calls uv_timer_set_repeat).

Takes effect from the next timeout event. If set from within a timer callback on a repeating timer, the new value is used from the following iteration; on a non-repeating timer the timer will have already stopped.

Parameters

  • interval Repeat period in milliseconds; 0 disables repeating.

active

const

bool active() const

Defined in src/base/include/icy/timer.h:112

Returns

true if the timer is currently counting down.


timeout

const

std::int64_t timeout() const

Defined in src/base/include/icy/timer.h:115

Returns

The configured initial timeout in milliseconds.


interval

const

std::int64_t interval() const

Defined in src/base/include/icy/timer.h:118

Returns

The effective repeat interval reported by libuv, in milliseconds.


count

std::int64_t count()

Defined in src/base/include/icy/timer.h:121

Returns

The number of times the timer has fired since the last start or again().


handle

uv::Handle< uv_timer_t > & handle()

Defined in src/base/include/icy/timer.h:124

Returns

Reference to the underlying uv_timer_t handle wrapper.


async

virtual const override

virtual bool async() const override

Defined in src/base/include/icy/timer.h:127

Returns

false; the timer is event-loop-driven, not thread-based.

Reimplements

Protected Attributes

ReturnNameDescription
uv::Handle< uv_timer_t >_handle
std::int64_t_timeout
std::int64_t_interval
std::int64_t_count

_handle

uv::Handle< uv_timer_t > _handle

Defined in src/base/include/icy/timer.h:140


_timeout

std::int64_t _timeout

Defined in src/base/include/icy/timer.h:141


_interval

std::int64_t _interval

Defined in src/base/include/icy/timer.h:142


_count

std::int64_t _count

Defined in src/base/include/icy/timer.h:143

Protected Methods

ReturnNameDescription
TimerDeleted constructor.
TimerDeleted constructor.
voidinit

Timer

Timer(const Timer &) = delete

Defined in src/base/include/icy/timer.h:133

Deleted constructor.


Timer

Timer(Timer &&) = delete

Defined in src/base/include/icy/timer.h:135

Deleted constructor.


init

void init()

Defined in src/base/include/icy/timer.h:138