IOCompletion.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #if compiler(>=6.2) && $Lifetimes
  2. #if os(Linux)
  3. import CSystem
  4. public extension IORing {
  5. struct Completion: ~Copyable {
  6. @inlinable init(rawValue inRawValue: io_uring_cqe) {
  7. rawValue = inRawValue
  8. }
  9. @usableFromInline let rawValue: io_uring_cqe
  10. }
  11. }
  12. public extension IORing.Completion {
  13. struct Flags: OptionSet, Hashable, Codable {
  14. public let rawValue: UInt32
  15. @inlinable public init(rawValue: UInt32) {
  16. self.rawValue = rawValue
  17. }
  18. ///`IORING_CQE_F_BUFFER` Indicates the buffer ID is stored in the upper 16 bits
  19. @inlinable public static var allocatedBuffer: Flags { Flags(rawValue: 1 << 0) }
  20. ///`IORING_CQE_F_MORE` Indicates more completions will be generated from the request that generated this
  21. @inlinable public static var moreCompletions: Flags { Flags(rawValue: 1 << 1) }
  22. //`IORING_CQE_F_SOCK_NONEMPTY`, but currently unused
  23. //@inlinable public static var socketNotEmpty: Flags { Flags(rawValue: 1 << 2) }
  24. //`IORING_CQE_F_NOTIF`, but currently unused
  25. //@inlinable public static var isNotificationEvent: Flags { Flags(rawValue: 1 << 3) }
  26. //IORING_CQE_F_BUF_MORE will eventually be (1U << 4) if we add IOU_PBUF_RING_INC support
  27. }
  28. }
  29. public extension IORing.Completion {
  30. @inlinable var context: UInt64 {
  31. get {
  32. rawValue.user_data
  33. }
  34. }
  35. @inlinable var userPointer: UnsafeRawPointer? {
  36. get {
  37. UnsafeRawPointer(bitPattern: UInt(rawValue.user_data))
  38. }
  39. }
  40. @inlinable var result: Int32 {
  41. get {
  42. rawValue.res
  43. }
  44. }
  45. @inlinable var flags: IORing.Completion.Flags {
  46. get {
  47. Flags(rawValue: rawValue.flags & 0x0000FFFF)
  48. }
  49. }
  50. @inlinable var bufferIndex: UInt16? {
  51. get {
  52. if self.flags.contains(.allocatedBuffer) {
  53. return UInt16(rawValue.flags >> 16)
  54. } else {
  55. return nil
  56. }
  57. }
  58. }
  59. }
  60. #endif // os(Linux)
  61. #endif // compiler(>=6.2) && $Lifetimes