Donny9 opened a new pull request, #18238: URL: https://github.com/apache/nuttx/pull/18238
## Summary This PR implements Linux-compatible `TFD_TIMER_CANCEL_ON_SET` flag for timerfd, allowing applications to detect discontinuous changes to the system real-time clock. ### Background: According to the Linux [timerfd_create(2)](https://man7.org/linux/man-pages/man2/timerfd_create.2.html) man page: > If the associated clock is either CLOCK_REALTIME or CLOCK_REALTIME_ALARM, the timer is absolute (TFD_TIMER_ABSTIME), and the flag TFD_TIMER_CANCEL_ON_SET was specified when calling timerfd_settime(), then read(2) fails with the error ECANCELED if the real-time clock undergoes a discontinuous change. This feature is essential for applications that use absolute timers and need to respond to system time changes (e.g., NTP synchronization, manual time adjustments). ### Implementation Details: #### 1. Clock Notifier Infrastructure - New header: `include/nuttx/clock_notifier.h` - New source: `sched/clock/clock_notifier.c` - Provides notifier chain mechanism for clock change events - APIs: `register_clock_notifier()`, `unregister_clock_notifier()`, `clock_notifier_call_chain()` #### 2. Timerfd Enhancement - Add support for `TFD_TIMER_CANCEL_ON_SET` flag - Register with clock notifier when flag is set - Cancel timer and return `-ECANCELED` when clock change detected - Properly handle cleanup and unregistration #### 3. Clock Change Notifications Clock notifier is called in the following scenarios: - `clock_settime()` - manual time setting - `clock_initialize()` - system initialization - `clock_timekeeping_set_wall_time()` - timekeeping adjustments - `rpmsg_rtc` - remote processor time synchronization ### Why This Change: Linux Compatibility: Implements standard Linux timerfd behavior Time Change Detection: Applications can detect and respond to system time changes Robustness: Prevents stale absolute timers after time adjustments Use Cases: Essential for scheduling systems, alarm applications, and time-sensitive services ## Impact * Stability: Positive: Applications can now properly handle system time changes Positive: Prevents incorrect behavior with stale absolute timers No regression: Existing timerfd functionality unchanged (flag is opt-in) * Compatibility: Linux compatible: Implements standard Linux timerfd behavior Backward compatible: New flag is optional, existing code unaffected No breaking changes: All existing APIs remain unchanged * Code Quality: Enhanced: Adds proper clock change notification infrastructure Improved: Better separation of concerns with notifier pattern Maintainable: Clean API for future clock-related notifications ### Use Case Example: ```c // Create timerfd with CLOCK_REALTIME int tfd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK); // Set absolute timer with CANCEL_ON_SET flag struct itimerspec its; its.it_value.tv_sec = target_time; its.it_value.tv_nsec = 0; its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; timerfd_settime(tfd, TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET, &its, NULL); // Use with epoll int epfd = epoll_create1(0); struct epoll_event ev = {.events = EPOLLIN, .data.fd = tfd}; epoll_ctl(epfd, EPOLL_CTL_ADD, tfd, &ev); // Wait for events struct epoll_event events[1]; int n = epoll_wait(epfd, events, 1, -1); // Read timerfd uint64_t expirations; ssize_t ret = read(tfd, &expirations, sizeof(expirations)); if (ret < 0 && errno == ECANCELED) { // System time changed! Recalculate timer or take action printf("System time changed, updating schedule\n"); } else if (ret > 0) { // Normal timer expiration printf("Timer expired\n"); } -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
