diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-05-20 17:42:34 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2021-05-20 17:42:34 +0200 |
commit | a9b377e9e10eb5194c0bdff32136c11b17253bfd (patch) | |
tree | a71c2e5736b8a948586530c3044e70581014f75b /rwcancel/rwcancel.go | |
parent | 9087e444e6457a21fdb1037b9ed204ac417abc05 (diff) |
rwcancel: use poll instead of select
Suggested-by: Lennart Poettering <lennart@poettering.net>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'rwcancel/rwcancel.go')
-rw-r--r-- | rwcancel/rwcancel.go | 33 |
1 files changed, 12 insertions, 21 deletions
diff --git a/rwcancel/rwcancel.go b/rwcancel/rwcancel.go index 70eb4ca..05484e1 100644 --- a/rwcancel/rwcancel.go +++ b/rwcancel/rwcancel.go @@ -17,13 +17,6 @@ import ( "golang.org/x/sys/unix" ) -func max(a, b int) int { - if a > b { - return a - } - return b -} - type RWCancel struct { fd int closingReader *os.File @@ -50,13 +43,12 @@ func RetryAfterError(err error) bool { } func (rw *RWCancel) ReadyRead() bool { - closeFd := int(rw.closingReader.Fd()) - fdset := fdSet{} - fdset.set(rw.fd) - fdset.set(closeFd) + closeFd := int32(rw.closingReader.Fd()) + + pollFds := []unix.PollFd{{Fd: int32(rw.fd), Events: unix.POLLIN}, {Fd: closeFd, Events: unix.POLLIN}} var err error for { - err = unixSelect(max(rw.fd, closeFd)+1, &fdset.FdSet, nil, nil, nil) + _, err = unix.Poll(pollFds, -1) if err == nil || !RetryAfterError(err) { break } @@ -64,20 +56,18 @@ func (rw *RWCancel) ReadyRead() bool { if err != nil { return false } - if fdset.check(closeFd) { + if pollFds[1].Revents != 0 { return false } - return fdset.check(rw.fd) + return pollFds[0].Revents != 0 } func (rw *RWCancel) ReadyWrite() bool { - closeFd := int(rw.closingReader.Fd()) - fdset := fdSet{} - fdset.set(rw.fd) - fdset.set(closeFd) + closeFd := int32(rw.closingReader.Fd()) + pollFds := []unix.PollFd{{Fd: int32(rw.fd), Events: unix.POLLOUT}, {Fd: closeFd, Events: unix.POLLOUT}} var err error for { - err = unixSelect(max(rw.fd, closeFd)+1, nil, &fdset.FdSet, nil, nil) + _, err = unix.Poll(pollFds, -1) if err == nil || !RetryAfterError(err) { break } @@ -85,10 +75,11 @@ func (rw *RWCancel) ReadyWrite() bool { if err != nil { return false } - if fdset.check(closeFd) { + + if pollFds[1].Revents != 0 { return false } - return fdset.check(rw.fd) + return pollFds[0].Revents != 0 } func (rw *RWCancel) Read(p []byte) (n int, err error) { |