OpenBSD manual page server

Manual Page Search Parameters

TIMEOUT_SET(9) Kernel Developer's Manual TIMEOUT_SET(9)

timeout_set, timeout_set_flags, timeout_add, timeout_add_sec, timeout_add_msec, timeout_add_nsec, timeout_add_usec, timeout_add_tv, timeout_del, timeout_del_barrier, timeout_barrier, timeout_pending, timeout_initialized, timeout_triggered, TIMEOUT_INITIALIZER, TIMEOUT_INITIALIZER_FLAGSexecute a function after a specified period of time

#include <sys/types.h>
#include <sys/timeout.h>

void
timeout_set(struct timeout *to, void (*fn)(void *), void *arg);

void
timeout_set_flags(struct timeout *to, void (*fn)(void *), void *arg, int flags);

int
timeout_add(struct timeout *to, int ticks);

int
timeout_del(struct timeout *to);

int
timeout_del_barrier(struct timeout *to);

void
timeout_barrier(struct timeout *to);

int
timeout_pending(struct timeout *to);

int
timeout_initialized(struct timeout *to);

int
timeout_triggered(struct timeout *to);

int
timeout_add_tv(struct timeout *to, struct timeval *);

int
timeout_add_sec(struct timeout *to, int sec);

int
timeout_add_msec(struct timeout *to, int msec);

int
timeout_add_usec(struct timeout *to, int usec);

int
timeout_add_nsec(struct timeout *to, int nsec);

TIMEOUT_INITIALIZER(void (*fn)(void *), void *arg);

TIMEOUT_INITIALIZER_FLAGS(void (*fn)(void *), void *arg, int flags);

The timeout API provides a mechanism to execute a function at a given time. The granularity of the time is limited by the granularity of the hardclock(9) timer which executes hz(9) times a second.

It is the responsibility of the caller to provide these functions with pre-allocated timeout structures.

The () function prepares the timeout structure to to be used in future calls to timeout_add() and timeout_del(). The timeout will be prepared to call the function specified by the fn argument with a void * argument given in the arg argument. Once initialized, the to structure can be used repeatedly in timeout_add() and timeout_del() and does not need to be reinitialized unless the function called and/or its argument must change.

The () function is similar to timeout_set() but it additionally accepts the bitwise OR of zero or more of the following flags:

Runs the timeout in a process context instead of the default IPL_SOFTCLOCK interrupt context.

The function () schedules the execution of the to timeout in at least ticks/hz seconds. Negative values of ticks are illegal. If the value is ‘0’ it will, in the current implementation, be treated as ‘1’, but in the future it might cause an immediate timeout. The timeout in the to argument must be already initialized by timeout_set() or timeout_set_flags() and may not be used in calls to timeout_set() or timeout_set_flags() until it has timed out or been removed with timeout_del(). If the timeout in the to argument is already scheduled, the old execution time will be replaced by the new one.

The function () will cancel the timeout in the argument to. If the timeout has already executed or has never been added the call will have no effect.

() is like timeout_del() but it will wait until any current execution of the timeout has completed.

() ensures that any current execution of the timeout in the argument to has completed before returning. If the timeout to has been initialised with timeout_set() it will take the kernel lock.

The () macro can be used to check if a timeout is scheduled to run.

The () macro can be used to check if a timeout has been initialized.

The () macro can be used to check if a timeout is running or has been run. The timeout_add() and timeout_del() functions clear the triggered state for that timeout.

When possible, use the (), (), (), (), and () functions instead of timeout_add(). Those functions add a timeout whilst converting the time specified by the respective types. They also defer the timeout handler for at least one tick if called with a positive value.

A timeout declaration can be initialised with the () macro. The timeout will be prepared to call the function specified by the fn argument with the void * argument given in arg.

The () macro is similar to TIMEOUT_INITIALIZER(), but it accepts additional flags. See the timeout_set_flags() function for details.

timeout_set() and timeout_set_flags() can be called during autoconf, from process context, or from interrupt context.

timeout_add(), timeout_add_sec(), timeout_add_msec(), timeout_add_nsec(), timeout_add_usec(), timeout_add_tv(), timeout_del(), timeout_pending(), timeout_initialized(), timeout_triggered() can be called during autoconf, from process context, or from any interrupt context at or below IPL_CLOCK.

timeout_barrier() and timeout_del_barrier() can be called from process context.

When the timeout runs, the fn argument to timeout_set() or timeout_set_flags() will be called in an interrupt context at IPL_SOFTCLOCK or a process context if the TIMEOUT_PROC flag was given at initialization.

timeout_add(), timeout_add_sec(), timeout_add_msec(), timeout_add_nsec(), timeout_add_usec(), and timeout_add_tv() will return 1 if the timeout to was added to the timeout schedule or 0 if it was already queued.

timeout_del() and timeout_del_barrier() will return 1 if the timeout to was removed from the pending timeout schedule or 0 if it was not currently queued.

These functions are implemented in the file sys/kern/kern_timeout.c.

hz(9), splclock(9), tsleep(9), tvtohz(9)

George Varghese and Anthony Lauck, Hashed and hierarchical timing wheels: efficient data structures for implementing a timer facility, IEEE/ACM, Transactions on Networking, no. 6, vol. 5, pp. 824–834, December 1997, especially Schemes 6 and 7.

August 7, 2020 OpenBSD-6.8