2021-12-17 01:20:26 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/vfs/fs_timerfd.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/wdog.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2021-12-17 01:20:26 +01:00
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/timerfd.h>
|
|
|
|
|
|
|
|
#include "clock/clock.h"
|
|
|
|
#include "inode/inode.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
typedef struct timerfd_waiter_sem_s
|
|
|
|
{
|
|
|
|
sem_t sem;
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct timerfd_waiter_sem_s *next;
|
2021-12-17 01:20:26 +01:00
|
|
|
} timerfd_waiter_sem_t;
|
|
|
|
|
|
|
|
/* This structure describes the internal state of the driver */
|
|
|
|
|
|
|
|
struct timerfd_priv_s
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t lock; /* Enforces device exclusive access */
|
|
|
|
FAR timerfd_waiter_sem_t *rdsems; /* List of blocking readers */
|
|
|
|
int clock; /* Clock to use as the timing base */
|
|
|
|
int delay; /* If non-zero, used to reset repetitive
|
|
|
|
* timers */
|
|
|
|
struct wdog_s wdog; /* The watchdog that provides the timing */
|
|
|
|
timerfd_t counter; /* timerfd counter */
|
|
|
|
uint8_t crefs; /* References counts on timerfd (max: 255) */
|
2021-12-17 01:20:26 +01:00
|
|
|
|
|
|
|
/* The following is a list if poll structures of threads waiting for
|
|
|
|
* driver events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_TIMER_FD_POLL
|
|
|
|
FAR struct pollfd *fds[CONFIG_TIMER_FD_NPOLLWAITERS];
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int timerfd_open(FAR struct file *filep);
|
|
|
|
static int timerfd_close(FAR struct file *filep);
|
|
|
|
|
|
|
|
static ssize_t timerfd_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len);
|
|
|
|
#ifdef CONFIG_TIMER_FD_POLL
|
|
|
|
static int timerfd_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int timerfd_blocking_io(FAR struct timerfd_priv_s *dev,
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR timerfd_waiter_sem_t *sem,
|
2021-12-17 01:20:26 +01:00
|
|
|
FAR timerfd_waiter_sem_t **slist);
|
|
|
|
|
|
|
|
static FAR struct timerfd_priv_s *timerfd_allocdev(void);
|
|
|
|
static void timerfd_destroy(FAR struct timerfd_priv_s *dev);
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
static void timerfd_timeout(wdparm_t arg);
|
2021-12-17 01:20:26 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_timerfd_fops =
|
|
|
|
{
|
|
|
|
timerfd_open, /* open */
|
|
|
|
timerfd_close, /* close */
|
|
|
|
timerfd_read, /* read */
|
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* seek */
|
2022-01-10 15:51:17 +01:00
|
|
|
NULL, /* ioctl */
|
2023-01-05 13:33:35 +01:00
|
|
|
NULL, /* truncate */
|
|
|
|
NULL, /* mmap */
|
2021-12-17 01:20:26 +01:00
|
|
|
#ifdef CONFIG_TIMER_FD_POLL
|
2022-01-10 15:51:17 +01:00
|
|
|
timerfd_poll /* poll */
|
2021-12-17 01:20:26 +01:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2022-12-22 15:15:40 +01:00
|
|
|
static struct inode g_timerfd_inode =
|
|
|
|
{
|
|
|
|
NULL, /* i_parent */
|
|
|
|
NULL, /* i_peer */
|
|
|
|
NULL, /* i_child */
|
|
|
|
1, /* i_crefs */
|
|
|
|
FSNODEFLAG_TYPE_DRIVER, /* i_flags */
|
|
|
|
{
|
|
|
|
&g_timerfd_fops /* u */
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-17 01:20:26 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct timerfd_priv_s *timerfd_allocdev(void)
|
|
|
|
{
|
|
|
|
FAR struct timerfd_priv_s *dev;
|
|
|
|
|
|
|
|
dev = (FAR struct timerfd_priv_s *)
|
|
|
|
kmm_zalloc(sizeof(struct timerfd_priv_s));
|
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
/* Initialize the private structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&dev->lock);
|
|
|
|
nxmutex_lock(&dev->lock);
|
2021-12-17 01:20:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void timerfd_destroy(FAR struct timerfd_priv_s *dev)
|
|
|
|
{
|
|
|
|
wd_cancel(&dev->wdog);
|
2022-12-22 15:15:40 +01:00
|
|
|
nxmutex_unlock(&dev->lock);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&dev->lock);
|
2021-12-17 01:20:26 +01:00
|
|
|
kmm_free(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int timerfd_open(FAR struct file *filep)
|
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct timerfd_priv_s *priv = filep->f_priv;
|
2021-12-17 01:20:26 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the device structures */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2021-12-17 01:20:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (priv->crefs >= 255)
|
|
|
|
{
|
|
|
|
/* More than 255 opens; uint8_t would overflow to zero */
|
|
|
|
|
|
|
|
ret = -EMFILE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Save the new open count on success */
|
|
|
|
|
|
|
|
priv->crefs += 1;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2021-12-17 01:20:26 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int timerfd_close(FAR struct file *filep)
|
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct timerfd_priv_s *priv = filep->f_priv;
|
2021-12-17 01:20:26 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the device structures */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2021-12-17 01:20:26 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrement the references to the driver. If the reference count will
|
|
|
|
* decrement to 0, then uninitialize the driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (priv->crefs > 1)
|
|
|
|
{
|
|
|
|
/* Just decrement the reference count and release the semaphore */
|
|
|
|
|
|
|
|
priv->crefs--;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2021-12-17 01:20:26 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-create the path to the driver. */
|
|
|
|
|
|
|
|
finfo("destroy\n");
|
|
|
|
|
|
|
|
timerfd_destroy(priv);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int timerfd_blocking_io(FAR struct timerfd_priv_s *dev,
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR timerfd_waiter_sem_t *sem,
|
2021-12-17 01:20:26 +01:00
|
|
|
FAR timerfd_waiter_sem_t **slist)
|
|
|
|
{
|
|
|
|
int ret;
|
2022-12-22 15:15:40 +01:00
|
|
|
|
2021-12-17 01:20:26 +01:00
|
|
|
sem->next = *slist;
|
|
|
|
*slist = sem;
|
|
|
|
|
|
|
|
/* Wait for timerfd to notify */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&sem->sem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR timerfd_waiter_sem_t *cur_sem;
|
|
|
|
|
|
|
|
cur_sem = *slist;
|
2021-12-17 01:20:26 +01:00
|
|
|
if (cur_sem == sem)
|
|
|
|
{
|
|
|
|
*slist = sem->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (cur_sem)
|
|
|
|
{
|
|
|
|
if (cur_sem->next == sem)
|
|
|
|
{
|
|
|
|
cur_sem->next = sem->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
return ret;
|
2021-12-17 01:20:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t timerfd_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct timerfd_priv_s *dev = filep->f_priv;
|
2021-12-17 01:20:26 +01:00
|
|
|
irqstate_t intflags;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if (len < sizeof(timerfd_t) || buffer == NULL)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
/* Ensure that interrupts are disabled and we do not lose counts
|
|
|
|
* if expiration occurs after read, but before setting counter
|
|
|
|
* to zero
|
|
|
|
*/
|
|
|
|
|
|
|
|
intflags = enter_critical_section();
|
2021-12-17 01:20:26 +01:00
|
|
|
|
|
|
|
/* Wait for an incoming event */
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
if (dev->counter == 0)
|
2021-12-17 01:20:26 +01:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
timerfd_waiter_sem_t sem;
|
|
|
|
|
2021-12-17 01:20:26 +01:00
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
2022-12-23 07:37:39 +01:00
|
|
|
leave_critical_section(intflags);
|
2021-12-17 01:20:26 +01:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
nxsem_init(&sem.sem, 0, 0);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = timerfd_blocking_io(dev, &sem, &dev->rdsems);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-12-23 07:37:39 +01:00
|
|
|
leave_critical_section(intflags);
|
2021-12-17 01:20:26 +01:00
|
|
|
nxsem_destroy(&sem.sem);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2022-12-23 07:37:39 +01:00
|
|
|
while (dev->counter == 0);
|
2021-12-17 01:20:26 +01:00
|
|
|
|
|
|
|
nxsem_destroy(&sem.sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
*(FAR timerfd_t *)buffer = dev->counter;
|
|
|
|
dev->counter = 0;
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
leave_critical_section(intflags);
|
2021-12-17 01:20:26 +01:00
|
|
|
|
|
|
|
return sizeof(timerfd_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_TIMER_FD_POLL
|
|
|
|
static int timerfd_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
|
|
|
bool setup)
|
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct timerfd_priv_s *dev = filep->f_priv;
|
2022-12-23 07:37:39 +01:00
|
|
|
irqstate_t intflags;
|
|
|
|
int ret = OK;
|
2021-12-17 01:20:26 +01:00
|
|
|
int i;
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
intflags = enter_critical_section();
|
2021-12-17 01:20:26 +01:00
|
|
|
if (!setup)
|
|
|
|
{
|
|
|
|
/* This is a request to tear down the poll. */
|
|
|
|
|
|
|
|
FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
|
|
|
|
|
|
|
|
/* Remove all memory of the poll setup */
|
|
|
|
|
2022-12-22 15:15:40 +01:00
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
2021-12-17 01:20:26 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a request to set up the poll. Find an available
|
|
|
|
* slot for the poll structure reference
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_TIMER_FD_NPOLLWAITERS; i++)
|
|
|
|
{
|
|
|
|
/* Find an available slot */
|
|
|
|
|
|
|
|
if (!dev->fds[i])
|
|
|
|
{
|
|
|
|
/* Bind the poll structure and this slot */
|
|
|
|
|
|
|
|
dev->fds[i] = fds;
|
|
|
|
fds->priv = &dev->fds[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= CONFIG_TIMER_FD_NPOLLWAITERS)
|
|
|
|
{
|
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify the POLLIN event if the counter is not zero */
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
if (dev->counter > 0)
|
2021-12-17 01:20:26 +01:00
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
#ifdef CONFIG_TIMER_FD_POLL
|
|
|
|
poll_notify(dev->fds, CONFIG_TIMER_FD_NPOLLWAITERS, POLLIN);
|
|
|
|
#endif
|
2021-12-17 01:20:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2022-12-23 07:37:39 +01:00
|
|
|
leave_critical_section(intflags);
|
2021-12-17 01:20:26 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
static void timerfd_timeout(wdparm_t arg)
|
2021-12-17 01:20:26 +01:00
|
|
|
{
|
|
|
|
FAR struct timerfd_priv_s *dev = (FAR struct timerfd_priv_s *)arg;
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR timerfd_waiter_sem_t *cur_sem;
|
2022-12-23 07:37:39 +01:00
|
|
|
irqstate_t intflags;
|
2021-12-17 01:20:26 +01:00
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
/* 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 > 0)
|
2021-12-17 01:20:26 +01:00
|
|
|
{
|
2022-12-23 07:37:39 +01:00
|
|
|
wd_start(&dev->wdog, dev->delay, timerfd_timeout, arg);
|
2021-12-17 01:20:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_TIMER_FD_POLL
|
|
|
|
/* Notify all poll/select waiters */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(dev->fds, CONFIG_TIMER_FD_NPOLLWAITERS, POLLIN);
|
2021-12-17 01:20:26 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Notify all of the waiting readers */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
cur_sem = dev->rdsems;
|
2021-12-17 01:20:26 +01:00
|
|
|
while (cur_sem != NULL)
|
|
|
|
{
|
|
|
|
nxsem_post(&cur_sem->sem);
|
|
|
|
cur_sem = cur_sem->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->rdsems = NULL;
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
leave_critical_section(intflags);
|
2021-12-17 01:20:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int timerfd_create(int clockid, int flags)
|
|
|
|
{
|
|
|
|
FAR struct timerfd_priv_s *new_dev;
|
|
|
|
int new_fd;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity checks. */
|
|
|
|
|
2022-12-22 15:15:40 +01:00
|
|
|
if ((clockid != CLOCK_REALTIME &&
|
|
|
|
clockid != CLOCK_MONOTONIC &&
|
|
|
|
clockid != CLOCK_BOOTTIME) ||
|
|
|
|
(flags & ~(TFD_NONBLOCK | TFD_CLOEXEC)) != 0)
|
2021-12-17 01:20:26 +01:00
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate instance data for this driver */
|
|
|
|
|
|
|
|
new_dev = timerfd_allocdev();
|
|
|
|
if (new_dev == NULL)
|
|
|
|
{
|
|
|
|
/* Failed to allocate new device */
|
|
|
|
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the timer instance */
|
|
|
|
|
|
|
|
new_dev->clock = clockid;
|
2022-12-22 15:15:40 +01:00
|
|
|
new_fd = file_allocate(&g_timerfd_inode, O_RDONLY | flags,
|
|
|
|
0, new_dev, 0, true);
|
|
|
|
if (new_fd < 0)
|
2021-12-17 01:20:26 +01:00
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
ret = new_fd;
|
|
|
|
goto errout_with_dev;
|
2021-12-17 01:20:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Device is ready for use */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&new_dev->lock);
|
2021-12-17 01:20:26 +01:00
|
|
|
|
|
|
|
return new_fd;
|
|
|
|
|
2022-12-22 15:15:40 +01:00
|
|
|
errout_with_dev:
|
2021-12-17 01:20:26 +01:00
|
|
|
timerfd_destroy(new_dev);
|
|
|
|
errout:
|
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int timerfd_settime(int fd, int flags,
|
|
|
|
FAR const struct itimerspec *new_value,
|
|
|
|
FAR struct itimerspec *old_value)
|
|
|
|
{
|
|
|
|
FAR struct timerfd_priv_s *dev;
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct file *filep;
|
2021-12-17 01:20:26 +01:00
|
|
|
irqstate_t intflags;
|
|
|
|
sclock_t delay;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Some sanity checks */
|
|
|
|
|
|
|
|
if (!new_value)
|
|
|
|
{
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
if ((flags & ~TFD_TIMER_ABSTIME) != 0)
|
2021-12-17 01:20:26 +01:00
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get file pointer by file descriptor */
|
|
|
|
|
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check fd come from us */
|
|
|
|
|
|
|
|
if (!filep->f_inode || filep->f_inode->u.i_ops != &g_timerfd_fops)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:15:40 +01:00
|
|
|
dev = (FAR struct timerfd_priv_s *)filep->f_priv;
|
2021-12-17 01:20:26 +01:00
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
/* Disable interrupts here to ensure that expiration counter is accessed
|
|
|
|
* atomicaly.
|
|
|
|
*/
|
|
|
|
|
|
|
|
intflags = enter_critical_section();
|
|
|
|
|
2021-12-17 01:20:26 +01:00
|
|
|
if (old_value)
|
|
|
|
{
|
|
|
|
/* Get the number of ticks before the underlying watchdog expires */
|
|
|
|
|
|
|
|
delay = wd_gettime(&dev->wdog);
|
|
|
|
|
|
|
|
/* Convert that to a struct timespec and return it */
|
|
|
|
|
|
|
|
clock_ticks2time(delay, &old_value->it_value);
|
|
|
|
clock_ticks2time(dev->delay, &old_value->it_interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disarm the timer (in case the timer was already armed when
|
|
|
|
* timerfd_settime() is called).
|
|
|
|
*/
|
|
|
|
|
|
|
|
wd_cancel(&dev->wdog);
|
|
|
|
|
|
|
|
/* Clear expiration counter */
|
|
|
|
|
|
|
|
dev->counter = 0;
|
|
|
|
|
|
|
|
/* If the it_value member of value is zero, the timer will not be
|
|
|
|
* re-armed
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (new_value->it_value.tv_sec <= 0 && new_value->it_value.tv_nsec <= 0)
|
|
|
|
{
|
2022-12-23 07:37:39 +01:00
|
|
|
leave_critical_section(intflags);
|
2021-12-17 01:20:26 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup up any repetitive timer */
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
clock_time2ticks(&new_value->it_interval, &delay);
|
|
|
|
dev->delay = delay;
|
2021-12-17 01:20:26 +01:00
|
|
|
|
|
|
|
/* We need to disable timer interrupts through the following section so
|
|
|
|
* that the system timer is stable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Check if abstime is selected */
|
|
|
|
|
|
|
|
if ((flags & TFD_TIMER_ABSTIME) != 0)
|
|
|
|
{
|
|
|
|
/* Calculate a delay corresponding to the absolute time in 'value' */
|
|
|
|
|
|
|
|
clock_abstime2ticks(dev->clock, &new_value->it_value, &delay);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Calculate a delay assuming that 'value' holds the relative time
|
|
|
|
* to wait. We have internal knowledge that clock_time2ticks always
|
|
|
|
* returns success.
|
|
|
|
*/
|
|
|
|
|
|
|
|
clock_time2ticks(&new_value->it_value, &delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the time is in the past or now, then set up the next interval
|
|
|
|
* instead (assuming a repetitive timer).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (delay <= 0)
|
|
|
|
{
|
|
|
|
delay = dev->delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then start the watchdog */
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
ret = wd_start(&dev->wdog, delay, timerfd_timeout, (wdparm_t)dev);
|
|
|
|
if (ret < 0)
|
2021-12-17 01:20:26 +01:00
|
|
|
{
|
2022-12-23 07:37:39 +01:00
|
|
|
leave_critical_section(intflags);
|
|
|
|
goto errout;
|
2021-12-17 01:20:26 +01:00
|
|
|
}
|
|
|
|
|
2022-12-23 07:37:39 +01:00
|
|
|
leave_critical_section(intflags);
|
2021-12-17 01:20:26 +01:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int timerfd_gettime(int fd, FAR struct itimerspec *curr_value)
|
|
|
|
{
|
|
|
|
FAR struct timerfd_priv_s *dev;
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct file *filep;
|
2021-12-17 01:20:26 +01:00
|
|
|
sclock_t ticks;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Some sanity checks */
|
|
|
|
|
|
|
|
if (!curr_value)
|
|
|
|
{
|
|
|
|
ret = -EFAULT;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get file pointer by file descriptor */
|
|
|
|
|
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check fd come from us */
|
|
|
|
|
|
|
|
if (!filep->f_inode || filep->f_inode->u.i_ops != &g_timerfd_fops)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2022-12-22 15:15:40 +01:00
|
|
|
dev = (FAR struct timerfd_priv_s *)filep->f_priv;
|
2021-12-17 01:20:26 +01:00
|
|
|
|
|
|
|
/* Get the number of ticks before the underlying watchdog expires */
|
|
|
|
|
|
|
|
ticks = wd_gettime(&dev->wdog);
|
|
|
|
|
|
|
|
/* Convert that to a struct timespec and return it */
|
|
|
|
|
|
|
|
clock_ticks2time(ticks, &curr_value->it_value);
|
|
|
|
clock_ticks2time(dev->delay, &curr_value->it_interval);
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
|
|
|
}
|