mirror of
https://github.com/apache/nuttx.git
synced 2026-05-28 20:08:15 +08:00
fs/timerfd: implement TFD_TIMER_CANCEL_ON_SET to detect clock changes
Implement Linux-compatible TFD_TIMER_CANCEL_ON_SET flag for timerfd to allow applications to detect discontinuous changes to CLOCK_REALTIME. Background: According to Linux timerfd_create(2) man page, when a timerfd is created with CLOCK_REALTIME and TFD_TIMER_CANCEL_ON_SET flag is specified, the read() operation should fail with ECANCELED if the real-time clock undergoes a discontinuous change. This allows applications to detect and respond to system time changes. Implementation: 1. Add clock notifier infrastructure (clock_notifier.h/c) to notify interested parties when system time changes 2. Implement TFD_TIMER_CANCEL_ON_SET flag support in timerfd 3. Register timerfd with clock notifier when flag is set 4. Cancel timer and return ECANCELED when clock change is detected 5. Notify clock changes in: - clock_settime() - clock_initialize() - clock_timekeeping_set_wall_time() - rpmsg_rtc time synchronization Changes include: - New files: include/nuttx/clock_notifier.h, sched/clock/clock_notifier.c - Modified: fs/vfs/fs_timerfd.c to handle TFD_TIMER_CANCEL_ON_SET - Modified: clock subsystem to call notifier chain on time changes - Modified: rpmsg_rtc to notify time changes during sync Use case example: Applications using timerfd with absolute time can now detect when system time is adjusted (e.g., NTP sync, manual time change) and take appropriate action such as recalculating timeouts or updating scheduled events. Reference: https://man7.org/linux/man-pages/man2/timerfd_create.2.html Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
+103
-34
@@ -36,6 +36,8 @@
|
||||
#include <nuttx/irq.h>
|
||||
#include <nuttx/wdog.h>
|
||||
#include <nuttx/mutex.h>
|
||||
#include <nuttx/nuttx.h>
|
||||
#include <nuttx/clock_notifier.h>
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/timerfd.h>
|
||||
@@ -70,6 +72,8 @@ struct timerfd_priv_s
|
||||
struct wdog_s wdog; /* The watchdog that provides the timing */
|
||||
timerfd_t counter; /* timerfd counter */
|
||||
uint8_t crefs; /* References counts on timerfd (max: 255) */
|
||||
struct notifier_block nb; /* The clock notifier node */
|
||||
bool cancel; /* Canceled by discontinuous change to the clock */
|
||||
|
||||
/* The following is a list if poll structures of threads waiting for
|
||||
* driver events.
|
||||
@@ -156,8 +160,19 @@ static FAR struct timerfd_priv_s *timerfd_allocdev(void)
|
||||
return dev;
|
||||
}
|
||||
|
||||
static void
|
||||
timerfd_unregister_clock_notifier(FAR struct timerfd_priv_s *dev)
|
||||
{
|
||||
if (dev->nb.notifier_call)
|
||||
{
|
||||
unregister_clock_notifier(&dev->nb);
|
||||
dev->nb.notifier_call = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void timerfd_destroy(FAR struct timerfd_priv_s *dev)
|
||||
{
|
||||
timerfd_unregister_clock_notifier(dev);
|
||||
wd_cancel(&dev->wdog);
|
||||
nxmutex_unlock(&dev->lock);
|
||||
nxmutex_destroy(&dev->lock);
|
||||
@@ -287,6 +302,13 @@ static ssize_t timerfd_read(FAR struct file *filep, FAR char *buffer,
|
||||
|
||||
intflags = enter_critical_section();
|
||||
|
||||
if (dev->cancel)
|
||||
{
|
||||
dev->cancel = false;
|
||||
leave_critical_section(intflags);
|
||||
return -ECANCELED;
|
||||
}
|
||||
|
||||
/* Wait for an incoming event */
|
||||
|
||||
if (dev->counter == 0)
|
||||
@@ -309,6 +331,13 @@ static ssize_t timerfd_read(FAR struct file *filep, FAR char *buffer,
|
||||
nxsem_destroy(&sem.sem);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (dev->cancel)
|
||||
{
|
||||
dev->cancel = false;
|
||||
leave_critical_section(intflags);
|
||||
return -ECANCELED;
|
||||
}
|
||||
}
|
||||
while (dev->counter == 0);
|
||||
|
||||
@@ -373,11 +402,9 @@ static int timerfd_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
||||
|
||||
/* Notify the POLLIN event if the counter is not zero */
|
||||
|
||||
if (dev->counter > 0)
|
||||
if (dev->counter > 0 || dev->cancel)
|
||||
{
|
||||
#ifdef CONFIG_TIMER_FD_POLL
|
||||
poll_notify(&fds, 1, POLLIN);
|
||||
#endif
|
||||
}
|
||||
|
||||
out:
|
||||
@@ -386,28 +413,9 @@ out:
|
||||
}
|
||||
#endif
|
||||
|
||||
static void timerfd_timeout(wdparm_t arg)
|
||||
static void timerfd_notify(FAR struct timerfd_priv_s *dev)
|
||||
{
|
||||
FAR struct timerfd_priv_s *dev = (FAR struct timerfd_priv_s *)arg;
|
||||
FAR timerfd_waiter_sem_t *cur_sem;
|
||||
irqstate_t intflags;
|
||||
|
||||
/* Disable interrupts to ensure that expiration counter is accessed
|
||||
* atomically
|
||||
*/
|
||||
|
||||
intflags = enter_critical_section();
|
||||
|
||||
/* Increment timer expiration counter */
|
||||
|
||||
dev->counter++;
|
||||
|
||||
/* If this is a repetitive timer, then restart the watchdog */
|
||||
|
||||
if (dev->delay > 0u)
|
||||
{
|
||||
wd_start(&dev->wdog, dev->delay, timerfd_timeout, arg);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_TIMER_FD_POLL
|
||||
/* Notify all poll/select waiters */
|
||||
@@ -426,6 +434,56 @@ static void timerfd_timeout(wdparm_t arg)
|
||||
|
||||
dev->rdsems = NULL;
|
||||
|
||||
if (dev->delay > 0)
|
||||
{
|
||||
wd_start(&dev->wdog, dev->delay, timerfd_timeout,
|
||||
(wdparm_t)dev);
|
||||
}
|
||||
else
|
||||
{
|
||||
timerfd_unregister_clock_notifier(dev);
|
||||
}
|
||||
}
|
||||
|
||||
static int timerfd_changed_handler(FAR struct notifier_block *nb,
|
||||
unsigned long action, FAR void *data)
|
||||
{
|
||||
if (action == CLOCK_REALTIME)
|
||||
{
|
||||
FAR struct timerfd_priv_s *dev;
|
||||
|
||||
dev = container_of(nb, struct timerfd_priv_s, nb);
|
||||
dev->cancel = true;
|
||||
wd_cancel(&dev->wdog);
|
||||
timerfd_notify(dev);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void timerfd_timeout(wdparm_t arg)
|
||||
{
|
||||
FAR struct timerfd_priv_s *dev = (FAR struct timerfd_priv_s *)arg;
|
||||
irqstate_t intflags;
|
||||
|
||||
/* Disable interrupts to ensure that expiration counter is accessed
|
||||
* atomically
|
||||
*/
|
||||
|
||||
intflags = enter_critical_section();
|
||||
|
||||
if (dev->cancel)
|
||||
{
|
||||
leave_critical_section(intflags);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Increment timer expiration counter */
|
||||
|
||||
dev->counter++;
|
||||
|
||||
timerfd_notify(dev);
|
||||
|
||||
leave_critical_section(intflags);
|
||||
}
|
||||
|
||||
@@ -503,7 +561,7 @@ int timerfd_settime(int fd, int flags,
|
||||
goto errout;
|
||||
}
|
||||
|
||||
if ((flags & ~TFD_TIMER_ABSTIME) != 0)
|
||||
if ((flags & ~(TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)) != 0)
|
||||
{
|
||||
ret = -EINVAL;
|
||||
goto errout;
|
||||
@@ -548,6 +606,10 @@ int timerfd_settime(int fd, int flags,
|
||||
|
||||
wd_cancel(&dev->wdog);
|
||||
|
||||
/* Unregister notifier cb if it exists */
|
||||
|
||||
timerfd_unregister_clock_notifier(dev);
|
||||
|
||||
/* Clear expiration counter */
|
||||
|
||||
dev->counter = 0;
|
||||
@@ -558,9 +620,7 @@ int timerfd_settime(int fd, int flags,
|
||||
|
||||
if (new_value->it_value.tv_sec <= 0 && new_value->it_value.tv_nsec <= 0)
|
||||
{
|
||||
leave_critical_section(intflags);
|
||||
file_put(filep);
|
||||
return OK;
|
||||
goto errout_with_csection;
|
||||
}
|
||||
|
||||
/* Setup up any repetitive timer */
|
||||
@@ -599,24 +659,33 @@ int timerfd_settime(int fd, int flags,
|
||||
delay = dev->delay;
|
||||
}
|
||||
|
||||
if (flags & TFD_TIMER_CANCEL_ON_SET)
|
||||
{
|
||||
dev->nb.notifier_call = timerfd_changed_handler;
|
||||
register_clock_notifier(&dev->nb);
|
||||
}
|
||||
|
||||
/* Then start the watchdog */
|
||||
|
||||
ret = wd_start(&dev->wdog, delay, timerfd_timeout, (wdparm_t)dev);
|
||||
if (ret < 0)
|
||||
{
|
||||
leave_critical_section(intflags);
|
||||
goto errout_with_filep;
|
||||
timerfd_unregister_clock_notifier(dev);
|
||||
goto errout_with_csection;
|
||||
}
|
||||
|
||||
errout_with_csection:
|
||||
leave_critical_section(intflags);
|
||||
file_put(filep);
|
||||
return OK;
|
||||
|
||||
errout_with_filep:
|
||||
file_put(filep);
|
||||
errout:
|
||||
set_errno(-ret);
|
||||
return ERROR;
|
||||
if (ret < 0)
|
||||
{
|
||||
set_errno(-ret);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
int timerfd_gettime(int fd, FAR struct itimerspec *curr_value)
|
||||
|
||||
Reference in New Issue
Block a user