Some checks failed
Release CLI / Pre-release build (push) Has been cancelled
Release CLI / Pre-release test (macos-12) (push) Has been cancelled
Release CLI / Pre-release test (ubuntu-22.04) (push) Has been cancelled
Release CLI / Pre-release test (windows-2022) (push) Has been cancelled
Release CLI / Release (push) Has been cancelled
The PR introduces the `platform.Select()` API, wrapping `select(2)` on POSIX and emulated in some cases on Windows. RATIONALE.md contains a full explanation of the approach followed in `poll_oneoff` to handle Stdin and the other types of file descriptors, and the clock subscriptions. It also introduces an abstraction (`StdioFilePoller`) to allow the simulation of different scenarios (waiting for input, input ready, timeout expired, etc.) when unit-testing interactive input. This closes #1317. Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package platform
|
|
|
|
import (
|
|
"syscall"
|
|
"time"
|
|
"unsafe"
|
|
)
|
|
|
|
// syscall_select invokes select on Darwin, with the given timeout Duration.
|
|
// We implement our own version instead of relying on syscall.Select because the latter
|
|
// only returns the error and discards the result.
|
|
func syscall_select(n int, r, w, e *FdSet, timeout *time.Duration) (int, error) {
|
|
var t *syscall.Timeval
|
|
if timeout != nil {
|
|
tv := syscall.NsecToTimeval(timeout.Nanoseconds())
|
|
t = &tv
|
|
}
|
|
result, _, errno := syscall_syscall6(
|
|
libc_select_trampoline_addr,
|
|
uintptr(n),
|
|
uintptr(unsafe.Pointer(r)),
|
|
uintptr(unsafe.Pointer(w)),
|
|
uintptr(unsafe.Pointer(e)),
|
|
uintptr(unsafe.Pointer(t)),
|
|
0)
|
|
res := int(result)
|
|
if errno == 0 {
|
|
return res, nil
|
|
}
|
|
return res, errno
|
|
}
|
|
|
|
// libc_select_trampoline_addr is the address of the
|
|
// `libc_select_trampoline` symbol, defined in `select_darwin.s`.
|
|
//
|
|
// We use this to invoke the syscall through syscall_syscall6 imported below.
|
|
var libc_select_trampoline_addr uintptr
|
|
|
|
// Imports the select symbol from libc as `libc_select`.
|
|
//
|
|
// Note: CGO mechanisms are used in darwin regardless of the CGO_ENABLED value
|
|
// or the "cgo" build flag. See /RATIONALE.md for why.
|
|
//go:cgo_import_dynamic libc_select select "/usr/lib/libSystem.B.dylib"
|