From df9a4e7bf4fc264c4992acc96e8ac737768c291b Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 18 May 2021 17:42:22 -0400 Subject: [PATCH] linux::io_uring: add polling SQEs Signed-off-by: Drew DeVault --- io_uring/sqe.ha | 30 ++++++++++++++++++++++++++++++ diff --git a/io_uring/sqe.ha b/io_uring/sqe.ha index 14492cd066fc6c06d092c45116a7ca174577a711..b6c2070b85a39b43bc2183db9cbace0728aa2ad3 100644 --- a/io_uring/sqe.ha +++ b/io_uring/sqe.ha @@ -1,3 +1,4 @@ +use endian; use rt; use types; @@ -23,6 +24,13 @@ sqe.fd = fd; sqe.addr = addr; sqe.length = length; sqe.off = offs; +}; + +// Sets the user data field of an [[sqe]]. This is copied to the [[cqe]] and can +// be used to correlate a completion event with the original SQE. +export fn setuser(sqe: *sqe, user_data: *void) void = { + static assert(size(uintptr) <= size(u64)); + sqe.user_data = user_data: uintptr: u64; }; // Prepares a no-op "operation" for an [[sqe]]. @@ -123,3 +131,25 @@ ) void = { preprw(sqe, op::FSYNC, fd, null, 0, 0, flags...); sqe.fsync_flags = fsync_flags; }; + +// Adds a request to poll a file descriptor for the given set of poll events. +// This will only happen once, the poll request must be submitted with a new SQE +// to re-poll the file descriptor later. The caller must call [[setuser]] to +// provide a user data field in order to use [[poll_remove]] to remove this poll +// request later. +export fn poll_add( + sqe: *sqe, + fd: int, + poll_mask: uint, + flags: sqe_flags... +) void = { + preprw(sqe, op::POLL_ADD, fd, null, 0, 0, flags...); + assert(endian::host == &endian::little); // TODO? + sqe.poll32_events = poll_mask: u32; +}; + +// Removes an existing poll request by matching the SQE's user_data field. +export fn poll_remove(sqe: *sqe, user_data: *void, flags: sqe_flags...) void = { + preprw(sqe, op::POLL_REMOVE, -1, null, 0, 0, flags...); + setuser(sqe, user_data); +}; -- 2.48.1