libuev
Functions
timer.c File Reference

Monotonic timers, Linux timerfd(2) More...

#include <errno.h>
#include <sys/timerfd.h>
#include <unistd.h>
#include "uev.h"
Include dependency graph for timer.c:

Functions

int uev_timer_init (uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, int timeout, int period)
 Create and start a timer watcher. More...
 
int uev_timer_set (uev_t *w, int timeout, int period)
 Reset a timer. More...
 
int uev_timer_start (uev_t *w)
 Start a stopped timer watcher. More...
 
int uev_timer_stop (uev_t *w)
 Stop and unregister a timer watcher. More...
 

Detailed Description

Monotonic timers, Linux timerfd(2)

Function Documentation

◆ uev_timer_init()

int uev_timer_init ( uev_ctx_t ctx,
uev_t w,
uev_cb_t cb,
void *  arg,
int  timeout,
int  period 
)

Create and start a timer watcher.

Parameters
ctxA valid libuEv context
wPointer to an uev_t watcher
cbCallback function
argOptional callback argument
timeoutTimeout in milliseconds before cb is called
periodFor periodic timers this is the period time that timeout is reset to

This function creates, and optionally starts, a timer watcher. There are two types of timers: one-shot and periodic.

One-shot timers only use timeout, period is zero.

Periodic timers can either start their life disabled, with timeout set to zero, or with the same value as period.

When the timeout expires, for either of the two types, cb is called, with the optional arg argument. A one-shot timer ends its life there, while a periodic task's timeout is reset to the period and restarted.

A timer is automatically started if the event loop is already running, otherwise it is kept on hold until triggered by calling uev_run().

See also
uev_timer_set
Returns
POSIX OK(0) or non-zero with errno set on error.

◆ uev_timer_set()

int uev_timer_set ( uev_t w,
int  timeout,
int  period 
)

Reset a timer.

Parameters
wWatcher to reset
timeoutTimeout in milliseconds before cb is called, zero disarms timer
periodFor periodic timers this is the period time that timeout is reset to

Note, the timeout value must be non-zero. Setting it to zero disarms the timer. This is the behavior of the underlying Linux function timerfd_settimer(2)

Returns
POSIX OK(0) or non-zero with errno set on error.

◆ uev_timer_start()

int uev_timer_start ( uev_t w)

Start a stopped timer watcher.

Parameters
wWatcher to start (again)
Returns
POSIX OK(0) or non-zero with errno set on error.

◆ uev_timer_stop()

int uev_timer_stop ( uev_t w)

Stop and unregister a timer watcher.

Parameters
wWatcher to stop
Returns
POSIX OK(0) or non-zero with errno set on error.