From 75c85d47e7cff5bdc7c5f654a76459bccace5fcc Mon Sep 17 00:00:00 2001 From: Guillaume Lessard Date: Mon, 27 Jul 2026 18:33:21 -0700 Subject: [PATCH] Account for big-endian architectures for poll event encoding --- Sources/System/IORing/IORequest.swift | 2 +- Sources/System/IORing/RawIORequest.swift | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Sources/System/IORing/IORequest.swift b/Sources/System/IORing/IORequest.swift index 72ddcac..dfe2dba 100644 --- a/Sources/System/IORing/IORequest.swift +++ b/Sources/System/IORing/IORequest.swift @@ -609,7 +609,7 @@ extension IORing.Request { if isMultiShot { request.rawValue.len = Self.SWIFT_IORING_POLL_ADD_MULTI } - request.rawValue.poll32_events = pollEvents.rawValue + request.pollEvents = pollEvents } return request diff --git a/Sources/System/IORing/RawIORequest.swift b/Sources/System/IORing/RawIORequest.swift index 1de6013..507fc0d 100644 --- a/Sources/System/IORing/RawIORequest.swift +++ b/Sources/System/IORing/RawIORequest.swift @@ -75,6 +75,27 @@ extension RawIORequest { set { rawValue.addr = newValue } } + /// The poll event mask, stored in `sqe->poll32_events`. + /// + /// Big-endian kernels swap the halfwords of this field before reading it. + /// Equivalent to liburing's `__io_uring_prep_poll_mask`. + @_alwaysEmitIntoClient var pollEvents: IORing.Request.PollEvents { + get { .init(rawValue: _applyPollMask(rawValue.poll32_events)) } + set { rawValue.poll32_events = _applyPollMask(newValue.rawValue) } + } + + /// Converts a poll event mask between its in-memory and `sqe` encodings. + /// + /// This is a halfword rotate (`swahw32` from ``), which keeps + /// the byte order within each half. + @_alwaysEmitIntoClient func _applyPollMask(_ mask: UInt32) -> UInt32 { + #if _endian(big) + return (mask &<< 16) | (mask &>> 16) + #else + return mask + #endif + } + @inlinable public var flags: Flags { get { Flags(rawValue: rawValue.flags) } set { rawValue.flags = newValue.rawValue }