mirror of
https://git.zx2c4.com/wireguard-go
synced 2024-11-15 01:05:15 +01:00
rwcancel: use poll instead of select
Suggested-by: Lennart Poettering <lennart@poettering.net> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
9087e444e6
commit
a9b377e9e1
@ -1,24 +0,0 @@
|
|||||||
// +build !windows
|
|
||||||
|
|
||||||
/* SPDX-License-Identifier: MIT
|
|
||||||
*
|
|
||||||
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package rwcancel
|
|
||||||
|
|
||||||
import "golang.org/x/sys/unix"
|
|
||||||
|
|
||||||
type fdSet struct {
|
|
||||||
unix.FdSet
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fdset *fdSet) set(i int) {
|
|
||||||
bits := 32 << (^uint(0) >> 63)
|
|
||||||
fdset.Bits[i/bits] |= 1 << uint(i%bits)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (fdset *fdSet) check(i int) bool {
|
|
||||||
bits := 32 << (^uint(0) >> 63)
|
|
||||||
return (fdset.Bits[i/bits] & (1 << uint(i%bits))) != 0
|
|
||||||
}
|
|
@ -17,13 +17,6 @@ import (
|
|||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
func max(a, b int) int {
|
|
||||||
if a > b {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
type RWCancel struct {
|
type RWCancel struct {
|
||||||
fd int
|
fd int
|
||||||
closingReader *os.File
|
closingReader *os.File
|
||||||
@ -50,13 +43,12 @@ func RetryAfterError(err error) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rw *RWCancel) ReadyRead() bool {
|
func (rw *RWCancel) ReadyRead() bool {
|
||||||
closeFd := int(rw.closingReader.Fd())
|
closeFd := int32(rw.closingReader.Fd())
|
||||||
fdset := fdSet{}
|
|
||||||
fdset.set(rw.fd)
|
pollFds := []unix.PollFd{{Fd: int32(rw.fd), Events: unix.POLLIN}, {Fd: closeFd, Events: unix.POLLIN}}
|
||||||
fdset.set(closeFd)
|
|
||||||
var err error
|
var err error
|
||||||
for {
|
for {
|
||||||
err = unixSelect(max(rw.fd, closeFd)+1, &fdset.FdSet, nil, nil, nil)
|
_, err = unix.Poll(pollFds, -1)
|
||||||
if err == nil || !RetryAfterError(err) {
|
if err == nil || !RetryAfterError(err) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -64,20 +56,18 @@ func (rw *RWCancel) ReadyRead() bool {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if fdset.check(closeFd) {
|
if pollFds[1].Revents != 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return fdset.check(rw.fd)
|
return pollFds[0].Revents != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rw *RWCancel) ReadyWrite() bool {
|
func (rw *RWCancel) ReadyWrite() bool {
|
||||||
closeFd := int(rw.closingReader.Fd())
|
closeFd := int32(rw.closingReader.Fd())
|
||||||
fdset := fdSet{}
|
pollFds := []unix.PollFd{{Fd: int32(rw.fd), Events: unix.POLLOUT}, {Fd: closeFd, Events: unix.POLLOUT}}
|
||||||
fdset.set(rw.fd)
|
|
||||||
fdset.set(closeFd)
|
|
||||||
var err error
|
var err error
|
||||||
for {
|
for {
|
||||||
err = unixSelect(max(rw.fd, closeFd)+1, nil, &fdset.FdSet, nil, nil)
|
_, err = unix.Poll(pollFds, -1)
|
||||||
if err == nil || !RetryAfterError(err) {
|
if err == nil || !RetryAfterError(err) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -85,10 +75,11 @@ func (rw *RWCancel) ReadyWrite() bool {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if fdset.check(closeFd) {
|
|
||||||
|
if pollFds[1].Revents != 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return fdset.check(rw.fd)
|
return pollFds[0].Revents != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rw *RWCancel) Read(p []byte) (n int, err error) {
|
func (rw *RWCancel) Read(p []byte) (n int, err error) {
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
// +build !linux,!windows
|
|
||||||
|
|
||||||
/* SPDX-License-Identifier: MIT
|
|
||||||
*
|
|
||||||
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package rwcancel
|
|
||||||
|
|
||||||
import "golang.org/x/sys/unix"
|
|
||||||
|
|
||||||
func unixSelect(nfd int, r *unix.FdSet, w *unix.FdSet, e *unix.FdSet, timeout *unix.Timeval) error {
|
|
||||||
_, err := unix.Select(nfd, r, w, e, timeout)
|
|
||||||
return err
|
|
||||||
}
|
|
@ -1,13 +0,0 @@
|
|||||||
/* SPDX-License-Identifier: MIT
|
|
||||||
*
|
|
||||||
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package rwcancel
|
|
||||||
|
|
||||||
import "golang.org/x/sys/unix"
|
|
||||||
|
|
||||||
func unixSelect(nfd int, r *unix.FdSet, w *unix.FdSet, e *unix.FdSet, timeout *unix.Timeval) (err error) {
|
|
||||||
_, err = unix.Select(nfd, r, w, e, timeout)
|
|
||||||
return
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user