sd_event_add_time(3) — Linux manual page


SD_EVENT_ADD_TIME(3)          sd_event_add_time         SD_EVENT_ADD_TIME(3)

NAME         top

       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, sd_event_source_get_time_clock,
       sd_event_time_handler_t - Add a timer event source to an event loop

SYNOPSIS         top

       #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_t usec,
                                              void *userdata);

       int sd_event_add_time(sd_event *event, sd_event_source **source,
                             clockid_t clock, uint64_t usec,
                             uint64_t accuracy,
                             sd_event_time_handler_t handler,
                             void *userdata);

       int sd_event_add_time_relative(sd_event *event,
                                      sd_event_source **source,
                                      clockid_t clock, uint64_t usec,
                                      uint64_t accuracy,
                                      sd_event_time_handler_t handler,
                                      void *userdata);

       int sd_event_source_get_time(sd_event_source *source,
                                    uint64_t *usec);

       int sd_event_source_set_time(sd_event_source *source, uint64_t usec);

       int sd_event_source_set_time_relative(sd_event_source *source,
                                             uint64_t usec);

       int sd_event_source_get_time_accuracy(sd_event_source *source,
                                             uint64_t *usec);

       int sd_event_source_set_time_accuracy(sd_event_source *source,
                                             uint64_t usec);

       int sd_event_source_get_time_clock(sd_event_source *source,
                                          clockid_t *clock);

DESCRIPTION         top

       sd_event_add_time() adds a new timer event source to an event loop.
       The event loop object is specified in the event parameter, the event
       source object is returned in the source parameter. The clock
       parameter takes a clock identifier, one of CLOCK_REALTIME,
       CLOCK_BOOTTIME_ALARM. See timerfd_create(2) for details regarding the
       various types of clocks. The usec 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
       (including 0), this timer source "fires" immediately and is ready to
       be dispatched. If the parameter is specified as UINT64_MAX the timer
       event will never elapse, which may be used as an alternative to
       explicitly disabling a timer event source with
       sd_event_source_set_enabled(3). The accuracy parameter specifies an
       additional accuracy value in µs specifying how much the timer event
       may be delayed. Use 0 to 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. The handler parameter
       shall reference a function to call when the timer elapses. The
       handler function will be passed the userdata pointer, which may be
       chosen freely by the caller. The handler is also passed the
       configured trigger time, even if it is actually called slightly
       later, subject to the specified accuracy value, the kernel timer
       slack (see prctl(2)), and additional scheduling latencies. To query
       the actual time the handler was called use sd_event_now(3).

       By default, the timer will elapse once (SD_EVENT_ONESHOT), but this
       may be changed with sd_event_source_set_enabled(3). If the handler
       function returns a negative error code, it will be disabled after the
       invocation, even if the SD_EVENT_ON mode was requested before. Note
       that a timer event set to SD_EVENT_ON will fire continuously unless
       its configured time is updated using sd_event_source_set_time().

       sd_event_add_time_relative() is like sd_event_add_time(), but takes a
       relative time specification. It's relative to the current time of the
       event loop iteration, as returned by sd_event_now(3).

       To destroy an event source object use sd_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 using sd_event_source_set_enabled(3) with

       If the second parameter of sd_event_add_time() is NULL no 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 the handler to sd_event_add_time() is NULL, and the event source
       fires, this will be considered a request to exit the event loop. In
       this case, the userdata parameter, cast to an integer, is used for
       the exit code passed to sd_event_exit(3).

       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 via sd_event_now(3), add the desired
       timespan to it, and use the result as the usec parameter to

       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 with sd_event_source_set_time(3) for the next
       timer iteration, and reenable the timer using
       sd_event_source_set_enabled(). To calculate the next point in time to
       pass to sd_event_source_set_time(), either use as base the usec
       parameter passed to the timer callback, or the timestamp returned by
       sd_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 with sd_event_add_time() or
       sd_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 via

       sd_event_source_set_time() changes the time of an event source
       created previously with sd_event_add_time() or
       sd_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 to
       sd_event_source_set_time(), but takes a time relative to the current
       time of the event loop iteration, as returned by sd_event_now().

       sd_event_source_get_time_accuracy() retrieves the configured accuracy
       value of an event source created previously with sd_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 with sd_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 with sd_event_add_time(). It takes
       the event source object and a pointer to a variable to store the
       clock identifier in.

RETURN VALUE         top

       On success, these functions return 0 or a positive integer. On
       failure, they return a negative errno-style error code.

       Returned values may indicate the following problems:

           Not enough memory to allocate an object.

           An invalid argument has been passed.

           The event loop is already terminated.

           The event loop has been created in a different process.

           The selected clock is not supported by the event loop

           The passed event source is not a timer event source.

           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).

NOTES         top

       These APIs are implemented as a shared library, which can be compiled
       and linked to with the libsystemd pkg-config(1) file.

SEE ALSO         top

       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),

COLOPHON         top

       This page is part of the systemd (systemd system and service manager)
       project.  Information about the project can be found at 
       ⟨⟩.  If you have a bug
       report for this manual page, see
       ⟨⟩.  This
       page was obtained from the project's upstream Git repository
       ⟨⟩ on 2020-09-18.  (At that
       time, the date of the most recent commit that was found in the repos‐
       itory was 2020-09-18.)  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 is not part
       of the original manual page), send a mail to

systemd 246                                             SD_EVENT_ADD_TIME(3)

Pages that refer to this page: sd-event(3)sd_event(3)sd_event_add_child(3)sd_event_add_child_pidfd(3)sd_event_add_defer(3)sd_event_add_exit(3)sd_event_add_inotify(3)sd_event_add_io(3)sd_event_add_post(3)sd_event_add_signal(3)sd_event_armed(3)SD_EVENT_ARMED(3)sd_event_child_handler_t(3)sd_event_default(3)sd_event_destroy_t(3)sd_event_dispatch(3)sd_event_exiting(3)SD_EVENT_EXITING(3)sd_event_finished(3)SD_EVENT_FINISHED(3)sd_event_get_iteration(3)sd_event_get_state(3)sd_event_get_tid(3)sd_event_get_watchdog(3)sd_event_handler_t(3)sd_event_initial(3)SD_EVENT_INITIAL(3)sd_event_inotify_handler_t(3)sd_event_io_handler_t(3)sd_event_loop(3)sd_event_new(3)sd_event_now(3)sd_event_off(3)SD_EVENT_OFF(3)sd_event_on(3)SD_EVENT_ON(3)sd_event_oneshot(3)SD_EVENT_ONESHOT(3)sd_event_pending(3)SD_EVENT_PENDING(3)sd_event_prepare(3)sd_event_preparing(3)SD_EVENT_PREPARING(3)sd_event_priority_idle(3)SD_EVENT_PRIORITY_IDLE(3)sd_event_priority_important(3)SD_EVENT_PRIORITY_IMPORTANT(3)sd_event_priority_normal(3)SD_EVENT_PRIORITY_NORMAL(3)sd_event_ref(3)sd_event_run(3)sd_event_running(3)SD_EVENT_RUNNING(3)sd_event_set_watchdog(3)sd_event_signal_handler_t(3)sd_event_source(3)sd_event_source_disable_unref(3)sd_event_source_disable_unrefp(3)sd_event_source_get_child_pid(3)sd_event_source_get_child_pidfd(3)sd_event_source_get_child_pidfd_own(3)sd_event_source_get_child_process_own(3)sd_event_source_get_description(3)sd_event_source_get_destroy_callback(3)sd_event_source_get_enabled(3)sd_event_source_get_event(3)sd_event_source_get_floating(3)sd_event_source_get_inotify_mask(3)sd_event_source_get_io_events(3)sd_event_source_get_io_fd(3)sd_event_source_get_io_fd_own(3)sd_event_source_get_io_revents(3)sd_event_source_get_pending(3)sd_event_source_get_priority(3)sd_event_source_get_signal(3)sd_event_source_get_userdata(3)sd_event_source_ref(3)sd_event_source_send_child_signal(3)sd_event_source_set_child_pidfd_own(3)sd_event_source_set_child_process_own(3)sd_event_source_set_description(3)sd_event_source_set_destroy_callback(3)sd_event_source_set_enabled(3)sd_event_source_set_floating(3)sd_event_source_set_io_events(3)sd_event_source_set_io_fd(3)sd_event_source_set_io_fd_own(3)sd_event_source_set_prepare(3)sd_event_source_set_priority(3)sd_event_source_set_userdata(3)sd_event_source_unref(3)sd_event_source_unrefp(3)sd_event_unref(3)sd_event_unrefp(3)sd_event_wait(3)30-systemd-environment-d-generator(7)systemd.directives(7)systemd.index(7)