Skip to content

Commit 8b65ece

Browse files
committed
add SetupFlags option set for configuring flags
1 parent 4ff8328 commit 8b65ece

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

Sources/IORing/IORing.swift

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public actor IORingActor {
4040
public final class IORing: CustomStringConvertible {
4141
public typealias Offset = Int64 // for 64-bit offsetes on 32-bit platforms
4242

43-
public nonisolated static let shared = try! IORing(entries: nil, flags: 0, shared: true)
43+
public nonisolated static let shared = try! IORing(entries: nil, flags: [], shared: true)
4444

4545
private nonisolated static let DefaultIORingQueueEntries = 128
4646

@@ -133,7 +133,7 @@ public final class IORing: CustomStringConvertible {
133133
}
134134
}
135135

136-
struct SqeFlags: OptionSet {
136+
struct SqeFlags: OptionSet, Sendable {
137137
typealias RawValue = UInt8
138138

139139
let rawValue: RawValue
@@ -159,6 +159,35 @@ public final class IORing: CustomStringConvertible {
159159
static let cqeSkipSuccess = SqeFlags(rawValue: 1 << 6) // IOSQE_CQE_SKIP_SUCCESS_BIT
160160
}
161161

162+
public struct SetupFlags: OptionSet, Sendable {
163+
public typealias RawValue = UInt32
164+
165+
public let rawValue: RawValue
166+
167+
public init(rawValue: RawValue) {
168+
self.rawValue = rawValue
169+
}
170+
171+
public static let ioPoll = SetupFlags(rawValue: 1 << 0) // IORING_SETUP_IOPOLL
172+
public static let sqPoll = SetupFlags(rawValue: 1 << 1) // IORING_SETUP_SQPOLL
173+
public static let sqAff = SetupFlags(rawValue: 1 << 2) // IORING_SETUP_SQ_AFF
174+
public static let cqSize = SetupFlags(rawValue: 1 << 3) // IORING_SETUP_CQSIZE
175+
public static let clamp = SetupFlags(rawValue: 1 << 4) // IORING_SETUP_CLAMP
176+
public static let attachWq = SetupFlags(rawValue: 1 << 5) // IORING_SETUP_ATTACH_WQ
177+
public static let rDisabled = SetupFlags(rawValue: 1 << 6) // IORING_SETUP_R_DISABLED
178+
public static let submitAll = SetupFlags(rawValue: 1 << 7) // IORING_SETUP_SUBMIT_ALL
179+
public static let coopTaskRun = SetupFlags(rawValue: 1 << 8) // IORING_SETUP_COOP_TASKRUN
180+
public static let taskRunFlag = SetupFlags(rawValue: 1 << 9) // IORING_SETUP_TASKRUN_FLAG
181+
public static let sqe128 = SetupFlags(rawValue: 1 << 10) // IORING_SETUP_SQE128
182+
public static let cqe32 = SetupFlags(rawValue: 1 << 11) // IORING_SETUP_CQE32
183+
public static let singleIssuer = SetupFlags(rawValue: 1 << 12) // IORING_SETUP_SINGLE_ISSUER
184+
public static let deferTaskRun = SetupFlags(rawValue: 1 << 13) // IORING_SETUP_DEFER_TASKRUN
185+
public static let noMmap = SetupFlags(rawValue: 1 << 14) // IORING_SETUP_NO_MMAP
186+
public static let registeredFdOnly =
187+
SetupFlags(rawValue: 1 << 15) // IORING_SETUP_REGISTERED_FD_ONLY
188+
public static let noSqArray = SetupFlags(rawValue: 1 << 16) // IORING_SETUP_NO_SQARRAY
189+
}
190+
162191
private struct AcceptIoPrio: OptionSet {
163192
typealias RawValue = UInt16
164193

@@ -194,21 +223,29 @@ public final class IORing: CustomStringConvertible {
194223
}
195224
}
196225

197-
public convenience nonisolated init(entries: Int? = nil, flags: UInt32 = 0) throws {
226+
public convenience nonisolated init(entries: Int? = nil, flags: SetupFlags = []) throws {
198227
try self.init(entries: entries, flags: flags, shared: false)
199228
}
200229

201-
private nonisolated init(entries: Int?, flags: UInt32, shared: Bool) throws {
230+
public convenience nonisolated init(entries: Int? = nil, flags: UInt32) throws {
231+
try self.init(entries: entries, flags: SetupFlags(rawValue: flags), shared: false)
232+
}
233+
234+
private nonisolated init(entries: Int?, flags: SetupFlags, shared: Bool) throws {
202235
let entries = entries ?? IORing.getIORingQueueEntries()
203236
var ring = io_uring()
204237
var params = io_uring_params()
238+
var flags = flags
239+
240+
flags.remove(.attachWq)
205241

206-
params.flags = flags & ~IORING_SETUP_ATTACH_WQ
207242
if !shared {
208-
params.flags |= IORING_SETUP_ATTACH_WQ
243+
flags.insert(.attachWq)
209244
params.wq_fd = UInt32(IORing.shared.ringFd)
210245
}
211246

247+
params.flags = flags.rawValue
248+
212249
try Errno.throwingErrno {
213250
io_uring_queue_init_params(CUnsignedInt(entries), &ring, &params)
214251
}

0 commit comments

Comments
 (0)