2020-07-28 13:08:48 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* fs/vfs/fs_eventfd.c
|
2020-07-28 13:08:48 +02:00
|
|
|
*
|
|
|
|
* 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>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2020-07-28 13:08:48 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <debug.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2020-07-28 13:08:48 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/eventfd.h>
|
|
|
|
|
|
|
|
#include "inode/inode.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
typedef struct eventfd_waiter_sem_s
|
|
|
|
{
|
|
|
|
sem_t sem;
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct eventfd_waiter_sem_s *next;
|
2020-07-28 13:08:48 +02:00
|
|
|
} eventfd_waiter_sem_t;
|
|
|
|
|
|
|
|
/* This structure describes the internal state of the driver */
|
|
|
|
|
|
|
|
struct eventfd_priv_s
|
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
mutex_t lock; /* Enforces device exclusive access */
|
|
|
|
FAR eventfd_waiter_sem_t *rdsems; /* List of blocking readers */
|
|
|
|
FAR eventfd_waiter_sem_t *wrsems; /* List of blocking writers */
|
|
|
|
eventfd_t counter; /* eventfd counter */
|
|
|
|
uint8_t crefs; /* References counts on eventfd (max: 255) */
|
2020-07-28 13:08:48 +02:00
|
|
|
|
|
|
|
/* The following is a list if poll structures of threads waiting for
|
|
|
|
* driver events.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_EVENT_FD_POLL
|
|
|
|
FAR struct pollfd *fds[CONFIG_EVENT_FD_NPOLLWAITERS];
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int eventfd_do_open(FAR struct file *filep);
|
|
|
|
static int eventfd_do_close(FAR struct file *filep);
|
|
|
|
|
|
|
|
static ssize_t eventfd_do_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len);
|
|
|
|
static ssize_t eventfd_do_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t len);
|
|
|
|
#ifdef CONFIG_EVENT_FD_POLL
|
|
|
|
static int eventfd_do_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2023-11-19 12:19:53 +01:00
|
|
|
bool setup);
|
2020-07-28 13:08:48 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static int eventfd_blocking_io(FAR struct eventfd_priv_s *dev,
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR eventfd_waiter_sem_t *sem,
|
2020-07-28 13:08:48 +02:00
|
|
|
FAR eventfd_waiter_sem_t **slist);
|
|
|
|
|
|
|
|
static FAR struct eventfd_priv_s *eventfd_allocdev(void);
|
|
|
|
static void eventfd_destroy(FAR struct eventfd_priv_s *dev);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_eventfd_fops =
|
|
|
|
{
|
|
|
|
eventfd_do_open, /* open */
|
|
|
|
eventfd_do_close, /* close */
|
|
|
|
eventfd_do_read, /* read */
|
|
|
|
eventfd_do_write, /* write */
|
2021-12-17 19:31:05 +01:00
|
|
|
NULL, /* seek */
|
2022-01-10 15:51:17 +01:00
|
|
|
NULL, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2020-07-28 13:08:48 +02:00
|
|
|
#ifdef CONFIG_EVENT_FD_POLL
|
2022-01-10 15:51:17 +01:00
|
|
|
eventfd_do_poll /* poll */
|
2020-07-28 13:08:48 +02:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2022-12-22 15:15:40 +01:00
|
|
|
static struct inode g_eventfd_inode =
|
|
|
|
{
|
|
|
|
NULL, /* i_parent */
|
|
|
|
NULL, /* i_peer */
|
|
|
|
NULL, /* i_child */
|
|
|
|
1, /* i_crefs */
|
|
|
|
FSNODEFLAG_TYPE_DRIVER, /* i_flags */
|
|
|
|
{
|
|
|
|
&g_eventfd_fops /* u */
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-28 13:08:48 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct eventfd_priv_s *eventfd_allocdev(void)
|
|
|
|
{
|
|
|
|
FAR struct eventfd_priv_s *dev;
|
|
|
|
|
|
|
|
dev = (FAR struct eventfd_priv_s *)
|
|
|
|
kmm_zalloc(sizeof(struct eventfd_priv_s));
|
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
/* Initialize the private structure */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&dev->lock);
|
|
|
|
nxmutex_lock(&dev->lock);
|
2023-09-08 10:02:49 +02:00
|
|
|
dev->crefs++;
|
2020-07-28 13:08:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void eventfd_destroy(FAR struct eventfd_priv_s *dev)
|
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
nxmutex_unlock(&dev->lock);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_destroy(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
kmm_free(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int eventfd_do_open(FAR struct file *filep)
|
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct eventfd_priv_s *priv = filep->f_priv;
|
2020-07-28 13:08:48 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the device structures */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-07-28 13:08:48 +02: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);
|
2020-07-28 13:08:48 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int eventfd_do_close(FAR struct file *filep)
|
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct eventfd_priv_s *priv = filep->f_priv;
|
2020-07-28 13:08:48 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get exclusive access to the device structures */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&priv->lock);
|
2020-07-28 13:08:48 +02: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 -= 1;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&priv->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-create the path to the driver. */
|
|
|
|
|
|
|
|
finfo("destroy\n");
|
|
|
|
|
|
|
|
eventfd_destroy(priv);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int eventfd_blocking_io(FAR struct eventfd_priv_s *dev,
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR eventfd_waiter_sem_t *sem,
|
2020-07-28 13:08:48 +02:00
|
|
|
FAR eventfd_waiter_sem_t **slist)
|
|
|
|
{
|
|
|
|
int ret;
|
2022-12-22 15:15:40 +01:00
|
|
|
|
2020-07-28 13:08:48 +02:00
|
|
|
sem->next = *slist;
|
|
|
|
*slist = sem;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
|
|
|
|
/* Wait for eventfd to notify */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&sem->sem);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR eventfd_waiter_sem_t *cur_sem;
|
|
|
|
|
2020-07-28 13:08:48 +02:00
|
|
|
/* Interrupted wait, unregister semaphore
|
2022-09-06 08:18:45 +02:00
|
|
|
* TODO ensure that lock wait does not fail (ECANCELED)
|
2020-07-28 13:08:48 +02:00
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
cur_sem = *slist;
|
2020-07-28 13:08:48 +02:00
|
|
|
if (cur_sem == sem)
|
|
|
|
{
|
|
|
|
*slist = sem->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (cur_sem)
|
|
|
|
{
|
|
|
|
if (cur_sem->next == sem)
|
|
|
|
{
|
|
|
|
cur_sem->next = sem->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
return nxmutex_lock(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t eventfd_do_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t len)
|
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct eventfd_priv_s *dev = filep->f_priv;
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR eventfd_waiter_sem_t *cur_sem;
|
2020-07-28 13:08:48 +02:00
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
if (len < sizeof(eventfd_t) || buffer == NULL)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for an incoming event */
|
|
|
|
|
|
|
|
if (dev->counter == 0)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
eventfd_waiter_sem_t sem;
|
|
|
|
|
2020-07-28 13:08:48 +02:00
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
nxsem_init(&sem.sem, 0, 0);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = eventfd_blocking_io(dev, &sem, &dev->rdsems);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nxsem_destroy(&sem.sem);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (dev->counter == 0);
|
|
|
|
|
|
|
|
nxsem_destroy(&sem.sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Device ready for read */
|
|
|
|
|
2022-12-22 15:15:40 +01:00
|
|
|
if ((filep->f_oflags & EFD_SEMAPHORE) != 0)
|
2020-07-28 13:08:48 +02:00
|
|
|
{
|
|
|
|
*(FAR eventfd_t *)buffer = 1;
|
|
|
|
dev->counter -= 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*(FAR eventfd_t *)buffer = dev->counter;
|
|
|
|
dev->counter = 0;
|
|
|
|
}
|
|
|
|
|
2020-09-26 14:25:03 +02:00
|
|
|
#ifdef CONFIG_EVENT_FD_POLL
|
|
|
|
/* Notify all poll/select waiters */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(dev->fds, CONFIG_EVENT_FD_NPOLLWAITERS, POLLOUT);
|
2020-09-26 14:25:03 +02:00
|
|
|
#endif
|
|
|
|
|
2020-07-28 13:08:48 +02:00
|
|
|
/* Notify all waiting writers that counter have been decremented */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
cur_sem = dev->wrsems;
|
2020-07-28 13:08:48 +02:00
|
|
|
while (cur_sem != NULL)
|
|
|
|
{
|
|
|
|
nxsem_post(&cur_sem->sem);
|
|
|
|
cur_sem = cur_sem->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->wrsems = NULL;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
return sizeof(eventfd_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t eventfd_do_write(FAR struct file *filep,
|
|
|
|
FAR const char *buffer, size_t len)
|
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct eventfd_priv_s *dev = filep->f_priv;
|
2022-09-06 08:18:45 +02:00
|
|
|
FAR eventfd_waiter_sem_t *cur_sem;
|
2020-07-28 13:08:48 +02:00
|
|
|
eventfd_t new_counter;
|
2022-12-22 15:15:40 +01:00
|
|
|
ssize_t ret;
|
2020-07-28 13:08:48 +02:00
|
|
|
|
|
|
|
if (len < sizeof(eventfd_t) || buffer == NULL ||
|
|
|
|
(*(FAR eventfd_t *)buffer == (eventfd_t)-1) ||
|
|
|
|
(*(FAR eventfd_t *)buffer == (eventfd_t)0))
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_counter = dev->counter + *(FAR eventfd_t *)buffer;
|
|
|
|
|
|
|
|
if (new_counter < dev->counter)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
eventfd_waiter_sem_t sem;
|
|
|
|
|
2020-07-28 13:08:48 +02:00
|
|
|
/* Overflow detected */
|
|
|
|
|
|
|
|
if (filep->f_oflags & O_NONBLOCK)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
return -EAGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
nxsem_init(&sem.sem, 0, 0);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = eventfd_blocking_io(dev, &sem, &dev->wrsems);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
nxsem_destroy(&sem.sem);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((new_counter = dev->counter + *(FAR eventfd_t *)buffer)
|
|
|
|
< dev->counter);
|
|
|
|
|
|
|
|
nxsem_destroy(&sem.sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ready to write, update counter */
|
|
|
|
|
|
|
|
dev->counter = new_counter;
|
|
|
|
|
2020-09-26 14:25:03 +02:00
|
|
|
#ifdef CONFIG_EVENT_FD_POLL
|
|
|
|
/* Notify all poll/select waiters */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(dev->fds, CONFIG_EVENT_FD_NPOLLWAITERS, POLLIN);
|
2020-09-26 14:25:03 +02:00
|
|
|
#endif
|
|
|
|
|
2020-07-28 13:08:48 +02:00
|
|
|
/* Notify all of the waiting readers */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
cur_sem = dev->rdsems;
|
2020-07-28 13:08:48 +02:00
|
|
|
while (cur_sem != NULL)
|
|
|
|
{
|
|
|
|
nxsem_post(&cur_sem->sem);
|
|
|
|
cur_sem = cur_sem->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->rdsems = NULL;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
return sizeof(eventfd_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_EVENT_FD_POLL
|
|
|
|
static int eventfd_do_poll(FAR struct file *filep, FAR struct pollfd *fds,
|
2022-12-22 15:15:40 +01:00
|
|
|
bool setup)
|
2020-07-28 13:08:48 +02:00
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
FAR struct eventfd_priv_s *dev = filep->f_priv;
|
2020-07-28 13:08:48 +02:00
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
pollevent_t eventset;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
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-09-18 10:46:53 +02:00
|
|
|
*slot = NULL;
|
|
|
|
fds->priv = NULL;
|
2021-12-17 01:20:26 +01:00
|
|
|
goto out;
|
2020-07-28 13:08:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a request to set up the poll. Find an available
|
|
|
|
* slot for the poll structure reference
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_EVENT_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_EVENT_FD_NPOLLWAITERS)
|
|
|
|
{
|
|
|
|
fds->priv = NULL;
|
|
|
|
ret = -EBUSY;
|
2021-12-17 01:20:26 +01:00
|
|
|
goto out;
|
2020-07-28 13:08:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify the POLLOUT event if the pipe is not full, but only if
|
|
|
|
* there is readers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
eventset = 0;
|
|
|
|
if (dev->counter < (eventfd_t)-1)
|
|
|
|
{
|
|
|
|
eventset |= POLLOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify the POLLIN event if the pipe is not empty */
|
|
|
|
|
|
|
|
if (dev->counter > 0)
|
|
|
|
{
|
|
|
|
eventset |= POLLIN;
|
|
|
|
}
|
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
poll_notify(&fds, 1, eventset);
|
2020-07-28 13:08:48 +02:00
|
|
|
|
2021-12-17 01:20:26 +01:00
|
|
|
out:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int eventfd(unsigned int count, int flags)
|
|
|
|
{
|
|
|
|
FAR struct eventfd_priv_s *new_dev;
|
2022-12-22 15:15:40 +01:00
|
|
|
int new_fd;
|
|
|
|
int ret;
|
2020-07-28 13:08:48 +02:00
|
|
|
|
2022-12-22 15:15:40 +01:00
|
|
|
if ((flags & ~(EFD_NONBLOCK | EFD_SEMAPHORE | EFD_CLOEXEC)) != 0)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto exit_set_errno;
|
|
|
|
}
|
2020-07-28 13:08:48 +02:00
|
|
|
|
|
|
|
/* Allocate instance data for this driver */
|
|
|
|
|
|
|
|
new_dev = eventfd_allocdev();
|
|
|
|
if (new_dev == NULL)
|
|
|
|
{
|
|
|
|
/* Failed to allocate new device */
|
|
|
|
|
2021-12-17 01:20:26 +01:00
|
|
|
ret = -ENOMEM;
|
2020-07-28 13:08:48 +02:00
|
|
|
goto exit_set_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_dev->counter = count;
|
2022-12-22 15:15:40 +01:00
|
|
|
new_fd = file_allocate(&g_eventfd_inode, O_RDWR | flags,
|
|
|
|
0, new_dev, 0, true);
|
|
|
|
if (new_fd < 0)
|
2020-07-28 13:08:48 +02:00
|
|
|
{
|
2022-12-22 15:15:40 +01:00
|
|
|
ret = new_fd;
|
|
|
|
goto exit_with_dev;
|
2020-07-28 13:08:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Device is ready for use */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&new_dev->lock);
|
2020-07-28 13:08:48 +02:00
|
|
|
|
|
|
|
return new_fd;
|
|
|
|
|
2022-12-22 15:15:40 +01:00
|
|
|
exit_with_dev:
|
2020-07-28 13:08:48 +02:00
|
|
|
eventfd_destroy(new_dev);
|
|
|
|
exit_set_errno:
|
2021-12-17 01:20:26 +01:00
|
|
|
set_errno(-ret);
|
2020-07-28 13:08:48 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|