zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 65208a7fa269497e85aab688f4d15f83568075d0 (tree)
parent 1373df4c34436b18b570f5b0f1be14da63966557
Author: Ayende Rahien <ayende@ayende.com>
Date:   Sat, 14 Aug 2021 06:41:18 +0300

Expose register_eventfd, register_eventfd_async, unregister_eventfd i… (#9449)

* 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 <joran@coil.com>

* Update lib/std/os/linux/io_uring.zig

Co-authored-by: Joran Dirk Greef <joran@coil.com>

* Updating util function name

Co-authored-by: Joran Dirk Greef <joran@coil.com>
Diffstat:
Mlib/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 @@ -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: