4
0
mirror of https://github.com/RT-Thread/rt-thread.git synced 2025-02-22 01:15:26 +08:00

Add fops for dfs_v2 and rt_set_errno (#7910)

This commit is contained in:
zmq810150896 2023-08-03 21:45:30 +08:00 committed by GitHub
parent 325c3d2a48
commit 2aa1056f19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 36 deletions

View File

@ -164,7 +164,7 @@ static int epoll_poll(struct dfs_file *file, struct rt_pollreq *req)
fdlist = fdlist->next; fdlist = fdlist->next;
mask = epoll_get_event(fdlist, &fdlist->req); mask = epoll_get_event(fdlist, &fdlist->req);
if (mask & fdlist->epev.events) if (mask & fdlist->revents)
{ {
events |= mask | POLLIN | EPOLLRDNORM; events |= mask | POLLIN | EPOLLRDNORM;
break; break;
@ -221,7 +221,7 @@ static int epoll_rdlist_add(struct rt_fd_list *fdl, rt_uint32_t revents)
if (rdlist != RT_NULL) if (rdlist != RT_NULL)
{ {
rdlist->rdl_event = fdl; rdlist->rdl_event = fdl;
rdlist->rdl_event->epev.events = fdl->epev.events & revents; rdlist->rdl_event->epev.events = fdl->revents & revents;
rdlist->next = ep->rdlist->next; rdlist->next = ep->rdlist->next;
rdlist->exclusive = 0; rdlist->exclusive = 0;
ep->rdlist->next = rdlist; ep->rdlist->next = rdlist;
@ -250,7 +250,7 @@ static int epoll_wqueue_callback(struct rt_wqueue_node *wait, void *key)
fdlist = rt_container_of(wait, struct rt_fd_list, wqn); fdlist = rt_container_of(wait, struct rt_fd_list, wqn);
if (fdlist->epev.events) if (fdlist->revents)
{ {
epoll_rdlist_add(fdlist, (rt_ubase_t)key); epoll_rdlist_add(fdlist, (rt_ubase_t)key);
} }
@ -279,10 +279,10 @@ static void epoll_ctl_install(struct rt_fd_list *fdlist, struct rt_eventpoll *ep
{ {
rt_uint32_t mask = 0; rt_uint32_t mask = 0;
fdlist->req._key = fdlist->epev.events; fdlist->req._key = fdlist->revents;
mask = epoll_get_event(fdlist, &fdlist->req); mask = epoll_get_event(fdlist, &fdlist->req);
if (mask & fdlist->epev.events) if (mask & fdlist->revents)
{ {
epoll_rdlist_add(fdlist, mask); epoll_rdlist_add(fdlist, mask);
} }
@ -317,6 +317,10 @@ static int epoll_epf_init(int fd)
rt_mutex_init(&ep->lock, EPOLL_MUTEX_NAME, RT_IPC_FLAG_FIFO); rt_mutex_init(&ep->lock, EPOLL_MUTEX_NAME, RT_IPC_FLAG_FIFO);
#ifdef RT_USING_DFS_V2
df->fops = &epoll_fops;
#endif
df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode)); df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
if (df->vnode) if (df->vnode)
{ {
@ -353,13 +357,13 @@ static int epoll_epf_init(int fd)
static int epoll_do_create(int size) static int epoll_do_create(int size)
{ {
rt_err_t ret = 0; rt_err_t ret = -1;
int status; int status;
int fd; int fd;
if (size < 0) if (size < 0)
{ {
ret = -EINVAL; rt_set_errno(EINVAL);
} }
else else
{ {
@ -371,12 +375,12 @@ static int epoll_do_create(int size)
if (status < 0) if (status < 0)
{ {
fd_release(fd); fd_release(fd);
ret = status; rt_set_errno(-status);
} }
} }
else else
{ {
ret = fd; rt_set_errno(-fd);
} }
} }
@ -518,23 +522,31 @@ static int epoll_do_ctl(int epfd, int op, int fd, struct epoll_event *event)
{ {
struct dfs_file *epdf; struct dfs_file *epdf;
struct rt_eventpoll *ep; struct rt_eventpoll *ep;
rt_err_t ret = -EINVAL; rt_err_t ret = 0;
if (op & ~EFD_SHARED_EPOLL_TYPE) if (op & ~EFD_SHARED_EPOLL_TYPE)
return -EINVAL; {
rt_set_errno(EINVAL);
return -1;
}
if ((epfd == fd) || (epfd < 0) || (fd < 0) || (event->data.fd != fd)) if ((epfd == fd) || (epfd < 0) || (fd < 0) || (event->data.fd != fd))
return -EINVAL; {
rt_set_errno(EINVAL);
return -1;
}
if (!(event->events & EPOLLEXCLUSIVE_BITS)) if (!(event->events & EPOLLEXCLUSIVE_BITS))
return -EINVAL; {
rt_set_errno(EINVAL);
return -1;
}
epdf = fd_get(epfd); epdf = fd_get(epfd);
if (epdf->vnode->data) if (epdf->vnode->data)
{ {
ep = epdf->vnode->data; ep = epdf->vnode->data;
ret = 0;
rt_mutex_take(&ep->lock, RT_WAITING_FOREVER); rt_mutex_take(&ep->lock, RT_WAITING_FOREVER);
@ -550,12 +562,18 @@ static int epoll_do_ctl(int epfd, int op, int fd, struct epoll_event *event)
ret = epoll_ctl_mod(epdf, event); ret = epoll_ctl_mod(epdf, event);
break; break;
default: default:
ret = -EINVAL; rt_set_errno(EINVAL);
break; break;
} }
}
rt_mutex_release(&ep->lock); if (ret < 0)
{
rt_set_errno(-ret);
ret = -1;
}
rt_mutex_release(&ep->lock);
}
return ret; return ret;
} }
@ -613,13 +631,13 @@ static int epoll_get_event(struct rt_fd_list *fl, rt_pollreq_t *req)
{ {
if (df->vnode->fops->poll) if (df->vnode->fops->poll)
{ {
req->_key = fl->epev.events | POLLERR | POLLHUP; req->_key = fl->revents | POLLERR | POLLHUP;
mask = df->vnode->fops->poll(df, req); mask = df->vnode->fops->poll(df, req);
if (mask < 0) if (mask < 0)
return mask; return mask;
} }
mask &= fl->epev.events | EPOLLOUT | POLLERR; mask &= fl->revents | EPOLLOUT | POLLERR;
} }
} }
@ -659,7 +677,8 @@ static int epoll_do(struct rt_eventpoll *ep, struct epoll_event *events, int max
if (rdlist->rdl_event->revents & EPOLLET) if (rdlist->rdl_event->revents & EPOLLET)
{ {
rt_wqueue_remove(&rdlist->rdl_event->wqn); rt_wqueue_remove(&rdlist->rdl_event->wqn);
epoll_get_event(rdlist->rdl_event, &rdlist->rdl_event->req); mask = epoll_get_event(rdlist->rdl_event, &rdlist->rdl_event->req);
rdlist->rdl_event->epev.events = mask & rdlist->rdl_event->revents;
isfree = 1; isfree = 1;
} }
else else
@ -729,7 +748,7 @@ static int epoll_do_wait(int epfd, struct epoll_event *events, int maxevents, in
struct rt_eventpoll *ep; struct rt_eventpoll *ep;
struct dfs_file *df; struct dfs_file *df;
lwp_sigset_t old_sig, new_sig; lwp_sigset_t old_sig, new_sig;
rt_err_t ret = -EINVAL; rt_err_t ret = 0;
if (ss) if (ss)
{ {
@ -755,6 +774,12 @@ static int epoll_do_wait(int epfd, struct epoll_event *events, int maxevents, in
lwp_thread_signal_mask(rt_thread_self(), LWP_SIG_MASK_CMD_SET_MASK, &old_sig, RT_NULL); lwp_thread_signal_mask(rt_thread_self(), LWP_SIG_MASK_CMD_SET_MASK, &old_sig, RT_NULL);
} }
if (ret < 0)
{
rt_set_errno(-ret);
ret = -1;
}
return ret; return ret;
} }

View File

@ -40,13 +40,13 @@ struct eventfd_ctx
#ifndef RT_USING_DFS_V2 #ifndef RT_USING_DFS_V2
static int eventfd_close(struct dfs_file *file); static int eventfd_close(struct dfs_file *file);
static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req); static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req);
static int eventfd_read(struct dfs_file *file, void *buf, size_t count); static ssize_t eventfd_read(struct dfs_file *file, void *buf, size_t count);
static int eventfd_write(struct dfs_file *file, const void *buf, size_t count); static ssize_t eventfd_write(struct dfs_file *file, const void *buf, size_t count);
#else #else
static int eventfd_close(struct dfs_file *file); static int eventfd_close(struct dfs_file *file);
static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req); static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req);
static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos); static ssize_t eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos);
static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos); static ssize_t eventfd_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos);
#endif #endif
static const struct dfs_file_ops eventfd_fops = static const struct dfs_file_ops eventfd_fops =
@ -76,7 +76,6 @@ static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req)
count = ctx->count; count = ctx->count;
rt_poll_add(&ctx->reader_queue, req); rt_poll_add(&ctx->reader_queue, req);
rt_poll_add(&ctx->writer_queue, req);
if (count > 0) if (count > 0)
events |= POLLIN; events |= POLLIN;
@ -91,9 +90,9 @@ static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req)
} }
#ifndef RT_USING_DFS_V2 #ifndef RT_USING_DFS_V2
static int eventfd_read(struct dfs_file *file, void *buf, size_t count) static ssize_t eventfd_read(struct dfs_file *file, void *buf, size_t count)
#else #else
static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos) static ssize_t eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
#endif #endif
{ {
struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data;
@ -107,7 +106,7 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p
rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER);
if (ctx->count == 0) if (ctx->count <= 0)
{ {
if (file->flags & O_NONBLOCK) if (file->flags & O_NONBLOCK)
{ {
@ -135,7 +134,6 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p
} }
ctx->count -= counter_num; ctx->count -= counter_num;
(*buffer) = counter_num; (*buffer) = counter_num;
rt_mutex_release(&ctx->lock); rt_mutex_release(&ctx->lock);
@ -144,9 +142,9 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p
} }
#ifndef RT_USING_DFS_V2 #ifndef RT_USING_DFS_V2
static int eventfd_write(struct dfs_file *file, const void *buf, size_t count) static ssize_t eventfd_write(struct dfs_file *file, const void *buf, size_t count)
#else #else
static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos) static ssize_t eventfd_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
#endif #endif
{ {
struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data;
@ -182,7 +180,6 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o
/* Release the mutex to avoid a deadlock */ /* Release the mutex to avoid a deadlock */
rt_mutex_release(&ctx->lock); rt_mutex_release(&ctx->lock);
rt_wqueue_wait(&ctx->writer_queue, 0, RT_WAITING_FOREVER); rt_wqueue_wait(&ctx->writer_queue, 0, RT_WAITING_FOREVER);
rt_wqueue_wakeup(&ctx->reader_queue, (void *)POLLIN);
rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER);
} }
} }
@ -232,6 +229,11 @@ static int rt_eventfd_create(struct dfs_file *df, unsigned int count, int flags)
rt_free(ctx); rt_free(ctx);
ret = -ENOMEM; ret = -ENOMEM;
} }
#ifdef RT_USING_DFS_V2
df->fops = &eventfd_fops;
#endif
} }
return ret; return ret;
@ -245,7 +247,10 @@ static int do_eventfd(unsigned int count, int flags)
rt_ssize_t ret = 0; rt_ssize_t ret = 0;
if (flags & ~EFD_FLAGS_SET) if (flags & ~EFD_FLAGS_SET)
return -RT_EINVAL; {
rt_set_errno(EINVAL);
return -1;
}
fd = fd_new(); fd = fd_new();
if (fd >= 0) if (fd >= 0)
@ -257,12 +262,14 @@ static int do_eventfd(unsigned int count, int flags)
if (status < 0) if (status < 0)
{ {
fd_release(fd); fd_release(fd);
ret = status; rt_set_errno(-status);
ret = -1;
} }
} }
else else
{ {
ret = fd; rt_set_errno(-fd);
ret = -1;
} }
return ret; return ret;