Skip to content

Commit 1f04ad1

Browse files
committed
zvfs: introduce timerfd implementation
This add a timerfd-like interface similar to other systems. Although this is not POSIX, it is very popular alternative to POSIX timers. The implementation is based on the current eventfd implementation, with the relevant improvements and changes. Add the ZVFS_TIMERFD so we can selectively enable/disable the timerfd implementation. Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
1 parent c73c306 commit 1f04ad1

File tree

4 files changed

+638
-0
lines changed

4 files changed

+638
-0
lines changed

include/zephyr/zvfs/timerfd.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2025 Atym, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_ZEPHYR_ZVFS_TIMERFD_H_
8+
#define ZEPHYR_INCLUDE_ZEPHYR_ZVFS_TIMERFD_H_
9+
10+
#include <stdint.h>
11+
12+
#include <zephyr/kernel.h>
13+
#include <zephyr/sys/timeutil.h>
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
#define ZVFS_TFD_NONBLOCK 0x4000
20+
21+
#define ZVFS_TFD_IOC_SET_TICKS 1
22+
23+
typedef uint64_t zvfs_timerfd_t;
24+
25+
/**
26+
* @brief Create a file descriptor for ZVFS timer
27+
*
28+
* The returned file descriptor can be used with POSIX read calls or with
29+
* the @ref zvfs_timerfd_* functions.
30+
*
31+
* It also supports polling and by including an timerfd in a call to poll,
32+
* it is possible to signal and wake the polling thread when the timer
33+
* expires.
34+
*
35+
* When using read() on a ZVFS timerfd, the size must always be at least
36+
* 8 bytes or the operation will fail with EINVAL.
37+
*
38+
* @return New ZVFS timerfd file descriptor on success, -1 on error
39+
*/
40+
int zvfs_timerfd(unsigned int clockid, int flags);
41+
42+
/**
43+
* @brief Read from a ZVFS timerfd
44+
*
45+
* If call is successful, the value parameter will have the number of
46+
* times the timer has expired since the last read.
47+
*
48+
* @param fd File descriptor
49+
* @param value Pointer for storing the expired times value
50+
*
51+
* @return 0 on success, -1 on error
52+
*/
53+
int zvfs_timerfd_read(int fd, zvfs_timerfd_t *value);
54+
55+
/**
56+
* @brief Get the current value and period of a ZVFS timerfd.
57+
*
58+
* @param fd File descriptor
59+
* @param remaining Pointer for storing the remaining milliseconds
60+
* @param period Pointer for storing the period milliseconds
61+
*
62+
* @return 0 on success, -1 on error
63+
*/
64+
int zvfs_timerfd_gettime(int fd, uint32_t *remaining, uint32_t *period);
65+
66+
/**
67+
* @brief Set the value and period of a ZVFS timerfd.
68+
*
69+
* @param fd File descriptor
70+
* @param duration Duration to write
71+
* @param period Period to write
72+
*
73+
* @return 0 on success, -1 on error
74+
*/
75+
int zvfs_timerfd_settime(int fd, k_timeout_t duration, k_timeout_t period);
76+
77+
#ifdef __cplusplus
78+
}
79+
#endif
80+
81+
#endif /* ZEPHYR_INCLUDE_ZEPHYR_ZVFS_TIMERFD_H_ */

lib/os/zvfs/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
zephyr_library()
44
zephyr_library_sources_ifdef(CONFIG_ZVFS_EVENTFD zvfs_eventfd.c)
5+
zephyr_library_sources_ifdef(CONFIG_ZVFS_TIMERFD zvfs_timerfd.c)
56
zephyr_library_sources_ifdef(CONFIG_ZVFS_POLL zvfs_poll.c)
67
zephyr_library_sources_ifdef(CONFIG_ZVFS_SELECT zvfs_select.c)

lib/os/zvfs/Kconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@ config ZVFS_EVENTFD_MAX
3434

3535
endif # ZVFS_EVENTFD
3636

37+
config ZVFS_TIMERFD
38+
bool "ZVFS timer file descriptor support"
39+
imply ZVFS_POLL
40+
help
41+
Enable support for ZVFS timer file descriptors. An timerfd can
42+
be used as an event wait/notify mechanism together with POSIX calls
43+
like read, write and poll.
44+
45+
if ZVFS_TIMERFD
46+
47+
config ZVFS_TIMERFD_MAX
48+
int "Maximum number of ZVFS timerfd's"
49+
default 1
50+
range 1 4096
51+
help
52+
The maximum number of supported timer file descriptors.
53+
54+
endif # ZVFS_TIMERFD
55+
3756
config ZVFS_POLL
3857
bool "ZVFS poll"
3958
select POLL

0 commit comments

Comments
 (0)