github.com/dshulyak/uring@v0.0.0-20210209113719-1b2ec51f1542/ops.go (about)

     1  package uring
     2  
     3  import (
     4  	"syscall"
     5  	"unsafe"
     6  
     7  	"golang.org/x/sys/unix"
     8  )
     9  
    10  // Nop ...
    11  func Nop(sqe *SQEntry) {
    12  	sqe.opcode = IORING_OP_NOP
    13  }
    14  
    15  // Write ...
    16  func Write(sqe *SQEntry, fd uintptr, buf []byte) {
    17  	sqe.opcode = IORING_OP_WRITE
    18  	sqe.fd = int32(fd)
    19  	sqe.addr = (uint64)(uintptr(unsafe.Pointer(&buf[0])))
    20  	sqe.len = uint32(len(buf))
    21  }
    22  
    23  // Read ...
    24  func Read(sqe *SQEntry, fd uintptr, buf []byte) {
    25  	sqe.opcode = IORING_OP_READ
    26  	sqe.fd = int32(fd)
    27  	sqe.addr = (uint64)(uintptr(unsafe.Pointer(&buf[0])))
    28  	sqe.len = uint32(len(buf))
    29  }
    30  
    31  // Writev ...
    32  func Writev(sqe *SQEntry, fd uintptr, iovec []syscall.Iovec, offset uint64, flags uint32) {
    33  	sqe.opcode = IORING_OP_WRITEV
    34  	sqe.fd = int32(fd)
    35  	sqe.len = uint32(len(iovec))
    36  	sqe.offset = offset
    37  	sqe.opcodeFlags = flags
    38  	sqe.addr = (uint64)(uintptr(unsafe.Pointer(&iovec[0])))
    39  }
    40  
    41  // Readv
    42  func Readv(sqe *SQEntry, fd uintptr, iovec []syscall.Iovec, offset uint64, flags uint32) {
    43  	sqe.opcode = IORING_OP_READV
    44  	sqe.fd = int32(fd)
    45  	sqe.len = uint32(len(iovec))
    46  	sqe.offset = offset
    47  	sqe.opcodeFlags = flags
    48  	sqe.addr = (uint64)(uintptr(unsafe.Pointer(&iovec[0])))
    49  }
    50  
    51  // WriteFixed ...
    52  func WriteFixed(sqe *SQEntry, fd uintptr, base *byte, len, offset uint64, flags uint32, bufIndex uint16) {
    53  	sqe.opcode = IORING_OP_WRITE_FIXED
    54  	sqe.fd = int32(fd)
    55  	sqe.len = uint32(len)
    56  	sqe.offset = offset
    57  	sqe.opcodeFlags = flags
    58  	sqe.addr = (uint64)(uintptr(unsafe.Pointer(base)))
    59  	sqe.SetBufIndex(bufIndex)
    60  }
    61  
    62  // ReadFixed ...
    63  func ReadFixed(sqe *SQEntry, fd uintptr, base *byte, len, offset uint64, flags uint32, bufIndex uint16) {
    64  	sqe.opcode = IORING_OP_READ_FIXED
    65  	sqe.fd = int32(fd)
    66  	sqe.len = uint32(len)
    67  	sqe.offset = offset
    68  	sqe.opcodeFlags = flags
    69  	sqe.addr = (uint64)(uintptr(unsafe.Pointer(base)))
    70  	sqe.SetBufIndex(bufIndex)
    71  }
    72  
    73  // Fsync ...
    74  func Fsync(sqe *SQEntry, fd uintptr) {
    75  	sqe.opcode = IORING_OP_FSYNC
    76  	sqe.fd = int32(fd)
    77  }
    78  
    79  // Fdatasync ...
    80  func Fdatasync(sqe *SQEntry, fd uintptr) {
    81  	sqe.opcode = IORING_OP_FSYNC
    82  	sqe.fd = int32(fd)
    83  	sqe.opcodeFlags = IORING_FSYNC_DATASYNC
    84  }
    85  
    86  // Openat
    87  func Openat(sqe *SQEntry, dfd int32, pathptr *byte, flags uint32, mode uint32) {
    88  	sqe.opcode = IORING_OP_OPENAT
    89  	sqe.fd = dfd
    90  	sqe.opcodeFlags = flags
    91  	sqe.addr = (uint64)(uintptr(unsafe.Pointer(pathptr)))
    92  	sqe.len = mode
    93  }
    94  
    95  // Close ...
    96  func Close(sqe *SQEntry, fd uintptr) {
    97  	sqe.opcode = IORING_OP_CLOSE
    98  	sqe.fd = int32(fd)
    99  }
   100  
   101  // Send ...
   102  func Send(sqe *SQEntry, fd uintptr, buf []byte, flags uint32) {
   103  	sqe.SetOpcode(IORING_OP_SEND)
   104  	sqe.SetFD(int32(fd))
   105  	sqe.SetAddr((uint64)(uintptr(unsafe.Pointer(&buf[0]))))
   106  	sqe.SetLen(uint32(len(buf)))
   107  	sqe.SetOpcodeFlags(flags)
   108  }
   109  
   110  // Recv ...
   111  func Recv(sqe *SQEntry, fd uintptr, buf []byte, flags uint32) {
   112  	sqe.SetOpcode(IORING_OP_RECV)
   113  	sqe.SetFD(int32(fd))
   114  	sqe.SetAddr((uint64)(uintptr(unsafe.Pointer(&buf[0]))))
   115  	sqe.SetLen(uint32(len(buf)))
   116  	sqe.SetOpcodeFlags(flags)
   117  }
   118  
   119  // Timeout operation.
   120  // if abs is true then IORING_TIMEOUT_ABS will be added to timeoutFlags.
   121  // count is the number of events to wait.
   122  func Timeout(sqe *SQEntry, ts *unix.Timespec, abs bool, count uint64) {
   123  	sqe.SetFD(-1)
   124  	sqe.SetOpcode(IORING_OP_TIMEOUT)
   125  	sqe.SetAddr((uint64)(uintptr(unsafe.Pointer(ts))))
   126  	sqe.SetLen(1)
   127  	if abs {
   128  		sqe.SetOpcodeFlags(IORING_TIMEOUT_ABS)
   129  	}
   130  	sqe.SetOffset(count)
   131  }
   132  
   133  // LinkTimeout will cancel linked operation if it doesn't complete in time.
   134  func LinkTimeout(sqe *SQEntry, ts *unix.Timespec, abs bool) {
   135  	sqe.SetFD(-1)
   136  	sqe.SetOpcode(IORING_OP_LINK_TIMEOUT)
   137  	sqe.SetAddr((uint64)(uintptr(unsafe.Pointer(ts))))
   138  	sqe.SetLen(1)
   139  	if abs {
   140  		sqe.SetOpcodeFlags(IORING_TIMEOUT_ABS)
   141  	}
   142  }