From 65208a7fa269497e85aab688f4d15f83568075d0 Mon Sep 17 00:00:00 2001 From: Ayende Rahien Date: Sat, 14 Aug 2021 06:41:18 +0300 Subject: [PATCH] =?UTF-8?q?Expose=20register=5Feventfd,=20register=5Fevent?= =?UTF-8?q?fd=5Fasync,=20unregister=5Feventfd=20i=E2=80=A6=20(#9449)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Expose register_eventfd, register_eventfd_async, unregister_eventfd in the IO URing API * Fixing formatting * Fixing typo * Removing unnecessary casts and adding better comments for a single registration of eventfd * Update lib/std/os/linux/io_uring.zig Co-authored-by: Joran Dirk Greef * Update lib/std/os/linux/io_uring.zig Co-authored-by: Joran Dirk Greef * Updating util function name Co-authored-by: Joran Dirk Greef --- lib/std/os/linux/io_uring.zig | 46 ++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/std/os/linux/io_uring.zig b/lib/std/os/linux/io_uring.zig index bb20769dc7..b6bde34b7d 100644 --- a/lib/std/os/linux/io_uring.zig +++ b/lib/std/os/linux/io_uring.zig @@ -629,13 +629,57 @@ pub const IO_Uring = struct { /// An application need unregister only if it wants to register a new array of file descriptors. pub fn register_files(self: *IO_Uring, fds: []const os.fd_t) !void { assert(self.fd >= 0); - comptime assert(@sizeOf(os.fd_t) == @sizeOf(c_int)); const res = linux.io_uring_register( self.fd, .REGISTER_FILES, @ptrCast(*const c_void, fds.ptr), @intCast(u32, fds.len), ); + try handle_registration_result(res); + } + + /// Registers the file descriptor for an eventfd that will be notified of completion events on + /// an io_uring instance. + /// Only a single a eventfd can be registered at any given point in time. + pub fn register_eventfd(self: *IO_Uring, fd: os.fd_t) !void { + assert(self.fd >= 0); + const res = linux.io_uring_register( + self.fd, + .REGISTER_EVENTFD, + @ptrCast(*const c_void, &fd), + 1, + ); + try handle_registration_result(res); + } + + /// Registers the file descriptor for an eventfd that will be notified of completion events on + /// an io_uring instance. Notifications are only posted for events that complete in an async manner. + /// This means that events that complete inline while being submitted do not trigger a notification event. + /// Only a single eventfd can be registered at any given point in time. + pub fn register_eventfd_async(self: *IO_Uring, fd: os.fd_t) !void { + assert(self.fd >= 0); + const res = linux.io_uring_register( + self.fd, + .REGISTER_EVENTFD_ASYNC, + @ptrCast(*const c_void, &fd), + 1, + ); + try handle_registration_result(res); + } + + /// Unregister the registered eventfd file descriptor. + pub fn unregister_eventfd(self: *IO_Uring) !void { + assert(self.fd >= 0); + const res = linux.io_uring_register( + self.fd, + .UNREGISTER_EVENTFD, + null, + 0, + ); + try handle_registration_result(res); + } + + fn handle_registration_result(res: usize) !void { switch (linux.getErrno(res)) { 0 => {}, // One or more fds in the array are invalid, or the kernel does not support sparse sets: