NAME |SYNOPSIS |DESCRIPTION |RETURN VALUE |NOTES |HISTORY |SEE ALSO |COLOPHON | |
SD_EVENT_ADD_TIME(3) sd_event_add_timeSD_EVENT_ADD_TIME(3)sd_event_add_time, sd_event_add_time_relative, sd_event_source_get_time, sd_event_source_set_time, sd_event_source_set_time_relative, sd_event_source_get_time_accuracy, sd_event_source_set_time_accuracy, sd_event_source_get_time_clock, sd_event_time_handler_t - Add a timer event source to an event loop
#include <systemd/sd-event.h>typedef struct sd_event_source sd_event_source;typedef int (*sd_event_time_handler_t)(sd_event_source *s,uint64_tusec,void *userdata);int sd_event_add_time(sd_event *event, sd_event_source **source,clockid_tclock, uint64_tusec,uint64_taccuracy,sd_event_time_handler_thandler,void *userdata);int sd_event_add_time_relative(sd_event *event,sd_event_source **source,clockid_tclock, uint64_tusec,uint64_taccuracy,sd_event_time_handler_thandler,void *userdata);int sd_event_source_get_time(sd_event_source *source,uint64_t *ret);int sd_event_source_set_time(sd_event_source *source,uint64_tusec);int sd_event_source_set_time_relative(sd_event_source *source,uint64_tusec);int sd_event_source_get_time_accuracy(sd_event_source *source,uint64_t *ret);int sd_event_source_set_time_accuracy(sd_event_source *source,uint64_tusec);int sd_event_source_get_time_clock(sd_event_source *source,clockid_t *ret);
sd_event_add_time()adds a new timer event source to an event loop. The event loop object is specified in theevent parameter, the event source object is returned in thesource parameter. Theclock parameter takes a clock identifier, one ofCLOCK_REALTIME,CLOCK_MONOTONIC,CLOCK_BOOTTIME,CLOCK_REALTIME_ALARM, orCLOCK_BOOTTIME_ALARM. Seetimerfd_create(2) for details regarding the various types of clocks. Theusec parameter specifies the earliest time, in microseconds (μs), relative to the clock's epoch, when the timer shall be triggered. If a time already in the past is specified (including0), this timer source "fires" immediately and is ready to be dispatched. If the parameter is specified asUINT64_MAXthe timer event will never elapse, which may be used as an alternative to explicitly disabling a timer event source withsd_event_source_set_enabled(3). Theaccuracy parameter specifies an additional accuracy value in μs specifying how much the timer event may be delayed. Use0to select the default accuracy (250ms). Use 1μs for maximum accuracy. Consider specifying 60000000μs (1min) or larger for long-running events that may be delayed substantially. Picking higher accuracy values allows the system to coalesce timer events more aggressively, improving power efficiency. Thehandler is a function to call when the timer elapses orNULL. Theuserdata pointer will be passed to the handler function, and may be chosen freely by the caller. The configured trigger time is also passed to the handler, even if the call actually happens slightly later, subject to the specified accuracy value, the kernel timer slack (seeprctl(2)), and additional scheduling latencies. To query the actual time the handler was called usesd_event_now(3). The handler may return negative to signal an error (see below), other return values are ignored. Ifhandler isNULL, a default handler that callssd_event_exit(3) will be used. By default, the timer will elapse once (SD_EVENT_ONESHOT), but this may be changed withsd_event_source_set_enabled(3). If the handler function returns a negative error code, it will either be disabled after the invocation, even if theSD_EVENT_ONmode was requested before, or it will cause the loop to terminate, seesd_event_source_set_exit_on_failure(3). Note that a timer event set toSD_EVENT_ONwill fire continuously unless its configured time is updated usingsd_event_source_set_time().sd_event_add_time_relative()is likesd_event_add_time(), but takes a relative time specification. It's relative to the current time of the event loop iteration, as returned bysd_event_now(3). To destroy an event source object usesd_event_source_unref(3), but note that the event source is only removed from the event loop when all references to the event source are dropped. To make sure an event source does not fire anymore, even if it is still referenced, disable the event source usingsd_event_source_set_enabled(3) withSD_EVENT_OFF. If the second parameter ofsd_event_add_time()isNULLno reference to the event source object is returned. In this case, the event source is considered "floating", and will be destroyed implicitly when the event loop itself is destroyed. If thehandler parameter tosd_event_add_time()isNULL, and the event source fires, this will be considered a request to exit the event loop. In this case, theuserdata parameter, cast to an integer, is passed as the exit code parameter tosd_event_exit(3). UseCLOCK_BOOTTIME_ALARMandCLOCK_REALTIME_ALARMto define event sources that may wake up the system from suspend. In order to set up relative timers (that is, relative to the current time), retrieve the current time viasd_event_now(3), add the desired timespan to it, and use the result as theusec parameter tosd_event_add_time(). In order to set up repetitive timers (that is, timers that are triggered in regular intervals), set up the timer normally, for the first invocation. Each time the event handler is invoked, update the timer's trigger time withsd_event_source_set_time(3) for the next timer iteration, and reenable the timer usingsd_event_source_set_enabled(). To calculate the next point in time to pass tosd_event_source_set_time(), either use as base theusec parameter passed to the timer callback, or the timestamp returned bysd_event_now(). In the former case timer events will be regular, while in the latter case, the scheduling latency will keep accumulating on the timer.sd_event_source_get_time()retrieves the configured time value of an event source created previously withsd_event_add_time()orsd_event_add_time_relative(). It takes the event source object and a pointer to a variable to store the time in, relative to the selected clock's epoch, in μs. The returned value is relative to the epoch, even if the event source was created with a relative time viasd_event_add_time_relative().sd_event_source_set_time()changes the time of an event source created previously withsd_event_add_time()orsd_event_add_time_relative(). It takes the event source object and a time relative to the selected clock's epoch, in μs.sd_event_source_set_time_relative()is similar tosd_event_source_set_time(), but takes a time relative to the current time of the event loop iteration, as returned bysd_event_now().sd_event_source_get_time_accuracy()retrieves the configured accuracy value of an event source created previously withsd_event_add_time(). It takes the event source object and a pointer to a variable to store the accuracy in. The accuracy is specified in μs.sd_event_source_set_time_accuracy()changes the configured accuracy of a timer event source created previously withsd_event_add_time(). It takes the event source object and accuracy, in μs.sd_event_source_get_time_clock()retrieves the configured clock of an event source created previously withsd_event_add_time(). It takes the event source object and a pointer to a variable to store the clock identifier in.
On success, these functions return 0 or a positive integer. On failure, they return a negative errno-style error code.Errors Returned values may indicate the following problems:-ENOMEM Not enough memory to allocate an object.-EINVAL An invalid argument has been passed.-ESTALE The event loop is already terminated.-ECHILD The event loop has been created in a different process, library or module instance.-EOPNOTSUPP The selected clock is not supported by the event loop implementation.-EDOM The passed event source is not a timer event source.-EOVERFLOW The passed relative time is outside of the allowed range for time values (i.e. the specified value added to the current time is outside the 64 bit unsigned integer range). Added in version 247.
Functions described here are available as a shared library, which can be compiled against and linked to with thelibsystemd pkg-config(1) file. The code described here usesgetenv(3), which is declared to be not multi-thread-safe. This means that the code calling the functions described here must not callsetenv(3) from a parallel thread. It is recommended to only do calls tosetenv()from an early phase of the program when no other threads have been started.
sd_event_add_time(),sd_event_source_get_time(),sd_event_source_set_time(),sd_event_source_get_time_accuracy(),sd_event_source_set_time_accuracy(), andsd_event_source_get_time_clock()were added in version 213.sd_event_time_handler_t()was added in version 217.sd_event_add_time_relative()andsd_event_source_set_time_relative()were added in version 247.
systemd(1),sd-event(3),sd_event_new(3),sd_event_now(3),sd_event_add_io(3),sd_event_add_signal(3),sd_event_add_child(3),sd_event_add_inotify(3),sd_event_add_defer(3),sd_event_source_set_enabled(3),sd_event_source_set_priority(3),sd_event_source_set_userdata(3),sd_event_source_set_description(3),sd_event_source_set_floating(3),clock_gettime(2),timerfd_create(2),prctl(2)
This page is part of thesystemd (systemd system and service manager) project. Information about the project can be found at ⟨http://www.freedesktop.org/wiki/Software/systemd⟩. If you have a bug report for this manual page, see ⟨http://www.freedesktop.org/wiki/Software/systemd/#bugreports⟩. This page was obtained from the project's upstream Git repository ⟨https://github.com/systemd/systemd.git⟩ on 2025-08-11. (At that time, the date of the most recent commit that was found in the repository was 2025-08-11.) If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up-to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgsystemd 258~rc2SD_EVENT_ADD_TIME(3)Pages that refer to this page:sd-event(3), sd_event_add_child(3), sd_event_add_defer(3), sd_event_add_inotify(3), sd_event_add_io(3), sd_event_add_memory_pressure(3), sd_event_add_signal(3), sd_event_add_time(3), sd_event_exit(3), sd_event_new(3), sd_event_now(3), sd_event_run(3), sd_event_set_watchdog(3), sd_event_source_get_event(3), sd_event_source_get_pending(3), sd_event_source_set_description(3), sd_event_source_set_destroy_callback(3), sd_event_source_set_enabled(3), sd_event_source_set_exit_on_failure(3), sd_event_source_set_floating(3), sd_event_source_set_prepare(3), sd_event_source_set_priority(3), sd_event_source_set_ratelimit(3), sd_event_source_set_userdata(3), sd_event_source_unref(3), sd_event_wait(3), systemd.directives(7), systemd.index(7)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |