2015-08-10 18:38:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/vfs/fs_epoll.c
|
|
|
|
*
|
2021-09-01 13:36:03 +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
|
2015-08-10 18:38:41 +02:00
|
|
|
*
|
2021-09-01 13:36:03 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-08-10 18:38:41 +02:00
|
|
|
*
|
2021-09-01 13:36:03 +02:00
|
|
|
* 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.
|
2015-08-10 18:38:41 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-10 18:41:58 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
|
2020-11-20 10:05:35 +01:00
|
|
|
#include <inttypes.h>
|
2015-08-10 18:38:41 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2015-08-10 18:41:58 +02:00
|
|
|
#include <debug.h>
|
2015-08-10 18:38:41 +02:00
|
|
|
|
nuttx/list: rename container_of to list_container_of from public header
Use private naming to avoid conflicts with user applications
In file included from libuv/src/unix/internal.h:25,
from libuv/src/unix/udp.c:23:
libuv/src/uv-common.h:57: warning: "container_of" redefined
57 | #define container_of(ptr, type, member) \
|
In file included from nuttx/include/nuttx/list.h:47,
from nuttx/include/nuttx/tls.h:40,
from nuttx/include/nuttx/sched.h:48,
from nuttx/include/nuttx/arch.h:87,
from nuttx/include/nuttx/userspace.h:35,
from nuttx/include/nuttx/mm/mm.h:30,
from nuttx/include/nuttx/kmalloc.h:34,
from nuttx/include/nuttx/lib/lib.h:31,
from nuttx/include/stdio.h:35,
from apps/system/libuv/libuv/include/uv.h:59,
from libuv/src/unix/udp.c:22:
nuttx/include/nuttx/nuttx.h:48: note: this is the location of the previous definition
48 | #define container_of(ptr, type, member) \
|
Signed-off-by: chao an <anchao@lixiang.com>
2024-02-29 03:19:49 +01:00
|
|
|
#include <nuttx/nuttx.h>
|
2020-12-23 03:45:10 +01:00
|
|
|
#include <nuttx/clock.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
2016-07-16 15:02:06 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2022-11-13 15:46:00 +01:00
|
|
|
#include <nuttx/list.h>
|
2022-11-05 22:31:51 +01:00
|
|
|
#include <nuttx/mutex.h>
|
2022-11-13 15:46:00 +01:00
|
|
|
#include <nuttx/signal.h>
|
2016-07-16 15:02:06 +02:00
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
#include "inode/inode.h"
|
|
|
|
|
2020-08-20 19:37:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
struct epoll_node_s
|
2020-08-20 19:37:17 +02:00
|
|
|
{
|
2023-09-22 09:17:15 +02:00
|
|
|
struct list_node node;
|
|
|
|
epoll_data_t data;
|
|
|
|
bool notified;
|
|
|
|
struct pollfd pfd;
|
|
|
|
FAR struct epoll_head_s *eph;
|
2020-08-20 19:37:17 +02:00
|
|
|
};
|
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
typedef struct epoll_node_s epoll_node_t;
|
|
|
|
|
|
|
|
struct epoll_head_s
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
int crefs;
|
|
|
|
mutex_t lock;
|
|
|
|
sem_t sem;
|
|
|
|
struct list_node setup; /* The setup list, store all the setuped
|
|
|
|
* epoll node.
|
|
|
|
*/
|
|
|
|
struct list_node teardown; /* The teardown list, store all the epoll
|
|
|
|
* node notified after epoll_wait finish,
|
|
|
|
* these epoll node should be setup again
|
|
|
|
* to check the pending poll notification.
|
|
|
|
*/
|
2023-03-22 10:27:23 +01:00
|
|
|
struct list_node oneshot; /* The oneshot list, store all the epoll
|
|
|
|
* node notified after epoll_wait and with
|
|
|
|
* EPOLLONESHOT events, these oneshot epoll
|
2024-07-28 13:55:52 +02:00
|
|
|
* nodes can be reset by epoll_ctl (move
|
2023-03-22 10:27:23 +01:00
|
|
|
* from oneshot list to the setup list).
|
|
|
|
*/
|
2022-11-13 15:46:00 +01:00
|
|
|
struct list_node free; /* The free list, store all the freed epoll
|
|
|
|
* node.
|
|
|
|
*/
|
|
|
|
struct list_node extend; /* The extend list, store all the malloced
|
|
|
|
* first node, used to free the malloced
|
|
|
|
* memory in epoll_do_close().
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct epoll_head_s epoll_head_t;
|
|
|
|
|
2020-12-23 03:45:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-01-26 14:36:49 +01:00
|
|
|
static int epoll_do_open(FAR struct file *filep);
|
2021-03-12 08:52:19 +01:00
|
|
|
static int epoll_do_close(FAR struct file *filep);
|
|
|
|
static int epoll_do_poll(FAR struct file *filep,
|
|
|
|
FAR struct pollfd *fds, bool setup);
|
2022-11-13 15:46:00 +01:00
|
|
|
static int epoll_setup(FAR epoll_head_t *eph);
|
|
|
|
static int epoll_teardown(FAR epoll_head_t *eph, FAR struct epoll_event *evs,
|
|
|
|
int maxevents);
|
2020-12-23 03:45:10 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_epoll_ops =
|
|
|
|
{
|
2022-01-26 14:36:49 +01:00
|
|
|
epoll_do_open, /* open */
|
2022-01-10 15:51:17 +01:00
|
|
|
epoll_do_close, /* close */
|
|
|
|
NULL, /* read */
|
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
NULL, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2022-01-10 15:51:17 +01:00
|
|
|
epoll_do_poll /* poll */
|
2020-12-23 03:45:10 +01:00
|
|
|
};
|
|
|
|
|
2022-03-16 15:48:54 +01:00
|
|
|
static struct inode g_epoll_inode =
|
|
|
|
{
|
2022-03-26 21:51:22 +01:00
|
|
|
NULL, /* i_parent */
|
|
|
|
NULL, /* i_peer */
|
|
|
|
NULL, /* i_child */
|
2024-09-14 10:42:04 +02:00
|
|
|
1, /* i_crefs */
|
2022-03-26 21:51:22 +01:00
|
|
|
FSNODEFLAG_TYPE_DRIVER, /* i_flags */
|
|
|
|
{
|
|
|
|
&g_epoll_ops /* u */
|
|
|
|
}
|
2022-03-16 15:48:54 +01:00
|
|
|
};
|
|
|
|
|
2020-12-23 03:45:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-12-25 08:04:01 +01:00
|
|
|
static FAR epoll_head_t *epoll_head_from_fd(int fd, FAR struct file **filep)
|
2020-12-23 03:45:10 +01:00
|
|
|
{
|
2021-03-12 08:52:19 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get file pointer by file descriptor */
|
|
|
|
|
2023-12-25 08:04:01 +01:00
|
|
|
ret = fs_getfilep(fd, filep);
|
2021-03-12 08:52:19 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check fd come from us */
|
|
|
|
|
2023-12-25 08:04:01 +01:00
|
|
|
if ((*filep)->f_inode->u.i_ops != &g_epoll_ops)
|
2021-03-12 08:52:19 +01:00
|
|
|
{
|
2023-12-25 08:04:01 +01:00
|
|
|
fs_putfilep(*filep);
|
2021-03-12 08:52:19 +01:00
|
|
|
set_errno(EBADF);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-12-25 08:04:01 +01:00
|
|
|
return (*filep)->f_priv;
|
2020-12-23 03:45:10 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 14:36:49 +01:00
|
|
|
static int epoll_do_open(FAR struct file *filep)
|
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
FAR epoll_head_t *eph = filep->f_priv;
|
2022-01-26 14:36:49 +01:00
|
|
|
int ret;
|
|
|
|
|
2022-11-05 22:31:51 +01:00
|
|
|
ret = nxmutex_lock(&eph->lock);
|
2022-01-26 14:36:49 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
eph->crefs++;
|
2022-11-05 22:31:51 +01:00
|
|
|
nxmutex_unlock(&eph->lock);
|
2022-01-26 14:36:49 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
static int epoll_do_close(FAR struct file *filep)
|
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
FAR epoll_head_t *eph = filep->f_priv;
|
|
|
|
FAR epoll_node_t *epn;
|
2022-11-25 16:33:22 +01:00
|
|
|
FAR epoll_node_t *tmp;
|
2022-01-26 14:36:49 +01:00
|
|
|
int ret;
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2022-11-05 22:31:51 +01:00
|
|
|
ret = nxmutex_lock(&eph->lock);
|
2022-01-26 14:36:49 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
eph->crefs--;
|
2022-11-05 22:31:51 +01:00
|
|
|
nxmutex_unlock(&eph->lock);
|
2022-01-26 14:36:49 +01:00
|
|
|
if (eph->crefs <= 0)
|
|
|
|
{
|
2022-11-05 22:31:51 +01:00
|
|
|
nxmutex_destroy(&eph->lock);
|
2022-11-13 15:46:00 +01:00
|
|
|
list_for_every_entry(&eph->setup, epn, epoll_node_t, node)
|
|
|
|
{
|
|
|
|
poll_fdsetup(epn->pfd.fd, &epn->pfd, false);
|
|
|
|
}
|
|
|
|
|
2022-11-25 16:33:22 +01:00
|
|
|
list_for_every_entry_safe(&eph->extend, epn, tmp, epoll_node_t, node)
|
2022-11-13 15:46:00 +01:00
|
|
|
{
|
2022-12-13 16:02:39 +01:00
|
|
|
list_delete(&epn->node);
|
2022-11-13 15:46:00 +01:00
|
|
|
kmm_free(epn);
|
|
|
|
}
|
|
|
|
|
2022-01-26 14:36:49 +01:00
|
|
|
kmm_free(eph);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2021-03-12 08:52:19 +01:00
|
|
|
}
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
static int epoll_do_poll(FAR struct file *filep,
|
|
|
|
FAR struct pollfd *fds, bool setup)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int epoll_do_create(int size, int flags)
|
2015-08-10 18:38:41 +02:00
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
FAR epoll_head_t *eph;
|
|
|
|
FAR epoll_node_t *epn;
|
2021-03-12 08:52:19 +01:00
|
|
|
int fd;
|
2022-11-13 15:46:00 +01:00
|
|
|
int i;
|
2020-12-23 03:45:10 +01:00
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
size = size <= 0 ? 1 : size;
|
|
|
|
eph = kmm_zalloc(sizeof(epoll_head_t) + sizeof(epoll_node_t) * size);
|
2021-03-12 08:52:19 +01:00
|
|
|
if (eph == NULL)
|
|
|
|
{
|
|
|
|
set_errno(ENOMEM);
|
2022-11-05 22:31:51 +01:00
|
|
|
return ERROR;
|
2021-03-12 08:52:19 +01:00
|
|
|
}
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
eph->size = size;
|
2022-11-05 22:31:51 +01:00
|
|
|
nxmutex_init(&eph->lock);
|
2022-11-13 15:46:00 +01:00
|
|
|
nxsem_init(&eph->sem, 0, 0);
|
2022-11-05 22:31:51 +01:00
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
/* List initialize */
|
|
|
|
|
|
|
|
epn = (FAR epoll_node_t *)(eph + 1);
|
|
|
|
|
|
|
|
list_initialize(&eph->setup);
|
|
|
|
list_initialize(&eph->teardown);
|
2023-03-22 10:27:23 +01:00
|
|
|
list_initialize(&eph->oneshot);
|
2022-11-13 15:46:00 +01:00
|
|
|
list_initialize(&eph->extend);
|
|
|
|
list_initialize(&eph->free);
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
|
|
|
list_add_tail(&eph->free, &epn[i].node);
|
|
|
|
}
|
2020-12-23 03:45:10 +01:00
|
|
|
|
2023-07-12 12:15:00 +02:00
|
|
|
eph->crefs++;
|
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
/* Alloc the file descriptor */
|
|
|
|
|
2022-10-26 12:15:16 +02:00
|
|
|
fd = file_allocate(&g_epoll_inode, flags, 0, eph, 0, true);
|
2021-03-12 08:52:19 +01:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
2022-11-05 22:31:51 +01:00
|
|
|
nxmutex_destroy(&eph->lock);
|
2021-03-12 08:52:19 +01:00
|
|
|
kmm_free(eph);
|
|
|
|
set_errno(-fd);
|
2022-11-05 22:31:51 +01:00
|
|
|
return ERROR;
|
2021-03-12 08:52:19 +01:00
|
|
|
}
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
return fd;
|
|
|
|
}
|
2022-11-05 22:16:03 +01:00
|
|
|
|
2023-09-22 09:17:15 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup all the fd.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* eph - The epoll head pointer
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Positive on success, negative on fail
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
static int epoll_setup(FAR epoll_head_t *eph)
|
|
|
|
{
|
|
|
|
FAR epoll_node_t *tepn;
|
|
|
|
FAR epoll_node_t *epn;
|
|
|
|
int ret;
|
2022-11-05 22:16:03 +01:00
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
ret = nxmutex_lock(&eph->lock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_every_entry_safe(&eph->teardown, epn, tepn, epoll_node_t, node)
|
|
|
|
{
|
|
|
|
/* Setup again to check the notified pollfd last epoll_wait() to
|
|
|
|
* cover the situation several poll event pending on one fd.
|
|
|
|
*/
|
|
|
|
|
2023-09-22 09:17:15 +02:00
|
|
|
epn->notified = false;
|
2022-11-13 15:46:00 +01:00
|
|
|
epn->pfd.revents = 0;
|
|
|
|
ret = poll_fdsetup(epn->pfd.fd, &epn->pfd, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("epoll setup failed, fd=%d, events=%08" PRIx32 ", ret=%d\n",
|
|
|
|
epn->pfd.fd, epn->pfd.events, ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_delete(&epn->node);
|
|
|
|
list_add_tail(&eph->setup, &epn->node);
|
|
|
|
}
|
|
|
|
|
|
|
|
nxmutex_unlock(&eph->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-09-22 09:17:15 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_teardown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Teardown all the notifed fd and check the notified fd's event with user
|
|
|
|
* expected event.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* eph - The epoll head pointer
|
|
|
|
* evs - The epoll events array
|
|
|
|
* maxevents - The epoll events array size
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Return the number of fd that notifed and the events is also user
|
|
|
|
* expected.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
static int epoll_teardown(FAR epoll_head_t *eph, FAR struct epoll_event *evs,
|
|
|
|
int maxevents)
|
|
|
|
{
|
|
|
|
FAR epoll_node_t *tepn;
|
|
|
|
FAR epoll_node_t *epn;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
nxmutex_lock(&eph->lock);
|
|
|
|
|
|
|
|
list_for_every_entry_safe(&eph->setup, epn, tepn, epoll_node_t, node)
|
|
|
|
{
|
2023-09-22 09:17:15 +02:00
|
|
|
/* Only check the notifed fd */
|
|
|
|
|
|
|
|
if (!epn->notified)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Teradown all the notified fd */
|
|
|
|
|
|
|
|
poll_fdsetup(epn->pfd.fd, &epn->pfd, false);
|
|
|
|
list_delete(&epn->node);
|
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
if (epn->pfd.revents != 0 && i < maxevents)
|
|
|
|
{
|
|
|
|
evs[i].data = epn->data;
|
|
|
|
evs[i++].events = epn->pfd.revents;
|
|
|
|
if ((epn->pfd.events & EPOLLONESHOT) != 0)
|
|
|
|
{
|
2023-03-22 10:27:23 +01:00
|
|
|
list_add_tail(&eph->oneshot, &epn->node);
|
2022-11-13 15:46:00 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
list_add_tail(&eph->teardown, &epn->node);
|
|
|
|
}
|
|
|
|
}
|
2023-09-22 09:17:15 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
list_add_tail(&eph->teardown, &epn->node);
|
|
|
|
}
|
2022-11-13 15:46:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
nxmutex_unlock(&eph->lock);
|
|
|
|
return i;
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 09:17:15 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_default_cb
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The default epoll callback function, this function do the final step of
|
|
|
|
* poll notification.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fds - The fds
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void epoll_default_cb(FAR struct pollfd *fds)
|
|
|
|
{
|
|
|
|
FAR epoll_node_t *epn = fds->arg;
|
|
|
|
int semcount = 0;
|
|
|
|
|
|
|
|
epn->notified = true;
|
|
|
|
if (fds->revents != 0)
|
|
|
|
{
|
|
|
|
nxsem_get_value(&epn->eph->sem, &semcount);
|
|
|
|
if (semcount < 1)
|
|
|
|
{
|
|
|
|
nxsem_post(&epn->eph->sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 04:58:27 +02:00
|
|
|
/****************************************************************************
|
2021-03-12 08:52:19 +01:00
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_create
|
2020-08-17 04:58:27 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
int epoll_create(int size)
|
2020-08-17 04:58:27 +02:00
|
|
|
{
|
2021-03-12 08:52:19 +01:00
|
|
|
return epoll_do_create(size, 0);
|
|
|
|
}
|
2020-08-17 04:58:27 +02:00
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_create1
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2020-08-17 04:58:27 +02:00
|
|
|
|
2021-03-12 08:52:19 +01:00
|
|
|
int epoll_create1(int flags)
|
|
|
|
{
|
|
|
|
return epoll_do_create(CONFIG_FS_NEPOLL_DESCRIPTORS, flags);
|
2020-08-17 04:58:27 +02:00
|
|
|
}
|
|
|
|
|
2015-08-10 18:38:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void epoll_close(int epfd)
|
|
|
|
{
|
2021-03-12 08:52:19 +01:00
|
|
|
close(epfd);
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_ctl
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-11-05 22:31:51 +01:00
|
|
|
int epoll_ctl(int epfd, int op, int fd, FAR struct epoll_event *ev)
|
2015-08-10 18:38:41 +02:00
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
FAR struct list_node *extend;
|
2023-12-25 08:04:01 +01:00
|
|
|
FAR struct file *filep;
|
2022-11-13 15:46:00 +01:00
|
|
|
FAR epoll_head_t *eph;
|
|
|
|
FAR epoll_node_t *epn;
|
2022-11-05 22:31:51 +01:00
|
|
|
int ret;
|
2020-12-23 03:41:13 +01:00
|
|
|
int i;
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2023-12-25 08:04:01 +01:00
|
|
|
eph = epoll_head_from_fd(epfd, &filep);
|
2021-03-12 08:52:19 +01:00
|
|
|
if (eph == NULL)
|
|
|
|
{
|
2022-11-05 22:31:51 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = nxmutex_lock(&eph->lock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto err_without_lock;
|
2021-03-12 08:52:19 +01:00
|
|
|
}
|
|
|
|
|
2015-08-10 18:38:41 +02:00
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case EPOLL_CTL_ADD:
|
2022-11-13 15:46:00 +01:00
|
|
|
finfo("%p CTL ADD: fd=%d ev=%08" PRIx32 "\n", eph, fd, ev->events);
|
|
|
|
|
|
|
|
/* Check repetition */
|
|
|
|
|
|
|
|
list_for_every_entry(&eph->setup, epn, epoll_node_t, node)
|
2020-12-23 03:41:13 +01:00
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
if (epn->pfd.fd == fd)
|
|
|
|
{
|
|
|
|
ret = -EEXIST;
|
|
|
|
goto err;
|
|
|
|
}
|
2020-12-23 03:41:13 +01:00
|
|
|
}
|
|
|
|
|
2022-11-25 11:56:53 +01:00
|
|
|
list_for_every_entry(&eph->teardown, epn, epoll_node_t, node)
|
|
|
|
{
|
|
|
|
if (epn->pfd.fd == fd)
|
|
|
|
{
|
|
|
|
ret = -EEXIST;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 10:27:23 +01:00
|
|
|
list_for_every_entry(&eph->oneshot, epn, epoll_node_t, node)
|
|
|
|
{
|
|
|
|
if (epn->pfd.fd == fd)
|
|
|
|
{
|
|
|
|
ret = -EEXIST;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
if (list_is_empty(&eph->free))
|
2020-12-23 03:41:13 +01:00
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
/* Malloc new epoll node, insert the first list_node to the
|
|
|
|
* extend list and insert the remaining epoll nodes to the free
|
|
|
|
* list.
|
|
|
|
*/
|
|
|
|
|
|
|
|
extend = kmm_zalloc(sizeof(*extend) +
|
2024-07-28 14:27:28 +02:00
|
|
|
2 * sizeof(epoll_node_t) * eph->size);
|
2022-11-13 15:46:00 +01:00
|
|
|
if (extend == NULL)
|
2020-12-23 03:41:13 +01:00
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
ret = -ENOMEM;
|
2022-11-05 22:31:51 +01:00
|
|
|
goto err;
|
2020-12-23 03:41:13 +01:00
|
|
|
}
|
2022-11-13 15:46:00 +01:00
|
|
|
|
2024-07-28 14:27:28 +02:00
|
|
|
eph->size *= 2;
|
2022-11-13 15:46:00 +01:00
|
|
|
list_add_tail(&eph->extend, extend);
|
|
|
|
epn = (FAR epoll_node_t *)(extend + 1);
|
|
|
|
for (i = 0; i < eph->size; i++)
|
|
|
|
{
|
|
|
|
list_add_tail(&eph->free, &epn[i].node);
|
|
|
|
}
|
2020-12-23 03:41:13 +01:00
|
|
|
}
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2023-02-08 09:56:16 +01:00
|
|
|
epn = container_of(list_remove_head(&eph->free), epoll_node_t, node);
|
2023-09-22 09:17:15 +02:00
|
|
|
epn->eph = eph;
|
2022-11-13 15:46:00 +01:00
|
|
|
epn->data = ev->data;
|
2023-09-22 09:17:15 +02:00
|
|
|
epn->notified = false;
|
|
|
|
epn->pfd.events = ev->events | POLLALWAYS;
|
2022-11-13 15:46:00 +01:00
|
|
|
epn->pfd.fd = fd;
|
2023-09-22 09:17:15 +02:00
|
|
|
epn->pfd.arg = epn;
|
|
|
|
epn->pfd.cb = epoll_default_cb;
|
2022-11-13 15:46:00 +01:00
|
|
|
epn->pfd.revents = 0;
|
|
|
|
|
|
|
|
ret = poll_fdsetup(fd, &epn->pfd, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
list_add_tail(&eph->free, &epn->node);
|
|
|
|
goto err;
|
|
|
|
}
|
2020-12-23 03:41:13 +01:00
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
list_add_tail(&eph->setup, &epn->node);
|
2020-12-23 03:41:13 +01:00
|
|
|
break;
|
2015-08-10 18:38:41 +02:00
|
|
|
|
|
|
|
case EPOLL_CTL_DEL:
|
2022-11-13 15:46:00 +01:00
|
|
|
finfo("%p CTL DEL: fd=%d\n", eph, fd);
|
|
|
|
list_for_every_entry(&eph->setup, epn, epoll_node_t, node)
|
2020-12-23 03:41:13 +01:00
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
if (epn->pfd.fd == fd)
|
2020-12-23 03:41:13 +01:00
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
poll_fdsetup(fd, &epn->pfd, false);
|
|
|
|
list_delete(&epn->node);
|
|
|
|
list_add_tail(&eph->free, &epn->node);
|
|
|
|
goto out;
|
2020-12-23 03:41:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
list_for_every_entry(&eph->teardown, epn, epoll_node_t, node)
|
2021-03-12 06:24:21 +01:00
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
if (epn->pfd.fd == fd)
|
|
|
|
{
|
|
|
|
list_delete(&epn->node);
|
|
|
|
list_add_tail(&eph->free, &epn->node);
|
|
|
|
goto out;
|
|
|
|
}
|
2021-03-12 06:24:21 +01:00
|
|
|
}
|
|
|
|
|
2023-03-22 10:27:23 +01:00
|
|
|
list_for_every_entry(&eph->oneshot, epn, epoll_node_t, node)
|
|
|
|
{
|
|
|
|
if (epn->pfd.fd == fd)
|
|
|
|
{
|
|
|
|
list_delete(&epn->node);
|
|
|
|
list_add_tail(&eph->free, &epn->node);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 06:24:21 +01:00
|
|
|
break;
|
2015-08-10 18:38:41 +02:00
|
|
|
|
|
|
|
case EPOLL_CTL_MOD:
|
2022-11-13 15:46:00 +01:00
|
|
|
finfo("%p CTL MOD: fd=%d ev=%08" PRIx32 "\n", eph, fd, ev->events);
|
|
|
|
list_for_every_entry(&eph->setup, epn, epoll_node_t, node)
|
2020-12-23 03:41:13 +01:00
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
if (epn->pfd.fd == fd)
|
2020-12-23 03:41:13 +01:00
|
|
|
{
|
2023-09-22 09:17:15 +02:00
|
|
|
if (epn->pfd.events != (ev->events | POLLALWAYS))
|
2022-11-13 15:46:00 +01:00
|
|
|
{
|
|
|
|
poll_fdsetup(fd, &epn->pfd, false);
|
|
|
|
|
2023-09-22 09:17:15 +02:00
|
|
|
epn->notified = false;
|
2022-11-13 15:46:00 +01:00
|
|
|
epn->data = ev->data;
|
2023-09-22 09:17:15 +02:00
|
|
|
epn->pfd.events = ev->events | POLLALWAYS;
|
2022-11-13 15:46:00 +01:00
|
|
|
epn->pfd.fd = fd;
|
|
|
|
epn->pfd.revents = 0;
|
|
|
|
|
|
|
|
ret = poll_fdsetup(fd, &epn->pfd, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-25 11:56:53 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_every_entry(&eph->teardown, epn, epoll_node_t, node)
|
|
|
|
{
|
|
|
|
if (epn->pfd.fd == fd)
|
|
|
|
{
|
2023-09-22 09:17:15 +02:00
|
|
|
if (epn->pfd.events != (ev->events | POLLALWAYS))
|
2022-11-25 11:56:53 +01:00
|
|
|
{
|
2023-09-22 09:17:15 +02:00
|
|
|
epn->notified = false;
|
2022-11-25 11:56:53 +01:00
|
|
|
epn->data = ev->data;
|
2023-09-22 09:17:15 +02:00
|
|
|
epn->pfd.events = ev->events | POLLALWAYS;
|
2022-11-25 11:56:53 +01:00
|
|
|
epn->pfd.fd = fd;
|
|
|
|
epn->pfd.revents = 0;
|
|
|
|
|
|
|
|
ret = poll_fdsetup(fd, &epn->pfd, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_delete(&epn->node);
|
|
|
|
list_add_tail(&eph->setup, &epn->node);
|
|
|
|
}
|
|
|
|
|
2023-03-22 10:27:23 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_every_entry(&eph->oneshot, epn, epoll_node_t, node)
|
|
|
|
{
|
|
|
|
if (epn->pfd.fd == fd)
|
|
|
|
{
|
2023-09-22 09:17:15 +02:00
|
|
|
epn->notified = false;
|
2023-03-22 10:27:23 +01:00
|
|
|
epn->data = ev->data;
|
2023-09-22 09:17:15 +02:00
|
|
|
epn->pfd.events = ev->events | POLLALWAYS;
|
2023-03-22 10:27:23 +01:00
|
|
|
epn->pfd.fd = fd;
|
|
|
|
epn->pfd.revents = 0;
|
|
|
|
|
|
|
|
ret = poll_fdsetup(fd, &epn->pfd, true);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_delete(&epn->node);
|
|
|
|
list_add_tail(&eph->setup, &epn->node);
|
2020-12-23 03:41:13 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-12 06:24:21 +01:00
|
|
|
break;
|
2020-12-23 03:41:13 +01:00
|
|
|
|
|
|
|
default:
|
2022-11-05 22:31:51 +01:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto err;
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
out:
|
2022-11-11 07:53:38 +01:00
|
|
|
nxmutex_unlock(&eph->lock);
|
2023-12-25 08:04:01 +01:00
|
|
|
fs_putfilep(filep);
|
2022-11-05 22:31:51 +01:00
|
|
|
return OK;
|
|
|
|
err:
|
|
|
|
nxmutex_unlock(&eph->lock);
|
|
|
|
err_without_lock:
|
2023-12-25 08:04:01 +01:00
|
|
|
fs_putfilep(filep);
|
2022-11-05 22:31:51 +01:00
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-08-16 19:00:36 +02:00
|
|
|
* Name: epoll_pwait
|
2015-08-10 18:38:41 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-08-16 19:00:36 +02:00
|
|
|
int epoll_pwait(int epfd, FAR struct epoll_event *evs,
|
|
|
|
int maxevents, int timeout, FAR const sigset_t *sigmask)
|
2015-08-10 18:38:41 +02:00
|
|
|
{
|
2023-12-25 08:04:01 +01:00
|
|
|
FAR struct file *filep;
|
2022-11-13 15:46:00 +01:00
|
|
|
FAR epoll_head_t *eph;
|
|
|
|
sigset_t oldsigmask;
|
|
|
|
int ret;
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2023-12-25 08:04:01 +01:00
|
|
|
eph = epoll_head_from_fd(epfd, &filep);
|
2021-03-12 08:52:19 +01:00
|
|
|
if (eph == NULL)
|
|
|
|
{
|
2022-11-05 22:31:51 +01:00
|
|
|
return ERROR;
|
2021-03-12 08:52:19 +01:00
|
|
|
}
|
|
|
|
|
2023-09-22 09:17:15 +02:00
|
|
|
retry:
|
2022-11-13 15:46:00 +01:00
|
|
|
ret = epoll_setup(eph);
|
|
|
|
if (ret < 0)
|
2020-12-23 03:45:10 +01:00
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
goto err;
|
2020-12-23 03:45:10 +01:00
|
|
|
}
|
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
/* Wait the poll ready */
|
2020-12-23 03:45:10 +01:00
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
nxsig_procmask(SIG_SETMASK, sigmask, &oldsigmask);
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
if (timeout == 0)
|
2015-08-10 18:38:41 +02:00
|
|
|
{
|
2023-09-22 09:17:15 +02:00
|
|
|
ret = -ETIMEDOUT;
|
2020-12-23 03:45:10 +01:00
|
|
|
}
|
2022-11-13 15:46:00 +01:00
|
|
|
else if (timeout > 0)
|
2020-12-23 03:45:10 +01:00
|
|
|
{
|
2024-07-28 13:55:52 +02:00
|
|
|
ret = nxsem_tickwait(&eph->sem, MSEC2TICK(timeout));
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
2022-11-13 15:46:00 +01:00
|
|
|
else
|
2020-08-15 22:05:24 +02:00
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
ret = nxsem_wait(&eph->sem);
|
2020-08-15 22:05:24 +02:00
|
|
|
}
|
|
|
|
|
2022-11-13 15:46:00 +01:00
|
|
|
nxsig_procmask(SIG_SETMASK, &oldsigmask, NULL);
|
2023-09-22 09:17:15 +02:00
|
|
|
if (ret < 0 && ret != -ETIMEDOUT)
|
2015-08-10 18:38:41 +02:00
|
|
|
{
|
2022-11-13 15:46:00 +01:00
|
|
|
goto err;
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
2023-09-22 09:17:15 +02:00
|
|
|
else /* ret >= 0 or ret == -ETIMEDOUT */
|
|
|
|
{
|
|
|
|
int num = epoll_teardown(eph, evs, maxevents);
|
|
|
|
if (num == 0 && ret >= 0)
|
|
|
|
{
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = num;
|
|
|
|
}
|
2015-08-10 18:38:41 +02:00
|
|
|
|
2023-12-25 08:04:01 +01:00
|
|
|
fs_putfilep(filep);
|
2023-09-22 09:17:15 +02:00
|
|
|
return ret;
|
2022-11-13 15:46:00 +01:00
|
|
|
|
|
|
|
err:
|
2023-12-25 08:04:01 +01:00
|
|
|
fs_putfilep(filep);
|
2022-11-13 15:46:00 +01:00
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
2015-08-10 18:38:41 +02:00
|
|
|
}
|
2020-08-16 19:00:36 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: epoll_wait
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int epoll_wait(int epfd, FAR struct epoll_event *evs,
|
|
|
|
int maxevents, int timeout)
|
|
|
|
{
|
2023-12-25 08:04:01 +01:00
|
|
|
FAR struct file *filep;
|
2022-11-13 15:46:00 +01:00
|
|
|
FAR epoll_head_t *eph;
|
|
|
|
int ret;
|
|
|
|
|
2023-12-25 08:04:01 +01:00
|
|
|
eph = epoll_head_from_fd(epfd, &filep);
|
2022-11-13 15:46:00 +01:00
|
|
|
if (eph == NULL)
|
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2023-09-22 09:17:15 +02:00
|
|
|
retry:
|
2022-11-13 15:46:00 +01:00
|
|
|
ret = epoll_setup(eph);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait the poll ready */
|
|
|
|
|
|
|
|
if (timeout == 0)
|
|
|
|
{
|
2023-09-22 09:17:15 +02:00
|
|
|
ret = -ETIMEDOUT;
|
2022-11-13 15:46:00 +01:00
|
|
|
}
|
|
|
|
else if (timeout > 0)
|
|
|
|
{
|
2024-07-28 13:55:52 +02:00
|
|
|
ret = nxsem_tickwait(&eph->sem, MSEC2TICK(timeout));
|
2022-11-13 15:46:00 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = nxsem_wait(&eph->sem);
|
|
|
|
}
|
|
|
|
|
2023-09-22 09:17:15 +02:00
|
|
|
if (ret < 0 && ret != -ETIMEDOUT)
|
2022-11-13 15:46:00 +01:00
|
|
|
{
|
|
|
|
goto err;
|
|
|
|
}
|
2023-09-22 09:17:15 +02:00
|
|
|
else /* ret >= 0 or ret == -ETIMEDOUT */
|
|
|
|
{
|
|
|
|
int num = epoll_teardown(eph, evs, maxevents);
|
|
|
|
if (num == 0 && ret >= 0)
|
|
|
|
{
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = num;
|
|
|
|
}
|
2022-11-13 15:46:00 +01:00
|
|
|
|
2023-12-25 08:04:01 +01:00
|
|
|
fs_putfilep(filep);
|
2023-09-22 09:17:15 +02:00
|
|
|
return ret;
|
2022-11-13 15:46:00 +01:00
|
|
|
|
|
|
|
err:
|
2023-12-25 08:04:01 +01:00
|
|
|
fs_putfilep(filep);
|
2022-11-13 15:46:00 +01:00
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
2020-08-16 19:00:36 +02:00
|
|
|
}
|