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>
32 lines
1.3 KiB
Go
32 lines
1.3 KiB
Go
package platform
|
|
|
|
import "time"
|
|
|
|
// Select exposes the select(2) syscall.
|
|
//
|
|
// # Notes on Parameters
|
|
//
|
|
// For convenience, we expose a pointer to a time.Duration instead of a pointer to a syscall.Timeval.
|
|
// It must be a pointer because `nil` means "wait forever".
|
|
//
|
|
// However, notice that select(2) may mutate the pointed Timeval on some platforms,
|
|
// for instance if the call returns early.
|
|
//
|
|
// This implementation *will not* update the pointed time.Duration value accordingly.
|
|
//
|
|
// See also: https://github.com/golang/sys/blob/master/unix/syscall_unix_test.go#L606-L617
|
|
//
|
|
// # Notes on the Syscall
|
|
//
|
|
// Because this is a blocking syscall, it will also block the carrier thread of the goroutine,
|
|
// preventing any means to support context cancellation directly.
|
|
//
|
|
// There are ways to obviate this issue. We outline here one idea, that is however not currently implemented.
|
|
// A common approach to support context cancellation is to add a signal file descriptor to the set,
|
|
// e.g. the read-end of a pipe or an eventfd on Linux.
|
|
// When the context is canceled, we may unblock a Select call by writing to the fd, causing it to return immediately.
|
|
// This however requires to do a bit of housekeeping to hide the "special" FD from the end-user.
|
|
func Select(n int, r, w, e *FdSet, timeout *time.Duration) (int, error) {
|
|
return syscall_select(n, r, w, e, timeout)
|
|
}
|