From ff5ce1078ec40145a8ac8d3d2fa2a54e8eb36fb2 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Thu, 20 Jul 2023 14:27:10 +0800 Subject: [PATCH 01/23] Add the system call eventfd --- components/libc/posix/Kconfig | 5 + components/libc/posix/io/eventfd/SConscript | 15 ++ components/libc/posix/io/eventfd/eventfd.c | 253 ++++++++++++++++++++ components/libc/posix/io/eventfd/eventfd.h | 45 ++++ components/lwp/lwp_syscall.c | 17 ++ 5 files changed, 335 insertions(+) create mode 100644 components/libc/posix/io/eventfd/SConscript create mode 100644 components/libc/posix/io/eventfd/eventfd.c create mode 100644 components/libc/posix/io/eventfd/eventfd.h diff --git a/components/libc/posix/Kconfig b/components/libc/posix/Kconfig index 148014407a7..d9f1638e20f 100644 --- a/components/libc/posix/Kconfig +++ b/components/libc/posix/Kconfig @@ -25,6 +25,11 @@ if RT_USING_POSIX_FS bool "Enable I/O Multiplexing select() " select RT_USING_POSIX_POLL default n + + config RT_USING_POSIX_EVENTFD + bool "Enable I/O event eventfd " + select RT_USING_POSIX_POLL + default n config RT_USING_POSIX_SOCKET bool "Enable BSD Socket I/O " diff --git a/components/libc/posix/io/eventfd/SConscript b/components/libc/posix/io/eventfd/SConscript new file mode 100644 index 00000000000..dd63e5ce2cb --- /dev/null +++ b/components/libc/posix/io/eventfd/SConscript @@ -0,0 +1,15 @@ +# RT-Thread building script for component + +from building import * + +cwd = GetCurrentDir() +src = [] +CPPPATH = [cwd] + + +if GetDepend('RT_USING_POSIX_EVENTFD'): + src += ['eventfd.c'] + +group = DefineGroup('POSIX', src, depend = [''], CPPPATH = CPPPATH) + +Return('group') diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c new file mode 100644 index 00000000000..d7aabbab4bc --- /dev/null +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -0,0 +1,253 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-07-20 zmq810150896 first version + */ + +#include +#include +#include +#include +#include +#include +#include +#include "poll.h" +#include "eventfd.h" + +#ifndef RT_USING_DFS_V2 +static int eventfd_close(struct dfs_file *file); +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 int eventfd_write(struct dfs_file *file, const void *buf, size_t count); +static int noop_lseek(struct dfs_file *file, off_t offset); +#else +static int eventfd_close(struct dfs_file *file); +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 int eventfd_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos); +static int noop_lseek(struct dfs_file *file, off_t offset, int wherece); +#endif + +struct check_rt_unamed_event_number +{ + char _check[RT_NAME_MAX - 4 - 1 - BITS(RT_UNAMED_PIPE_NUMBER)]; +}; + +static void *resoure_id[RT_UNAMED_PIPE_NUMBER]; +static resource_id_t id_mgr = RESOURCE_ID_INIT(RT_UNAMED_PIPE_NUMBER, resoure_id); + +static const struct dfs_file_ops eventfd_fops = +{ + .close = eventfd_close, + .poll = eventfd_poll, + .read = eventfd_read, + .write = eventfd_write, + .lseek = noop_lseek, +}; + +void eventfd_ctx_do_read(struct eventfd_ctx *ctx, rt_u64 *cnt) +{ + *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count; + ctx->count -= *cnt; +} + +static int eventfd_close(struct dfs_file *file) +{ + struct eventfd_ctx *ctx = file->data; + free(ctx); + return 0; +} + +static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req) +{ + struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; + rt_poll_add(&ctx->reader_queue, req); + rt_poll_add(&ctx->writer_queue, req); + + int events = 0; + rt_u64 count; + count = ctx->count; + + if (count > 0) + events |= POLLIN; + + if (count == ULLONG_MAX) + events |= POLLERR; + + if (ULLONG_MAX -1 > count) + events |= POLLOUT; + + return events; +} + +#ifndef RT_USING_DFS_V2 +static int eventfd_read(struct dfs_file *file, void *buf, size_t count) +#else +static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos) +#endif +{ + struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; + rt_u64 ucnt = 0; + + rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); + + if(ctx->count == 0) + { + if (file->flags & O_NONBLOCK) + { + rt_mutex_release(&ctx->lock); + return -EAGAIN; + } + rt_mutex_release(&ctx->lock); + rt_wqueue_wakeup(&ctx->writer_queue, (void*)POLLOUT); + rt_wqueue_wait(&ctx->reader_queue, 0, RT_WAITING_FOREVER); + rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); + } + + eventfd_ctx_do_read(ctx, &ucnt); + rt_mutex_release(&ctx->lock); + + return sizeof(ucnt); +} + +#ifndef RT_USING_DFS_V2 +static int eventfd_write(struct dfs_file *file, const void *buf, size_t count) +#else +static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos) +#endif +{ + struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; + rt_ssize_t res; + rt_u64 ucnt = *(rt_u64 *)buf; + + if (count < sizeof(ucnt)) + return -EINVAL; + + if (ucnt == ULLONG_MAX) + return -EINVAL; + + res = -EAGAIN; + rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); + if ((ULLONG_MAX - ctx->count) > ucnt) + res = sizeof(ucnt); + else if (!(file->flags & O_NONBLOCK)) + { + + for (res = 0;;) + { + // status fun + if ((ULLONG_MAX - ctx->count) >= ucnt) + { + res = sizeof(ucnt); + break; + } + rt_mutex_release(&ctx->lock); + 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); + } + } + + if (res > 0) + { + rt_kprintf("%u \n", ucnt); + ctx->count += ucnt; + rt_wqueue_wakeup(&ctx->reader_queue, (void *)POLLIN); + } + + rt_mutex_release(&ctx->lock); + + return res; +} + +#ifndef RT_USING_DFS_V2 +static int noop_lseek(struct dfs_file *file, off_t offset) +#else +static int noop_lseek(struct dfs_file *file, off_t offset, int wherece) +#endif +{ + #ifndef RT_USING_DFS_V2 + return file->pos; + #else + return file->fpos; + #endif +} + +static int rt_eventfd_create(struct dfs_file *df,const char *name, unsigned int count, int flags) +{ + struct eventfd_ctx *ctx = RT_NULL; + + ctx = (struct eventfd_ctx *)rt_malloc(sizeof(struct eventfd_ctx)); + if (ctx == RT_NULL) + return -ENOMEM; + + ctx->count = count; + ctx->flags = flags; + ctx->id = rt_thread_self(); + flags &= EFD_SHARED_FCNTL_FLAGS; + flags |= O_RDWR; + + rt_mutex_init(&ctx->lock, name, RT_IPC_FLAG_FIFO); + rt_wqueue_init(&ctx->reader_queue); + rt_wqueue_init(&ctx->writer_queue); + + df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode)); + if (!df->vnode) + { + free(ctx); + return -ENOMEM; + } + + dfs_vnode_init(df->vnode, FT_REGULAR, &eventfd_fops); + df->vnode->data = ctx; + + df->flags = flags; + + return 0; +} + +static int do_eventfd(unsigned int count, int flags) +{ + struct dfs_file *file; + char dname[8]; + int fd; + int eventno = 0; + int status; + + if (flags & ~EFD_FLAGS_SET) + return -RT_EINVAL; + + fd = fd_new(); + if (fd < 0) + { + return fd; + } + + file = fd_get(fd); + + eventno = resource_id_get(&id_mgr); + rt_snprintf(dname, sizeof(dname), "eventfd%d", eventno); + + status = rt_eventfd_create(file, dname, count, flags); + if (status < 0) + { + fd_release(fd); + return status; + } + + return fd; +} + +int eventfd(unsigned int count) +{ + return do_eventfd(count, 0); +} + +int eventfd2(unsigned int count, int flags) +{ + return do_eventfd(count, flags); +} \ No newline at end of file diff --git a/components/libc/posix/io/eventfd/eventfd.h b/components/libc/posix/io/eventfd/eventfd.h new file mode 100644 index 00000000000..7724d97ae8a --- /dev/null +++ b/components/libc/posix/io/eventfd/eventfd.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-07-20 zmq810150896 First version + */ + +#ifndef __EVENTFD_H__ +#define __EVENTFD_H__ + +#include +#include +#include + +#define EFD_SEMAPHORE (1 << 0) +#define EFD_CLOEXEC O_CLOEXEC +#define EFD_NONBLOCK O_NONBLOCK + +#define EFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK) +#define EFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS | EFD_SEMAPHORE) + +#define ULLONG_MAX (~0ULL) + +#define BITS(x) _BITS(x) +#define _BITS(x) (sizeof(#x) - 1) + +typedef unsigned long rt_u64; + +struct eventfd_ctx { + + rt_wqueue_t reader_queue; + rt_wqueue_t writer_queue; + rt_uint64_t count; + unsigned int flags; + struct rt_mutex lock; + rt_thread_t id; +}; + +int eventfd(unsigned int count); +int eventfd2(unsigned int count, int flags); + +#endif /* __EVENTFD_H__ */ diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index fa254235233..9f6424eadfe 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -41,6 +41,7 @@ #include #ifdef RT_USING_DFS +#include #include #include #include @@ -5316,6 +5317,20 @@ sysret_t sys_symlink(const char *existing, const char *new) return (ret < 0 ? GET_ERRNO() : ret); } +sysret_t sys_eventfd(unsigned int count) +{ + int ret; + ret = eventfd(count); + return (ret < 0 ? GET_ERRNO() : ret); +} + +sysret_t sys_eventfd2(unsigned int count, int flags) +{ + int ret; + ret = eventfd2(count, flags); + return (ret < 0 ? GET_ERRNO() : ret); +} + static const struct rt_syscall_def func_table[] = { SYSCALL_SIGN(sys_exit), /* 01 */ @@ -5547,6 +5562,8 @@ static const struct rt_syscall_def func_table[] = SYSCALL_SIGN(sys_sigtimedwait), SYSCALL_SIGN(sys_notimpl), SYSCALL_SIGN(sys_notimpl), /* 190 */ + SYSCALL_SIGN(sys_eventfd2), + SYSCALL_SIGN(sys_eventfd), }; const void *lwp_get_sys_api(rt_uint32_t number) From aebc311b7d29829e7a71d353b5ace2ecf29074d0 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Thu, 20 Jul 2023 15:04:17 +0800 Subject: [PATCH 02/23] Update code whitespace issues --- components/libc/posix/io/eventfd/eventfd.c | 32 +++++++++++----------- components/libc/posix/io/eventfd/eventfd.h | 10 +++---- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index d7aabbab4bc..90450e85eae 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -40,19 +40,19 @@ struct check_rt_unamed_event_number static void *resoure_id[RT_UNAMED_PIPE_NUMBER]; static resource_id_t id_mgr = RESOURCE_ID_INIT(RT_UNAMED_PIPE_NUMBER, resoure_id); -static const struct dfs_file_ops eventfd_fops = +static const struct dfs_file_ops eventfd_fops = { - .close = eventfd_close, - .poll = eventfd_poll, - .read = eventfd_read, - .write = eventfd_write, - .lseek = noop_lseek, + .close = eventfd_close, + .poll = eventfd_poll, + .read = eventfd_read, + .write = eventfd_write, + .lseek = noop_lseek, }; void eventfd_ctx_do_read(struct eventfd_ctx *ctx, rt_u64 *cnt) { - *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count; - ctx->count -= *cnt; + *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count; + ctx->count -= *cnt; } static int eventfd_close(struct dfs_file *file) @@ -67,7 +67,7 @@ static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req) struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; rt_poll_add(&ctx->reader_queue, req); rt_poll_add(&ctx->writer_queue, req); - + int events = 0; rt_u64 count; count = ctx->count; @@ -77,7 +77,7 @@ static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req) if (count == ULLONG_MAX) events |= POLLERR; - + if (ULLONG_MAX -1 > count) events |= POLLOUT; @@ -123,20 +123,20 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; rt_ssize_t res; rt_u64 ucnt = *(rt_u64 *)buf; - + if (count < sizeof(ucnt)) return -EINVAL; if (ucnt == ULLONG_MAX) return -EINVAL; - + res = -EAGAIN; rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); if ((ULLONG_MAX - ctx->count) > ucnt) res = sizeof(ucnt); else if (!(file->flags & O_NONBLOCK)) { - + for (res = 0;;) { // status fun @@ -204,7 +204,7 @@ static int rt_eventfd_create(struct dfs_file *df,const char *name, unsigned int dfs_vnode_init(df->vnode, FT_REGULAR, &eventfd_fops); df->vnode->data = ctx; - + df->flags = flags; return 0; @@ -228,7 +228,7 @@ static int do_eventfd(unsigned int count, int flags) } file = fd_get(fd); - + eventno = resource_id_get(&id_mgr); rt_snprintf(dname, sizeof(dname), "eventfd%d", eventno); @@ -250,4 +250,4 @@ int eventfd(unsigned int count) int eventfd2(unsigned int count, int flags) { return do_eventfd(count, flags); -} \ No newline at end of file +} diff --git a/components/libc/posix/io/eventfd/eventfd.h b/components/libc/posix/io/eventfd/eventfd.h index 7724d97ae8a..b36c6aace53 100644 --- a/components/libc/posix/io/eventfd/eventfd.h +++ b/components/libc/posix/io/eventfd/eventfd.h @@ -22,7 +22,7 @@ #define EFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK) #define EFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS | EFD_SEMAPHORE) -#define ULLONG_MAX (~0ULL) +#define ULLONG_MAX (~0ULL) #define BITS(x) _BITS(x) #define _BITS(x) (sizeof(#x) - 1) @@ -30,13 +30,13 @@ typedef unsigned long rt_u64; struct eventfd_ctx { - + rt_wqueue_t reader_queue; rt_wqueue_t writer_queue; - rt_uint64_t count; - unsigned int flags; + rt_uint64_t count; + unsigned int flags; struct rt_mutex lock; - rt_thread_t id; + rt_thread_t id; }; int eventfd(unsigned int count); From 3a10467b5175009a64a6b21065932272574ad8f2 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Thu, 20 Jul 2023 15:06:23 +0800 Subject: [PATCH 03/23] Removing blank lines --- components/libc/posix/io/eventfd/SConscript | 1 - 1 file changed, 1 deletion(-) diff --git a/components/libc/posix/io/eventfd/SConscript b/components/libc/posix/io/eventfd/SConscript index dd63e5ce2cb..b4becfce4b7 100644 --- a/components/libc/posix/io/eventfd/SConscript +++ b/components/libc/posix/io/eventfd/SConscript @@ -6,7 +6,6 @@ cwd = GetCurrentDir() src = [] CPPPATH = [cwd] - if GetDepend('RT_USING_POSIX_EVENTFD'): src += ['eventfd.c'] From 759777b72979e2ebe750f50797c4d16f09e507ba Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Thu, 20 Jul 2023 15:08:14 +0800 Subject: [PATCH 04/23] Remove redundant comments --- components/libc/posix/io/eventfd/eventfd.c | 1 - 1 file changed, 1 deletion(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 90450e85eae..9c2c696921c 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -139,7 +139,6 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o for (res = 0;;) { - // status fun if ((ULLONG_MAX - ctx->count) >= ucnt) { res = sizeof(ucnt); From 08dcf10ef41e19409531c71e294cb4b4e2f8c2b0 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Thu, 20 Jul 2023 17:28:03 +0800 Subject: [PATCH 05/23] Add comments where necessary, add uniform exits where necessary, and update where it does not make sense --- components/libc/posix/io/eventfd/eventfd.c | 112 ++++++++++++--------- components/libc/posix/io/eventfd/eventfd.h | 3 - 2 files changed, 63 insertions(+), 52 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 9c2c696921c..8b0c9debf84 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -49,7 +49,7 @@ static const struct dfs_file_ops eventfd_fops = .lseek = noop_lseek, }; -void eventfd_ctx_do_read(struct eventfd_ctx *ctx, rt_u64 *cnt) +void eventfd_ctx_do_read(struct eventfd_ctx *ctx, rt_ubase_t *cnt) { *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count; ctx->count -= *cnt; @@ -69,7 +69,7 @@ static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req) rt_poll_add(&ctx->writer_queue, req); int events = 0; - rt_u64 count; + rt_ubase_t count; count = ctx->count; if (count > 0) @@ -91,21 +91,26 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p #endif { struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; - rt_u64 ucnt = 0; + rt_ubase_t ucnt = 0; rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); if(ctx->count == 0) { + rt_kprintf("aaaaa %d \n", file->flags & O_NONBLOCK); if (file->flags & O_NONBLOCK) { + rt_wqueue_wakeup(&ctx->writer_queue, (void*)POLLOUT); rt_mutex_release(&ctx->lock); return -EAGAIN; } - rt_mutex_release(&ctx->lock); - rt_wqueue_wakeup(&ctx->writer_queue, (void*)POLLOUT); - rt_wqueue_wait(&ctx->reader_queue, 0, RT_WAITING_FOREVER); - rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); + else + { + rt_mutex_release(&ctx->lock); + rt_wqueue_wakeup(&ctx->writer_queue, (void*)POLLOUT); + rt_wqueue_wait(&ctx->reader_queue, 0, RT_WAITING_FOREVER); + rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); + } } eventfd_ctx_do_read(ctx, &ucnt); @@ -122,7 +127,11 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o { struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; rt_ssize_t res; - rt_u64 ucnt = *(rt_u64 *)buf; + /* + ucnt: unsigned count + Adds a value to the counter + */ + rt_ubase_t ucnt = *(rt_ubase_t *)buf; if (count < sizeof(ucnt)) return -EINVAL; @@ -136,7 +145,6 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o res = sizeof(ucnt); else if (!(file->flags & O_NONBLOCK)) { - for (res = 0;;) { if ((ULLONG_MAX - ctx->count) >= ucnt) @@ -144,6 +152,7 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o res = sizeof(ucnt); break; } + /* Release the mutex to avoid a deadlock */ rt_mutex_release(&ctx->lock); rt_wqueue_wait(&ctx->writer_queue, 0, RT_WAITING_FOREVER); rt_wqueue_wakeup(&ctx->reader_queue, (void *)POLLIN); @@ -153,7 +162,6 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o if (res > 0) { - rt_kprintf("%u \n", ucnt); ctx->count += ucnt; rt_wqueue_wakeup(&ctx->reader_queue, (void *)POLLIN); } @@ -169,44 +177,46 @@ static int noop_lseek(struct dfs_file *file, off_t offset) static int noop_lseek(struct dfs_file *file, off_t offset, int wherece) #endif { - #ifndef RT_USING_DFS_V2 - return file->pos; - #else - return file->fpos; - #endif + return 0; } static int rt_eventfd_create(struct dfs_file *df,const char *name, unsigned int count, int flags) { struct eventfd_ctx *ctx = RT_NULL; + rt_err_t ret = 0; ctx = (struct eventfd_ctx *)rt_malloc(sizeof(struct eventfd_ctx)); if (ctx == RT_NULL) - return -ENOMEM; - - ctx->count = count; - ctx->flags = flags; - ctx->id = rt_thread_self(); - flags &= EFD_SHARED_FCNTL_FLAGS; - flags |= O_RDWR; - - rt_mutex_init(&ctx->lock, name, RT_IPC_FLAG_FIFO); - rt_wqueue_init(&ctx->reader_queue); - rt_wqueue_init(&ctx->writer_queue); - - df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode)); - if (!df->vnode) { - free(ctx); - return -ENOMEM; + ret = -ENOMEM; + } + else + { + ctx->count = count; + ctx->flags = flags; + ctx->id = rt_thread_self(); + flags &= EFD_SHARED_FCNTL_FLAGS; + flags |= O_RDWR; + + rt_mutex_init(&ctx->lock, name, RT_IPC_FLAG_FIFO); + rt_wqueue_init(&ctx->reader_queue); + rt_wqueue_init(&ctx->writer_queue); + + df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode)); + if (df->vnode) + { + dfs_vnode_init(df->vnode, FT_REGULAR, &eventfd_fops); + df->vnode->data = ctx; + df->flags = flags; + } + else + { + free(ctx); + ret = -ENOMEM; + } } - dfs_vnode_init(df->vnode, FT_REGULAR, &eventfd_fops); - df->vnode->data = ctx; - - df->flags = flags; - - return 0; + return ret; } static int do_eventfd(unsigned int count, int flags) @@ -216,29 +226,33 @@ static int do_eventfd(unsigned int count, int flags) int fd; int eventno = 0; int status; + int res = 0; if (flags & ~EFD_FLAGS_SET) return -RT_EINVAL; fd = fd_new(); - if (fd < 0) + if (fd >= 0) { - return fd; - } - - file = fd_get(fd); + res = fd; + file = fd_get(fd); - eventno = resource_id_get(&id_mgr); - rt_snprintf(dname, sizeof(dname), "eventfd%d", eventno); + eventno = resource_id_get(&id_mgr); + rt_snprintf(dname, sizeof(dname), "eventfd%d", eventno); - status = rt_eventfd_create(file, dname, count, flags); - if (status < 0) + status = rt_eventfd_create(file, dname, count, flags); + if (status < 0) + { + fd_release(fd); + res = status; + } + } + else { - fd_release(fd); - return status; + res = fd; } - return fd; + return res; } int eventfd(unsigned int count) diff --git a/components/libc/posix/io/eventfd/eventfd.h b/components/libc/posix/io/eventfd/eventfd.h index b36c6aace53..62687ed8102 100644 --- a/components/libc/posix/io/eventfd/eventfd.h +++ b/components/libc/posix/io/eventfd/eventfd.h @@ -27,10 +27,7 @@ #define BITS(x) _BITS(x) #define _BITS(x) (sizeof(#x) - 1) -typedef unsigned long rt_u64; - struct eventfd_ctx { - rt_wqueue_t reader_queue; rt_wqueue_t writer_queue; rt_uint64_t count; From 4e8479bf1e1db1bc0a16d86200ce35901d0ce28a Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Thu, 20 Jul 2023 17:32:59 +0800 Subject: [PATCH 06/23] Update whitespace issues --- components/libc/posix/io/eventfd/eventfd.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 8b0c9debf84..18a4bec2c5e 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -127,11 +127,11 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o { struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; rt_ssize_t res; - /* - ucnt: unsigned count - Adds a value to the counter + /* + ucnt: unsigned count + Adds a value to the counter */ - rt_ubase_t ucnt = *(rt_ubase_t *)buf; + rt_ubase_t ucnt = *(rt_ubase_t *)buf; if (count < sizeof(ucnt)) return -EINVAL; From 0ddcc82d6f3f53f5a68c14c2ddf10f1970c5536e Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Thu, 20 Jul 2023 17:43:29 +0800 Subject: [PATCH 07/23] Problem updating mutex names --- components/libc/posix/io/eventfd/eventfd.c | 19 +++---------------- components/libc/posix/io/eventfd/eventfd.h | 3 +-- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 18a4bec2c5e..4f9740d964e 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -32,14 +32,6 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o static int noop_lseek(struct dfs_file *file, off_t offset, int wherece); #endif -struct check_rt_unamed_event_number -{ - char _check[RT_NAME_MAX - 4 - 1 - BITS(RT_UNAMED_PIPE_NUMBER)]; -}; - -static void *resoure_id[RT_UNAMED_PIPE_NUMBER]; -static resource_id_t id_mgr = RESOURCE_ID_INIT(RT_UNAMED_PIPE_NUMBER, resoure_id); - static const struct dfs_file_ops eventfd_fops = { .close = eventfd_close, @@ -180,7 +172,7 @@ static int noop_lseek(struct dfs_file *file, off_t offset, int wherece) return 0; } -static int rt_eventfd_create(struct dfs_file *df,const char *name, unsigned int count, int flags) +static int rt_eventfd_create(struct dfs_file *df, unsigned int count, int flags) { struct eventfd_ctx *ctx = RT_NULL; rt_err_t ret = 0; @@ -198,7 +190,7 @@ static int rt_eventfd_create(struct dfs_file *df,const char *name, unsigned int flags &= EFD_SHARED_FCNTL_FLAGS; flags |= O_RDWR; - rt_mutex_init(&ctx->lock, name, RT_IPC_FLAG_FIFO); + rt_mutex_init(&ctx->lock, EVENTFD_MUTEX_NAME, RT_IPC_FLAG_FIFO); rt_wqueue_init(&ctx->reader_queue); rt_wqueue_init(&ctx->writer_queue); @@ -222,9 +214,7 @@ static int rt_eventfd_create(struct dfs_file *df,const char *name, unsigned int static int do_eventfd(unsigned int count, int flags) { struct dfs_file *file; - char dname[8]; int fd; - int eventno = 0; int status; int res = 0; @@ -237,10 +227,7 @@ static int do_eventfd(unsigned int count, int flags) res = fd; file = fd_get(fd); - eventno = resource_id_get(&id_mgr); - rt_snprintf(dname, sizeof(dname), "eventfd%d", eventno); - - status = rt_eventfd_create(file, dname, count, flags); + status = rt_eventfd_create(file, count, flags); if (status < 0) { fd_release(fd); diff --git a/components/libc/posix/io/eventfd/eventfd.h b/components/libc/posix/io/eventfd/eventfd.h index 62687ed8102..731cba00062 100644 --- a/components/libc/posix/io/eventfd/eventfd.h +++ b/components/libc/posix/io/eventfd/eventfd.h @@ -24,8 +24,7 @@ #define ULLONG_MAX (~0ULL) -#define BITS(x) _BITS(x) -#define _BITS(x) (sizeof(#x) - 1) +#define EVENTFD_MUTEX_NAME "eventfd" struct eventfd_ctx { rt_wqueue_t reader_queue; From e5484a462dcceb3922b0cf016e69945695d6de36 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Thu, 20 Jul 2023 17:55:41 +0800 Subject: [PATCH 08/23] Remove redundant header references --- components/libc/posix/io/eventfd/eventfd.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 4f9740d964e..a51bcadfafe 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -11,8 +11,6 @@ #include #include #include -#include -#include #include #include #include "poll.h" From 14b4650ee65971e3fb3f950a902d017c2776b465 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Thu, 20 Jul 2023 20:51:39 +0800 Subject: [PATCH 09/23] Update code with formatting, macro definitions, and other irregularities --- components/libc/posix/io/eventfd/eventfd.c | 56 ++++++++++++++++------ components/libc/posix/io/eventfd/eventfd.h | 20 -------- components/lwp/lwp_syscall.c | 2 + 3 files changed, 43 insertions(+), 35 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index a51bcadfafe..7c54c8ef18f 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -16,6 +16,26 @@ #include "poll.h" #include "eventfd.h" +#define EFD_SEMAPHORE (1 << 0) +#define EFD_CLOEXEC O_CLOEXEC +#define EFD_NONBLOCK O_NONBLOCK + +#define EFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK) +#define EFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS | EFD_SEMAPHORE) + +#define ULLONG_MAX (~0ULL) + +#define EVENTFD_MUTEX_NAME "eventfd" + +struct eventfd_ctx { + rt_wqueue_t reader_queue; + rt_wqueue_t writer_queue; + rt_uint64_t count; + unsigned int flags; + struct rt_mutex lock; + rt_thread_t id; +}; + #ifndef RT_USING_DFS_V2 static int eventfd_close(struct dfs_file *file); static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req); @@ -30,6 +50,12 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o static int noop_lseek(struct dfs_file *file, off_t offset, int wherece); #endif +static inline void eventfd_ctx_do_read(struct eventfd_ctx *ctx, rt_ubase_t *cnt) +{ + *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count; + ctx->count -= *cnt; +} + static const struct dfs_file_ops eventfd_fops = { .close = eventfd_close, @@ -39,29 +65,27 @@ static const struct dfs_file_ops eventfd_fops = .lseek = noop_lseek, }; -void eventfd_ctx_do_read(struct eventfd_ctx *ctx, rt_ubase_t *cnt) -{ - *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count; - ctx->count -= *cnt; -} - static int eventfd_close(struct dfs_file *file) { - struct eventfd_ctx *ctx = file->data; - free(ctx); + struct eventfd_ctx *ctx = file->vnode->data; + + rt_mutex_detach (&ctx->lock); + rt_free(ctx); + return 0; } static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req) { struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; - rt_poll_add(&ctx->reader_queue, req); - rt_poll_add(&ctx->writer_queue, req); - int events = 0; rt_ubase_t count; + count = ctx->count; + rt_poll_add(&ctx->reader_queue, req); + rt_poll_add(&ctx->writer_queue, req); + if (count > 0) events |= POLLIN; @@ -85,9 +109,8 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); - if(ctx->count == 0) + if (ctx->count == 0) { - rt_kprintf("aaaaa %d \n", file->flags & O_NONBLOCK); if (file->flags & O_NONBLOCK) { rt_wqueue_wakeup(&ctx->writer_queue, (void*)POLLOUT); @@ -96,6 +119,7 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p } else { + /* In this case, when the data is read in blocked mode, when ctx->count is 0, the mutex needs to be released and wait for writing */ rt_mutex_release(&ctx->lock); rt_wqueue_wakeup(&ctx->writer_queue, (void*)POLLOUT); rt_wqueue_wait(&ctx->reader_queue, 0, RT_WAITING_FOREVER); @@ -130,7 +154,9 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o return -EINVAL; res = -EAGAIN; + rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); + if ((ULLONG_MAX - ctx->count) > ucnt) res = sizeof(ucnt); else if (!(file->flags & O_NONBLOCK)) @@ -201,7 +227,7 @@ static int rt_eventfd_create(struct dfs_file *df, unsigned int count, int flags) } else { - free(ctx); + rt_free(ctx); ret = -ENOMEM; } } @@ -214,7 +240,7 @@ static int do_eventfd(unsigned int count, int flags) struct dfs_file *file; int fd; int status; - int res = 0; + rt_ssize_t res = 0; if (flags & ~EFD_FLAGS_SET) return -RT_EINVAL; diff --git a/components/libc/posix/io/eventfd/eventfd.h b/components/libc/posix/io/eventfd/eventfd.h index 731cba00062..915b01f970c 100644 --- a/components/libc/posix/io/eventfd/eventfd.h +++ b/components/libc/posix/io/eventfd/eventfd.h @@ -15,26 +15,6 @@ #include #include -#define EFD_SEMAPHORE (1 << 0) -#define EFD_CLOEXEC O_CLOEXEC -#define EFD_NONBLOCK O_NONBLOCK - -#define EFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK) -#define EFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS | EFD_SEMAPHORE) - -#define ULLONG_MAX (~0ULL) - -#define EVENTFD_MUTEX_NAME "eventfd" - -struct eventfd_ctx { - rt_wqueue_t reader_queue; - rt_wqueue_t writer_queue; - rt_uint64_t count; - unsigned int flags; - struct rt_mutex lock; - rt_thread_t id; -}; - int eventfd(unsigned int count); int eventfd2(unsigned int count, int flags); diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index 9f6424eadfe..755ea9f8de9 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -5320,6 +5320,7 @@ sysret_t sys_symlink(const char *existing, const char *new) sysret_t sys_eventfd(unsigned int count) { int ret; + ret = eventfd(count); return (ret < 0 ? GET_ERRNO() : ret); } @@ -5327,6 +5328,7 @@ sysret_t sys_eventfd(unsigned int count) sysret_t sys_eventfd2(unsigned int count, int flags) { int ret; + ret = eventfd2(count, flags); return (ret < 0 ? GET_ERRNO() : ret); } From 985db89509391d58cdc40b177655623d9c10582b Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Fri, 21 Jul 2023 14:45:47 +0800 Subject: [PATCH 10/23] Add read bufAdd the value of the read function parameter buf --- components/libc/posix/io/eventfd/eventfd.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 7c54c8ef18f..6c5d33613ba 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -106,6 +106,7 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p { struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; rt_ubase_t ucnt = 0; + rt_ubase_t *buffer = (rt_ubase_t *)buf; rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); @@ -128,6 +129,9 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p } eventfd_ctx_do_read(ctx, &ucnt); + + (*buffer) = ucnt; + rt_mutex_release(&ctx->lock); return sizeof(ucnt); @@ -268,6 +272,7 @@ static int do_eventfd(unsigned int count, int flags) int eventfd(unsigned int count) { + rt_kprintf("qqq \n"); return do_eventfd(count, 0); } From 2dc0fde6965246fd36aeeda8549603492bbd3390 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Fri, 21 Jul 2023 14:48:44 +0800 Subject: [PATCH 11/23] Add read bufAdd the value of the read function parameter buf --- components/libc/posix/io/eventfd/eventfd.c | 1 - 1 file changed, 1 deletion(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 6c5d33613ba..b8234078bbb 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -272,7 +272,6 @@ static int do_eventfd(unsigned int count, int flags) int eventfd(unsigned int count) { - rt_kprintf("qqq \n"); return do_eventfd(count, 0); } From dff402fe154d6477ed43c2e85a62b4042d77ee8a Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Sat, 22 Jul 2023 11:19:15 +0800 Subject: [PATCH 12/23] detach the mutex memory --- components/libc/posix/io/eventfd/eventfd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index b8234078bbb..25f00bc2fe4 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -69,7 +69,7 @@ static int eventfd_close(struct dfs_file *file) { struct eventfd_ctx *ctx = file->vnode->data; - rt_mutex_detach (&ctx->lock); + rt_mutex_detach(&ctx->lock); rt_free(ctx); return 0; @@ -232,6 +232,7 @@ static int rt_eventfd_create(struct dfs_file *df, unsigned int count, int flags) else { rt_free(ctx); + rt_mutex_detach(&ctx->lock); ret = -ENOMEM; } } From d0ca79a540e351800ab9a8328c334ca94acb450e Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Sat, 22 Jul 2023 11:23:01 +0800 Subject: [PATCH 13/23] Add parentheses to keep the code regular --- components/libc/posix/io/eventfd/eventfd.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 25f00bc2fe4..521fbb2323a 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -162,7 +162,9 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); if ((ULLONG_MAX - ctx->count) > ucnt) + { res = sizeof(ucnt); + } else if (!(file->flags & O_NONBLOCK)) { for (res = 0;;) From 031c2b09b2947e49cdc9d687faaf3fb4ee4a3be5 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Sat, 22 Jul 2023 17:58:47 +0800 Subject: [PATCH 14/23] Fix lwp syscall.c file format issues --- components/lwp/lwp_syscall.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index 755ea9f8de9..4728b784829 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -5328,7 +5328,7 @@ sysret_t sys_eventfd(unsigned int count) sysret_t sys_eventfd2(unsigned int count, int flags) { int ret; - + ret = eventfd2(count, flags); return (ret < 0 ? GET_ERRNO() : ret); } From 59852c60baf1d065312bf4df86e8cd6b42d3a3a5 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Sat, 22 Jul 2023 22:26:20 +0800 Subject: [PATCH 15/23] Remove redundant header references and adjust memory release order --- components/libc/posix/io/eventfd/eventfd.c | 3 ++- components/libc/posix/io/eventfd/eventfd.h | 4 ---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 521fbb2323a..4cd4aebad5a 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "poll.h" #include "eventfd.h" @@ -233,8 +234,8 @@ static int rt_eventfd_create(struct dfs_file *df, unsigned int count, int flags) } else { - rt_free(ctx); rt_mutex_detach(&ctx->lock); + rt_free(ctx); ret = -ENOMEM; } } diff --git a/components/libc/posix/io/eventfd/eventfd.h b/components/libc/posix/io/eventfd/eventfd.h index 915b01f970c..a3bba2cf9e6 100644 --- a/components/libc/posix/io/eventfd/eventfd.h +++ b/components/libc/posix/io/eventfd/eventfd.h @@ -11,10 +11,6 @@ #ifndef __EVENTFD_H__ #define __EVENTFD_H__ -#include -#include -#include - int eventfd(unsigned int count); int eventfd2(unsigned int count, int flags); From 8cf71c39a7646d7da38e2e275aa05ee2ea6cbb7e Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Sun, 23 Jul 2023 11:06:02 +0800 Subject: [PATCH 16/23] Removes the system call for eventfd2 --- components/libc/posix/io/eventfd/eventfd.c | 7 +------ components/libc/posix/io/eventfd/eventfd.h | 3 +-- components/lwp/lwp_syscall.c | 11 +---------- 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 4cd4aebad5a..f82b24a11d9 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -274,12 +274,7 @@ static int do_eventfd(unsigned int count, int flags) return res; } -int eventfd(unsigned int count) -{ - return do_eventfd(count, 0); -} - -int eventfd2(unsigned int count, int flags) +int eventfd(unsigned int count, int flags) { return do_eventfd(count, flags); } diff --git a/components/libc/posix/io/eventfd/eventfd.h b/components/libc/posix/io/eventfd/eventfd.h index a3bba2cf9e6..8b3bc4edc4f 100644 --- a/components/libc/posix/io/eventfd/eventfd.h +++ b/components/libc/posix/io/eventfd/eventfd.h @@ -11,7 +11,6 @@ #ifndef __EVENTFD_H__ #define __EVENTFD_H__ -int eventfd(unsigned int count); -int eventfd2(unsigned int count, int flags); +int eventfd(unsigned int count, int flags); #endif /* __EVENTFD_H__ */ diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index 4728b784829..a95dbd85b59 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -5317,19 +5317,11 @@ sysret_t sys_symlink(const char *existing, const char *new) return (ret < 0 ? GET_ERRNO() : ret); } -sysret_t sys_eventfd(unsigned int count) -{ - int ret; - - ret = eventfd(count); - return (ret < 0 ? GET_ERRNO() : ret); -} - sysret_t sys_eventfd2(unsigned int count, int flags) { int ret; - ret = eventfd2(count, flags); + ret = eventfd(count, flags); return (ret < 0 ? GET_ERRNO() : ret); } @@ -5565,7 +5557,6 @@ static const struct rt_syscall_def func_table[] = SYSCALL_SIGN(sys_notimpl), SYSCALL_SIGN(sys_notimpl), /* 190 */ SYSCALL_SIGN(sys_eventfd2), - SYSCALL_SIGN(sys_eventfd), }; const void *lwp_get_sys_api(rt_uint32_t number) From d9cc2b849588d1aaedf62bc7a6493384b648d4d3 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Sun, 23 Jul 2023 12:55:48 +0800 Subject: [PATCH 17/23] Modify the counter handling logic --- components/libc/posix/io/eventfd/eventfd.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index f82b24a11d9..a23672b87b1 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -51,12 +51,6 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o static int noop_lseek(struct dfs_file *file, off_t offset, int wherece); #endif -static inline void eventfd_ctx_do_read(struct eventfd_ctx *ctx, rt_ubase_t *cnt) -{ - *cnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count; - ctx->count -= *cnt; -} - static const struct dfs_file_ops eventfd_fops = { .close = eventfd_close, @@ -129,7 +123,16 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p } } - eventfd_ctx_do_read(ctx, &ucnt); + if (ctx->flags & EFD_SEMAPHORE) + { + ucnt = 1; + } + else + { + ucnt = ctx->count; + } + + ctx->count -= ucnt; (*buffer) = ucnt; From 965465edf48b18b4dc3d9b693861c07f27148c12 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Mon, 24 Jul 2023 14:07:44 +0800 Subject: [PATCH 18/23] Update the data type, remove lseek, and add the count size test in the read function --- components/libc/posix/io/eventfd/eventfd.c | 26 ++++++++-------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index a23672b87b1..3408a004db3 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -28,7 +28,8 @@ #define EVENTFD_MUTEX_NAME "eventfd" -struct eventfd_ctx { +struct eventfd_ctx +{ rt_wqueue_t reader_queue; rt_wqueue_t writer_queue; rt_uint64_t count; @@ -42,13 +43,11 @@ static int eventfd_close(struct dfs_file *file); 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 int eventfd_write(struct dfs_file *file, const void *buf, size_t count); -static int noop_lseek(struct dfs_file *file, off_t offset); #else static int eventfd_close(struct dfs_file *file); 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 int eventfd_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos); -static int noop_lseek(struct dfs_file *file, off_t offset, int wherece); #endif static const struct dfs_file_ops eventfd_fops = @@ -57,7 +56,6 @@ static const struct dfs_file_ops eventfd_fops = .poll = eventfd_poll, .read = eventfd_read, .write = eventfd_write, - .lseek = noop_lseek, }; static int eventfd_close(struct dfs_file *file) @@ -74,7 +72,7 @@ static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req) { struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; int events = 0; - rt_ubase_t count; + rt_uint64_t count; count = ctx->count; @@ -100,8 +98,11 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p #endif { struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; - rt_ubase_t ucnt = 0; - rt_ubase_t *buffer = (rt_ubase_t *)buf; + rt_uint64_t ucnt = 0; + rt_uint64_t *buffer = (rt_uint64_t *)buf; + + if (count < sizeof(ucnt)) + return -EINVAL; rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); @@ -153,7 +154,7 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o ucnt: unsigned count Adds a value to the counter */ - rt_ubase_t ucnt = *(rt_ubase_t *)buf; + rt_uint64_t ucnt = *(rt_uint64_t *)buf; if (count < sizeof(ucnt)) return -EINVAL; @@ -197,15 +198,6 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o return res; } -#ifndef RT_USING_DFS_V2 -static int noop_lseek(struct dfs_file *file, off_t offset) -#else -static int noop_lseek(struct dfs_file *file, off_t offset, int wherece) -#endif -{ - return 0; -} - static int rt_eventfd_create(struct dfs_file *df, unsigned int count, int flags) { struct eventfd_ctx *ctx = RT_NULL; From c1f490b26c6af502bfc4fffbc30a1e48cc53703f Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Mon, 24 Jul 2023 15:15:05 +0800 Subject: [PATCH 19/23] Remove extra white space --- components/libc/posix/io/eventfd/eventfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 3408a004db3..c3c9cf739e1 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -100,7 +100,7 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; rt_uint64_t ucnt = 0; rt_uint64_t *buffer = (rt_uint64_t *)buf; - + if (count < sizeof(ucnt)) return -EINVAL; From 4163330becabae501628cde2b1cdd0db58539435 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Mon, 24 Jul 2023 16:21:06 +0800 Subject: [PATCH 20/23] Remove unnecessary elements from a struct --- components/libc/posix/io/eventfd/eventfd.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index c3c9cf739e1..2e98c578ab0 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -35,7 +35,6 @@ struct eventfd_ctx rt_uint64_t count; unsigned int flags; struct rt_mutex lock; - rt_thread_t id; }; #ifndef RT_USING_DFS_V2 @@ -212,7 +211,6 @@ static int rt_eventfd_create(struct dfs_file *df, unsigned int count, int flags) { ctx->count = count; ctx->flags = flags; - ctx->id = rt_thread_self(); flags &= EFD_SHARED_FCNTL_FLAGS; flags |= O_RDWR; From 3e9ced7c4f4ec35ff6052de3059a19c41c5119c8 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Mon, 24 Jul 2023 19:13:28 +0800 Subject: [PATCH 21/23] Variable naming changes --- components/libc/posix/io/eventfd/eventfd.c | 59 ++++++++++------------ 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 2e98c578ab0..410389697df 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -84,7 +84,7 @@ static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req) if (count == ULLONG_MAX) events |= POLLERR; - if (ULLONG_MAX -1 > count) + if ((ULLONG_MAX -1) > count) events |= POLLOUT; return events; @@ -97,10 +97,10 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p #endif { struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; - rt_uint64_t ucnt = 0; + rt_uint64_t counter_num = 0; rt_uint64_t *buffer = (rt_uint64_t *)buf; - if (count < sizeof(ucnt)) + if (count < sizeof(counter_num)) return -EINVAL; rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); @@ -125,20 +125,20 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p if (ctx->flags & EFD_SEMAPHORE) { - ucnt = 1; + counter_num = 1; } else { - ucnt = ctx->count; + counter_num = ctx->count; } - ctx->count -= ucnt; + ctx->count -= counter_num; - (*buffer) = ucnt; + (*buffer) = counter_num; rt_mutex_release(&ctx->lock); - return sizeof(ucnt); + return sizeof(counter_num); } #ifndef RT_USING_DFS_V2 @@ -148,34 +148,31 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o #endif { struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; - rt_ssize_t res; - /* - ucnt: unsigned count - Adds a value to the counter - */ - rt_uint64_t ucnt = *(rt_uint64_t *)buf; - - if (count < sizeof(ucnt)) + rt_ssize_t ret = 0; + + rt_uint64_t counter_num = *(rt_uint64_t *)buf; + + if (count < sizeof(counter_num)) return -EINVAL; - if (ucnt == ULLONG_MAX) + if (counter_num == ULLONG_MAX) return -EINVAL; - res = -EAGAIN; + ret = -EAGAIN; rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); - if ((ULLONG_MAX - ctx->count) > ucnt) + if ((ULLONG_MAX - ctx->count) > counter_num) { - res = sizeof(ucnt); + ret = sizeof(counter_num); } else if (!(file->flags & O_NONBLOCK)) { - for (res = 0;;) + for (;;) { - if ((ULLONG_MAX - ctx->count) >= ucnt) + if ((ULLONG_MAX - ctx->count) >= counter_num) { - res = sizeof(ucnt); + ret = sizeof(counter_num); break; } /* Release the mutex to avoid a deadlock */ @@ -186,15 +183,15 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o } } - if (res > 0) + if (ret > 0) { - ctx->count += ucnt; + ctx->count += counter_num; rt_wqueue_wakeup(&ctx->reader_queue, (void *)POLLIN); } rt_mutex_release(&ctx->lock); - return res; + return ret; } static int rt_eventfd_create(struct dfs_file *df, unsigned int count, int flags) @@ -241,7 +238,7 @@ static int do_eventfd(unsigned int count, int flags) struct dfs_file *file; int fd; int status; - rt_ssize_t res = 0; + rt_ssize_t ret = 0; if (flags & ~EFD_FLAGS_SET) return -RT_EINVAL; @@ -249,22 +246,22 @@ static int do_eventfd(unsigned int count, int flags) fd = fd_new(); if (fd >= 0) { - res = fd; + ret = fd; file = fd_get(fd); status = rt_eventfd_create(file, count, flags); if (status < 0) { fd_release(fd); - res = status; + ret = status; } } else { - res = fd; + ret = fd; } - return res; + return ret; } int eventfd(unsigned int count, int flags) From f06c9375968ad505294bd0db5c3b9c9af19f1e59 Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Mon, 24 Jul 2023 19:16:08 +0800 Subject: [PATCH 22/23] Canonical code --- components/libc/posix/io/eventfd/eventfd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 410389697df..251b64ea06e 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -84,7 +84,7 @@ static int eventfd_poll(struct dfs_file *file, struct rt_pollreq *req) if (count == ULLONG_MAX) events |= POLLERR; - if ((ULLONG_MAX -1) > count) + if ((ULLONG_MAX - 1) > count) events |= POLLOUT; return events; From f65237e01ba6c5f91e8c4ddf30572d2a17197cec Mon Sep 17 00:00:00 2001 From: zmq810150896 Date: Tue, 25 Jul 2023 11:57:34 +0800 Subject: [PATCH 23/23] Change the checking and assignment order --- components/libc/posix/io/eventfd/eventfd.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/components/libc/posix/io/eventfd/eventfd.c b/components/libc/posix/io/eventfd/eventfd.c index 251b64ea06e..1d6c79b8095 100644 --- a/components/libc/posix/io/eventfd/eventfd.c +++ b/components/libc/posix/io/eventfd/eventfd.c @@ -98,11 +98,13 @@ static int eventfd_read(struct dfs_file *file, void *buf, size_t count, off_t *p { struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; rt_uint64_t counter_num = 0; - rt_uint64_t *buffer = (rt_uint64_t *)buf; + rt_uint64_t *buffer; if (count < sizeof(counter_num)) return -EINVAL; + buffer = (rt_uint64_t *)buf; + rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); if (ctx->count == 0) @@ -150,11 +152,13 @@ static int eventfd_write(struct dfs_file *file, const void *buf, size_t count, o struct eventfd_ctx *ctx = (struct eventfd_ctx *)file->vnode->data; rt_ssize_t ret = 0; - rt_uint64_t counter_num = *(rt_uint64_t *)buf; + rt_uint64_t counter_num; if (count < sizeof(counter_num)) return -EINVAL; + counter_num = *(rt_uint64_t *)buf; + if (counter_num == ULLONG_MAX) return -EINVAL;