mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-11 18:32:12 +02:00
Workaround for go unix package not wrapping pselect() on darwin
This commit is contained in:
29
tools/utils/select_posix.go
Normal file
29
tools/utils/select_posix.go
Normal file
@@ -0,0 +1,29 @@
|
||||
//go:build !darwin
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func Select(nfd int, r *unix.FdSet, w *unix.FdSet, e *unix.FdSet, timeout time.Duration) (n int, err error) {
|
||||
deadline := time.Now().Add(timeout)
|
||||
for {
|
||||
t := deadline.Sub(time.Now())
|
||||
if t < 0 {
|
||||
t = 0
|
||||
}
|
||||
ts := NsecToTimespec(t)
|
||||
q, qerr := unix.Pselect(nfd, r, w, w, &ts, nil)
|
||||
if qerr == unix.EINTR {
|
||||
if time.Now().After(deadline) {
|
||||
return 0, os.ErrDeadlineExceeded
|
||||
}
|
||||
continue
|
||||
}
|
||||
return q, qerr
|
||||
}
|
||||
}
|
||||
31
tools/utils/select_without_pselect.go
Normal file
31
tools/utils/select_without_pselect.go
Normal file
@@ -0,0 +1,31 @@
|
||||
//go:build darwin
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Go unix does not wrap pselect on darwin
|
||||
|
||||
func Select(nfd int, r *unix.FdSet, w *unix.FdSet, e *unix.FdSet, timeout time.Duration) (n int, err error) {
|
||||
deadline := time.Now().Add(timeout)
|
||||
for {
|
||||
t := deadline.Sub(time.Now())
|
||||
if t < 0 {
|
||||
t = 0
|
||||
}
|
||||
ts := NsecToTimeval(t)
|
||||
q, qerr := unix.Select(nfd, r, w, w, &ts)
|
||||
if qerr == unix.EINTR {
|
||||
if time.Now().After(deadline) {
|
||||
return 0, os.ErrDeadlineExceeded
|
||||
}
|
||||
continue
|
||||
}
|
||||
return q, qerr
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user