github.com/ii64/gouring@v0.4.1/prep.go (about)

     1  package gouring
     2  
     3  import (
     4  	"syscall"
     5  	"unsafe"
     6  )
     7  
     8  func PrepRW(op IoUringOp, sqe *IoUringSqe, fd int,
     9  	addr unsafe.Pointer, len int, offset uint64) {
    10  	sqe.Opcode = op
    11  	sqe.Flags = 0
    12  	sqe.IoPrio = 0
    13  	sqe.Fd = int32(fd)
    14  	sqe.IoUringSqe_Union1 = IoUringSqe_Union1(offset)                    // union1
    15  	sqe.IoUringSqe_Union2 = *(*IoUringSqe_Union2)(unsafe.Pointer(&addr)) // union2
    16  	sqe.Len = uint32(len)
    17  	sqe.IoUringSqe_Union3 = 0 // sqe.SetOpFlags(0) // union3
    18  	sqe.IoUringSqe_Union4 = 0 // sqe.SetBufIndex(0) // union4
    19  	sqe.Personality = 0
    20  	sqe.IoUringSqe_Union5 = 0 // sqe.SetFileIndex(0) // union5
    21  	sqe.Addr3 = 0
    22  	sqe.__pad2[0] = 0
    23  }
    24  
    25  func PrepNop(sqe *IoUringSqe) {
    26  	PrepRW(IORING_OP_NOP, sqe, -1, nil, 0, 0)
    27  }
    28  
    29  func PrepTimeout(sqe *IoUringSqe, ts *syscall.Timespec, count uint32, flags uint32) {
    30  	PrepRW(IORING_OP_TIMEOUT, sqe, -1, unsafe.Pointer(ts), 1, uint64(count))
    31  	sqe.SetTimeoutFlags(flags)
    32  }
    33  
    34  func PrepTimeoutRemove(sqe *IoUringSqe, userDaata uint64, flags uint32) {
    35  	PrepRW(IORING_OP_TIMEOUT_REMOVE, sqe, -1, nil, 0, 0)
    36  	sqe.SetAddr_Value(userDaata)
    37  	sqe.SetTimeoutFlags(flags)
    38  }
    39  
    40  func PrepTimeoutUpdate(sqe *IoUringSqe, ts *syscall.Timespec, userData uint64, flags uint32) {
    41  	PrepRW(IORING_OP_TIMEOUT_REMOVE, sqe, -1, nil, 0, 0)
    42  	sqe.SetAddr_Value(userData)
    43  	sqe.SetTimeoutFlags(flags | IORING_TIMEOUT_UPDATE)
    44  }
    45  
    46  // ** "Syscall" OP
    47  
    48  func PrepRead(sqe *IoUringSqe, fd int, buf *byte, nb int, offset uint64) {
    49  	PrepRW(IORING_OP_READ, sqe, fd, unsafe.Pointer(buf), nb, offset)
    50  }
    51  func PrepReadv(sqe *IoUringSqe, fd int,
    52  	iov *syscall.Iovec, nrVecs int,
    53  	offset uint64) {
    54  	PrepRW(IORING_OP_READV, sqe, fd, unsafe.Pointer(iov), nrVecs, offset)
    55  }
    56  func PrepReadv2(sqe *IoUringSqe, fd int,
    57  	iov *syscall.Iovec, nrVecs int,
    58  	offset uint64, flags uint32) {
    59  	PrepReadv(sqe, fd, iov, nrVecs, offset)
    60  	sqe.SetRwFlags(flags)
    61  }
    62  
    63  func PrepWrite(sqe *IoUringSqe, fd int, buf *byte, nb int, offset uint64) {
    64  	PrepRW(IORING_OP_WRITE, sqe, fd, unsafe.Pointer(buf), nb, offset)
    65  }
    66  func PrepWritev(sqe *IoUringSqe, fd int,
    67  	iov *syscall.Iovec, nrVecs int,
    68  	offset uint64) {
    69  	PrepRW(IORING_OP_WRITEV, sqe, fd, unsafe.Pointer(iov), nrVecs, offset)
    70  }
    71  func PrepWritev2(sqe *IoUringSqe, fd int,
    72  	iov *syscall.Iovec, nrVecs int,
    73  	offset uint64, flags uint32) {
    74  	PrepWritev(sqe, fd, iov, nrVecs, offset)
    75  	sqe.SetRwFlags(flags)
    76  }
    77  
    78  func PrepAccept(sqe *IoUringSqe, fd int, rsa *syscall.RawSockaddrAny, rsaSz *uintptr, flags uint) {
    79  	// *rsaSz = syscall.SizeofSockaddrAny // leave this out to caller?
    80  	PrepRW(IORING_OP_ACCEPT, sqe, fd, unsafe.Pointer(rsa), 0, uint64(uintptr(unsafe.Pointer(rsaSz))))
    81  	sqe.SetAcceptFlags(uint32(flags))
    82  }
    83  
    84  func PrepClose(sqe *IoUringSqe, fd int) {
    85  	PrepRW(IORING_OP_CLOSE, sqe, fd, nil, 0, 0)
    86  }
    87  
    88  func PrepRecvmsg(sqe *IoUringSqe, fd int, msg *syscall.Msghdr, flags uint) {
    89  	PrepRW(IORING_OP_RECVMSG, sqe, fd, unsafe.Pointer(msg), 1, 0)
    90  	sqe.SetMsgFlags(uint32(flags))
    91  }
    92  
    93  func PrepSendmsg(sqe *IoUringSqe, fd int, msg *syscall.Msghdr, flags uint) {
    94  	PrepRW(IORING_OP_SENDMSG, sqe, fd, unsafe.Pointer(msg), 1, 0)
    95  	sqe.SetMsgFlags(uint32(flags))
    96  }
    97  
    98  // ** Multishot
    99  
   100  func PrepMultishotAccept(sqe *IoUringSqe, fd int, rsa *syscall.RawSockaddrAny, rsaSz *uintptr, flags uint) {
   101  	PrepAccept(sqe, fd, rsa, rsaSz, flags)
   102  	sqe.IoPrio |= IORING_ACCEPT_MULTISHOT
   103  }