-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add io_uring event loop backend #813
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
Changes from 4 commits
ca703bf
5cada92
e7acef2
6113c31
95c4a6d
a717320
10aba51
bcb6f28
800e971
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,7 @@ dist | |
| test | ||
| *_test | ||
| build | ||
| build-* | ||
| config.mk | ||
| hconfig.h | ||
| html/uploads | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -193,6 +193,10 @@ endif | |||||||||||||||||||||
| endif | ||||||||||||||||||||||
| endif | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ifeq ($(WITH_IO_URING), yes) | ||||||||||||||||||||||
| LDFLAGS += -luring | ||||||||||||||||||||||
| endif | ||||||||||||||||||||||
|
Comment on lines
199
to
+203
|
||||||||||||||||||||||
| ifeq ($(WITH_IO_URING), yes) | |
| LDFLAGS += -luring | |
| endif | |
| ifeq ($(OS), Linux) | |
| ifeq ($(WITH_IO_URING), yes) | |
| LDFLAGS += -luring | |
| endif | |
| endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,3 +38,6 @@ WITH_MBEDTLS=no | |
|
|
||
| # rudp | ||
| WITH_KCP=no | ||
|
|
||
| # event | ||
| WITH_IO_URING=no | ||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,209 @@ | ||||||||||||
| #include "iowatcher.h" | ||||||||||||
|
|
||||||||||||
| #ifdef EVENT_IO_URING | ||||||||||||
| #include "hplatform.h" | ||||||||||||
| #include "hdef.h" | ||||||||||||
| #include "hevent.h" | ||||||||||||
|
|
||||||||||||
| #include <liburing.h> | ||||||||||||
| #include <poll.h> | ||||||||||||
|
|
||||||||||||
| #define IO_URING_ENTRIES 1024 | ||||||||||||
| #define IO_URING_CANCEL_TAG UINT64_MAX | ||||||||||||
|
|
||||||||||||
| typedef struct io_uring_ctx_s { | ||||||||||||
| struct io_uring ring; | ||||||||||||
| int nfds; | ||||||||||||
| } io_uring_ctx_t; | ||||||||||||
|
|
||||||||||||
| int iowatcher_init(hloop_t* loop) { | ||||||||||||
| if (loop->iowatcher) return 0; | ||||||||||||
| io_uring_ctx_t* ctx; | ||||||||||||
| HV_ALLOC_SIZEOF(ctx); | ||||||||||||
| int ret = io_uring_queue_init(IO_URING_ENTRIES, &ctx->ring, 0); | ||||||||||||
| if (ret < 0) { | ||||||||||||
| HV_FREE(ctx); | ||||||||||||
| return ret; | ||||||||||||
| } | ||||||||||||
| ctx->nfds = 0; | ||||||||||||
| loop->iowatcher = ctx; | ||||||||||||
| return 0; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| int iowatcher_cleanup(hloop_t* loop) { | ||||||||||||
| if (loop->iowatcher == NULL) return 0; | ||||||||||||
| io_uring_ctx_t* ctx = (io_uring_ctx_t*)loop->iowatcher; | ||||||||||||
| io_uring_queue_exit(&ctx->ring); | ||||||||||||
| HV_FREE(loop->iowatcher); | ||||||||||||
| return 0; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| int iowatcher_add_event(hloop_t* loop, int fd, int events) { | ||||||||||||
| if (loop->iowatcher == NULL) { | ||||||||||||
| iowatcher_init(loop); | ||||||||||||
|
||||||||||||
| iowatcher_init(loop); | |
| int ret = iowatcher_init(loop); | |
| if (ret < 0) { | |
| return ret; | |
| } |
Copilot
AI
Mar 11, 2026
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.
When io_uring_get_sqe returns NULL, this function returns -1, but callers (e.g., hio_add) don't check the return value and will still set io->events. This can silently leave an fd unarmed. Consider ensuring SQE availability (submit/flush + retry, or increase ring size) and/or changing the call path to only update io->events on success.
Copilot
AI
Mar 11, 2026
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.
iowatcher_del_event returns -1 when io_uring_get_sqe fails, but hio_del doesn't check this and will still clear io->events. That can desynchronize the loop's idea of what is armed vs. what's actually registered in io_uring; handle SQE exhaustion here (submit/flush + retry) and avoid returning an unhandled error.
Copilot
AI
Mar 11, 2026
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.
On cqe->res < 0 (poll request failed), the code sets only HV_READ. If the fd is registered for write-only (e.g. connect uses HV_WRITE), the pending callback will run but hio_handle_events will ignore the event, potentially stalling the connection/error handling. Consider mapping this failure to io->revents |= (io->events ? io->events : HV_RDWR) (or at least set both READ and WRITE) so the appropriate handler runs.
| io->revents |= HV_READ; | |
| unsigned ev = io->events ? io->events : HV_RDWR; | |
| io->revents |= ev; |
Copilot
AI
Mar 11, 2026
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.
io_uring_submit is only called when nevents > 0, but this loop can queue re-arm SQEs even when no HV_READ/HV_WRITE bits were set (e.g., if cqe->res contains only poll bits you don't map to hv events). In that case the re-arm SQEs remain unsubmitted and the loop can block with nfds > 0 but no active polls. Track whether any SQEs were queued and submit based on that instead of nevents.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,5 +98,6 @@ | |
|
|
||
| #cmakedefine WITH_WEPOLL 1 | ||
| #cmakedefine WITH_KCP 1 | ||
| #cmakedefine WITH_IO_URING 1 | ||
|
|
||
| #endif // HV_CONFIG_H_ | ||
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.
LIBSunconditionally appendsuringwhenWITH_IO_URINGis ON. Since liburing is Linux-only, enabling this option on non-Linux platforms will fail at link time; consider guarding this with a Linux check (e.g.if(CMAKE_SYSTEM_NAME STREQUAL "Linux")) and emitting a clear configuration error otherwise.