github.com/pawelgaczynski/giouring@v0.0.0-20230826085535-69588b89acb9/syscall.go (about)

     1  // MIT License
     2  //
     3  // Copyright (c) 2023 Paweł Gaczyński
     4  //
     5  // Permission is hereby granted, free of charge, to any person obtaining a
     6  // copy of this software and associated documentation files (the
     7  // "Software"), to deal in the Software without restriction, including
     8  // without limitation the rights to use, copy, modify, merge, publish,
     9  // distribute, sublicense, and/or sell copies of the Software, and to
    10  // permit persons to whom the Software is furnished to do so, subject to
    11  // the following conditions:
    12  //
    13  // The above copyright notice and this permission notice shall be included
    14  // in all copies or substantial portions of the Software.
    15  //
    16  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    17  // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    18  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    19  // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    20  // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    21  // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    22  // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    23  
    24  package giouring
    25  
    26  import (
    27  	"runtime"
    28  	"syscall"
    29  	"unsafe"
    30  )
    31  
    32  /*
    33   * io_uring_enter - https://manpages.debian.org/unstable/liburing-dev/io_uring_enter.2.en.html
    34   */
    35  func (ring *Ring) Enter(submitted uint32, waitNr uint32, flags uint32, sig unsafe.Pointer) (uint, error) {
    36  	return ring.Enter2(submitted, waitNr, flags, sig, nSig/szDivider)
    37  }
    38  
    39  /*
    40   * io_uring_enter2 - https://manpages.debian.org/unstable/liburing-dev/io_uring_enter.2.en.html
    41   */
    42  func (ring *Ring) Enter2(
    43  	submitted uint32,
    44  	waitNr uint32,
    45  	flags uint32,
    46  	sig unsafe.Pointer,
    47  	size int,
    48  ) (uint, error) {
    49  	var (
    50  		consumed uintptr
    51  		errno    syscall.Errno
    52  	)
    53  
    54  	consumed, _, errno = syscall.Syscall6(
    55  		sysEnter,
    56  		uintptr(ring.enterRingFd),
    57  		uintptr(submitted),
    58  		uintptr(waitNr),
    59  		uintptr(flags),
    60  		uintptr(sig),
    61  		uintptr(size),
    62  	)
    63  
    64  	if errno > 0 {
    65  		return 0, errno
    66  	}
    67  
    68  	return uint(consumed), nil
    69  }
    70  
    71  // liburing: io_uring_setup - https://manpages.debian.org/unstable/liburing-dev/io_uring_setup.2.en.html
    72  func Setup(entries uint32, p *Params) (uint, error) {
    73  	fd, _, errno := syscall.Syscall(sysSetup, uintptr(entries), uintptr(unsafe.Pointer(p)), 0)
    74  	runtime.KeepAlive(p)
    75  
    76  	return uint(fd), errno
    77  }
    78  
    79  func syscallRegister(fd int, opcode uint32, arg unsafe.Pointer, nrArgs uint32) (uint, syscall.Errno) {
    80  	returnFirst, _, errno := syscall.Syscall6(
    81  		sysRegister,
    82  		uintptr(fd),
    83  		uintptr(opcode),
    84  		uintptr(arg),
    85  		uintptr(nrArgs),
    86  		0,
    87  		0,
    88  	)
    89  
    90  	return uint(returnFirst), errno
    91  }
    92  
    93  // liburing: io_uring_register - https://manpages.debian.org/unstable/liburing-dev/io_uring_register.2.en.html
    94  func (ring *Ring) Register(fd int, opcode uint32, arg unsafe.Pointer, nrArgs uint32) (uint, syscall.Errno) {
    95  	return syscallRegister(fd, opcode, arg, nrArgs)
    96  }