Skip to content
Open

timerfd #99433

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions include/zephyr/posix/sys/timerfd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2025 Atym, Inc
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_POSIX_SYS_TIMERFD_H_
#define ZEPHYR_INCLUDE_POSIX_SYS_TIMERFD_H_

#include <zephyr/zvfs/timerfd.h>

#ifdef __cplusplus
extern "C" {
#endif

#define TFD_NONBLOCK ZVFS_TFD_NONBLOCK

#define TFD_IOC_SET_TICKS ZVFS_TFD_IOC_SET_TICKS

#define TFD_TIMER_ABSTIME 1

typedef zvfs_timerfd_t timerfd_t;

/**
* @brief Create a file descriptor for ZVFS timer notification
*
* The returned file descriptor can be used with POSIX read calls or
* with the @ref zvfs_timerfd_settime or @ref zvfs_timerfd_gettime functions.
*
* It also supports polling and by including an timerfd in a call to poll,
* it is possible to signal and wake the polling thread by simply writing to
* the timerfd.
*
* When using read() on a ZVFS timerfd, the size must always be at
* least 8 bytes or the operation will fail with EINVAL.
*
* @return New ZVFS timerfd file descriptor on success, -1 on error
*/
int timerfd_create(int clockid, int flags);

/**
* @brief Get the itimespec configuration ZVFS timerfd
*
* @param fd File descriptor
* @param curr_value Pointer to struct itimerspec to store current value
*
* @return 0 on success, -1 on error
*/
int timerfd_gettime(int fd, struct itimerspec *curr_value);

/**
* @brief Set the itimespec configuration ZVFS timerfd
*
* The current value is stored in old_value if it is not NULL.
*
* @param fd File descriptor
* @param new_value Pointer to struct itimerspec to set new value
* @param old_value Pointer to struct itimerspec to store old value
*
* @return 0 on success, -1 on error
*/
int timerfd_settime(int fd, int flags, const struct itimerspec *new_value,
struct itimerspec *old_value);

Check warning on line 63 in include/zephyr/posix/sys/timerfd.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LEADING_SPACE

include/zephyr/posix/sys/timerfd.h:63 please, no spaces at the start of a line

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_INCLUDE_POSIX_SYS_TIMERFD_H_ */
81 changes: 81 additions & 0 deletions include/zephyr/zvfs/timerfd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2025 Atym, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_ZEPHYR_ZVFS_TIMERFD_H_
#define ZEPHYR_INCLUDE_ZEPHYR_ZVFS_TIMERFD_H_

#include <stdint.h>

#include <zephyr/kernel.h>
#include <zephyr/sys/timeutil.h>

#ifdef __cplusplus
extern "C" {
#endif

#define ZVFS_TFD_NONBLOCK 0x4000

#define ZVFS_TFD_IOC_SET_TICKS 1

typedef uint64_t zvfs_timerfd_t;

/**
* @brief Create a file descriptor for ZVFS timer
*
* The returned file descriptor can be used with POSIX read calls or with
* the @ref zvfs_timerfd_* functions.
*
* It also supports polling and by including an timerfd in a call to poll,
* it is possible to signal and wake the polling thread when the timer
* expires.
*
* When using read() on a ZVFS timerfd, the size must always be at least
* 8 bytes or the operation will fail with EINVAL.
*
* @return New ZVFS timerfd file descriptor on success, -1 on error
*/
int zvfs_timerfd(unsigned int clockid, int flags);

/**
* @brief Read from a ZVFS timerfd
*
* If call is successful, the value parameter will have the number of
* times the timer has expired since the last read.
*
* @param fd File descriptor
* @param value Pointer for storing the expired times value
*
* @return 0 on success, -1 on error
*/
int zvfs_timerfd_read(int fd, zvfs_timerfd_t *value);

/**
* @brief Get the current value and period of a ZVFS timerfd.
*
* @param fd File descriptor
* @param remaining Pointer for storing the remaining milliseconds
* @param period Pointer for storing the period milliseconds
*
* @return 0 on success, -1 on error
*/
int zvfs_timerfd_gettime(int fd, uint32_t *remaining, uint32_t *period);

/**
* @brief Set the value and period of a ZVFS timerfd.
*
* @param fd File descriptor
* @param duration Duration to write
* @param period Period to write
*
* @return 0 on success, -1 on error
*/
int zvfs_timerfd_settime(int fd, k_timeout_t duration, k_timeout_t period);

#ifdef __cplusplus
}
#endif

#endif /* ZEPHYR_INCLUDE_ZEPHYR_ZVFS_TIMERFD_H_ */
1 change: 1 addition & 0 deletions lib/os/zvfs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

zephyr_library()
zephyr_library_sources_ifdef(CONFIG_ZVFS_EVENTFD zvfs_eventfd.c)
zephyr_library_sources_ifdef(CONFIG_ZVFS_TIMERFD zvfs_timerfd.c)
zephyr_library_sources_ifdef(CONFIG_ZVFS_POLL zvfs_poll.c)
zephyr_library_sources_ifdef(CONFIG_ZVFS_SELECT zvfs_select.c)
19 changes: 19 additions & 0 deletions lib/os/zvfs/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ config ZVFS_EVENTFD_MAX

endif # ZVFS_EVENTFD

config ZVFS_TIMERFD
bool "ZVFS timer file descriptor support"
imply ZVFS_POLL
help
Enable support for ZVFS timer file descriptors. An timerfd can
be used as an event wait/notify mechanism together with POSIX calls
like read, write and poll.

if ZVFS_TIMERFD

config ZVFS_TIMERFD_MAX
int "Maximum number of ZVFS timerfd's"
default 1
range 1 4096
help
The maximum number of supported timer file descriptors.

endif # ZVFS_TIMERFD

config ZVFS_POLL
bool "ZVFS poll"
select POLL
Expand Down
Loading
Loading