-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add the system call eventfd #7835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ff5ce10
Add the system call eventfd
zmq810150896 aebc311
Update code whitespace issues
zmq810150896 3a10467
Removing blank lines
zmq810150896 759777b
Remove redundant comments
zmq810150896 08dcf10
Add comments where necessary, add uniform exits where necessary, and …
zmq810150896 4e8479b
Update whitespace issues
zmq810150896 0ddcc82
Problem updating mutex names
zmq810150896 e5484a4
Remove redundant header references
zmq810150896 14b4650
Update code with formatting, macro definitions, and other irregularities
zmq810150896 985db89
Add read bufAdd the value of the read function parameter buf
zmq810150896 2dc0fde
Add read bufAdd the value of the read function parameter buf
zmq810150896 dff402f
detach the mutex memory
zmq810150896 d0ca79a
Add parentheses to keep the code regular
zmq810150896 031c2b0
Fix lwp syscall.c file format issues
zmq810150896 59852c6
Remove redundant header references and adjust memory release order
zmq810150896 8cf71c3
Removes the system call for eventfd2
zmq810150896 d9cc2b8
Modify the counter handling logic
zmq810150896 965465e
Update the data type, remove lseek, and add the count size test in th…
zmq810150896 c1f490b
Remove extra white space
zmq810150896 4163330
Remove unnecessary elements from a struct
zmq810150896 3e9ced7
Variable naming changes
zmq810150896 f06c937
Canonical code
zmq810150896 f65237e
Change the checking and assignment order
zmq810150896 5fd2d85
Merge branch 'RT-Thread:master' into master
zmq810150896 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # 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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,274 @@ | ||
| /* | ||
| * 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 <rtthread.h> | ||
| #include <fcntl.h> | ||
| #include <rtdevice.h> | ||
| #include <stdint.h> | ||
| #include <unistd.h> | ||
| #include <dfs_file.h> | ||
| #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; | ||
| }; | ||
|
|
||
| #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); | ||
| #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); | ||
| #endif | ||
|
|
||
| static const struct dfs_file_ops eventfd_fops = | ||
| { | ||
| .close = eventfd_close, | ||
| .poll = eventfd_poll, | ||
| .read = eventfd_read, | ||
| .write = eventfd_write, | ||
| }; | ||
|
|
||
| static int eventfd_close(struct dfs_file *file) | ||
| { | ||
| 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; | ||
| int events = 0; | ||
| rt_uint64_t count; | ||
|
|
||
| count = ctx->count; | ||
|
|
||
| rt_poll_add(&ctx->reader_queue, req); | ||
| rt_poll_add(&ctx->writer_queue, req); | ||
|
|
||
| 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_uint64_t counter_num = 0; | ||
| 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) | ||
| { | ||
| if (file->flags & O_NONBLOCK) | ||
| { | ||
| rt_wqueue_wakeup(&ctx->writer_queue, (void*)POLLOUT); | ||
| rt_mutex_release(&ctx->lock); | ||
| return -EAGAIN; | ||
| } | ||
| 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); | ||
| rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); | ||
| } | ||
| } | ||
|
|
||
| if (ctx->flags & EFD_SEMAPHORE) | ||
| { | ||
| counter_num = 1; | ||
| } | ||
| else | ||
| { | ||
| counter_num = ctx->count; | ||
| } | ||
|
|
||
| ctx->count -= counter_num; | ||
|
|
||
| (*buffer) = counter_num; | ||
|
|
||
| rt_mutex_release(&ctx->lock); | ||
|
|
||
| return sizeof(counter_num); | ||
| } | ||
|
|
||
| #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 ret = 0; | ||
|
|
||
| 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; | ||
|
|
||
| ret = -EAGAIN; | ||
|
|
||
| rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); | ||
|
|
||
| if ((ULLONG_MAX - ctx->count) > counter_num) | ||
| { | ||
| ret = sizeof(counter_num); | ||
| } | ||
| else if (!(file->flags & O_NONBLOCK)) | ||
| { | ||
| for (;;) | ||
| { | ||
| if ((ULLONG_MAX - ctx->count) >= counter_num) | ||
| { | ||
| ret = sizeof(counter_num); | ||
| 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); | ||
| rt_mutex_take(&ctx->lock, RT_WAITING_FOREVER); | ||
| } | ||
| } | ||
|
|
||
| if (ret > 0) | ||
| { | ||
| ctx->count += counter_num; | ||
| rt_wqueue_wakeup(&ctx->reader_queue, (void *)POLLIN); | ||
| } | ||
|
|
||
| rt_mutex_release(&ctx->lock); | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| 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; | ||
|
|
||
| ctx = (struct eventfd_ctx *)rt_malloc(sizeof(struct eventfd_ctx)); | ||
| if (ctx == RT_NULL) | ||
| { | ||
| ret = -ENOMEM; | ||
| } | ||
| else | ||
| { | ||
| ctx->count = count; | ||
| ctx->flags = flags; | ||
| flags &= EFD_SHARED_FCNTL_FLAGS; | ||
| flags |= O_RDWR; | ||
|
|
||
| rt_mutex_init(&ctx->lock, EVENTFD_MUTEX_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 | ||
| { | ||
| rt_mutex_detach(&ctx->lock); | ||
| rt_free(ctx); | ||
BernardXiong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ret = -ENOMEM; | ||
| } | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| static int do_eventfd(unsigned int count, int flags) | ||
| { | ||
| struct dfs_file *file; | ||
| int fd; | ||
| int status; | ||
| rt_ssize_t ret = 0; | ||
|
|
||
| if (flags & ~EFD_FLAGS_SET) | ||
| return -RT_EINVAL; | ||
|
|
||
| fd = fd_new(); | ||
| if (fd >= 0) | ||
| { | ||
| ret = fd; | ||
| file = fd_get(fd); | ||
|
|
||
| status = rt_eventfd_create(file, count, flags); | ||
| if (status < 0) | ||
| { | ||
| fd_release(fd); | ||
| ret = status; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| ret = fd; | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| int eventfd(unsigned int count, int flags) | ||
| { | ||
| return do_eventfd(count, flags); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /* | ||
| * 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__ | ||
|
|
||
| int eventfd(unsigned int count, int flags); | ||
|
|
||
| #endif /* __EVENTFD_H__ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
整个函数的拿锁逻辑有点乱,应该可以代码梳理优化一下。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个拿锁,当数据以阻塞模式读取时,当ctx->计数为0时,需要释放互斥量并等待写入,在rt_wqueue_wait函数调用之前,必须释放锁,让eventfd_write函数可以写。当数据非堵塞方式读的时候,在ctx->count为0时,在return 前直接释放锁,并return。
这里区分堵塞和不堵塞方式,简而言之,就是堵塞的时候释放锁,当工作队列就绪的时候拿锁。不堵塞的时候直接释放锁,并返回。