Commit d617bce4 by Ian Lance Taylor

re PR go/56172 (net FAILs on Solaris)

	PR go/56172
net: Fixes for select based pollster.

Make Close work properly, mainly for testing.  Restart the
select if a descriptor is closed.

From-SVN: r195823
parent b96299a1
......@@ -64,7 +64,7 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) {
return false, nil
}
func (p *pollster) DelFD(fd int, mode int) {
func (p *pollster) DelFD(fd int, mode int) bool {
// pollServer is locked.
var kmode int
......@@ -77,6 +77,7 @@ func (p *pollster) DelFD(fd int, mode int) {
// EV_DELETE - delete event from kqueue list
syscall.SetKevent(ev, fd, kmode, syscall.EV_DELETE)
syscall.Kevent(p.kq, p.kbuf[:], nil, nil)
return false
}
func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err error) {
......
......@@ -114,7 +114,7 @@ func (p *pollster) StopWaiting(fd int, bits uint) {
}
}
func (p *pollster) DelFD(fd int, mode int) {
func (p *pollster) DelFD(fd int, mode int) bool {
// pollServer is locked.
if mode == 'r' {
......@@ -133,6 +133,7 @@ func (p *pollster) DelFD(fd int, mode int) {
i++
}
}
return false
}
func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err error) {
......
......@@ -7,6 +7,7 @@
package net
import (
"errors"
"os"
"syscall"
)
......@@ -17,6 +18,7 @@ type pollster struct {
readyReadFds, readyWriteFds *syscall.FdSet
nReady int
lastFd int
closed bool
}
func newpollster() (p *pollster, err error) {
......@@ -35,6 +37,10 @@ func newpollster() (p *pollster, err error) {
func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) {
// pollServer is locked.
if p.closed {
return false, errors.New("pollster closed")
}
if mode == 'r' {
syscall.FDSet(fd, p.readFds)
} else {
......@@ -52,19 +58,23 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) {
return true, nil
}
func (p *pollster) DelFD(fd int, mode int) {
func (p *pollster) DelFD(fd int, mode int) bool {
// pollServer is locked.
if p.closed {
return false
}
if mode == 'r' {
if !syscall.FDIsSet(fd, p.readFds) {
print("Select unexpected fd=", fd, " for read\n")
return
return false
}
syscall.FDClr(fd, p.readFds)
} else {
if !syscall.FDIsSet(fd, p.writeFds) {
print("Select unexpected fd=", fd, " for write\n")
return
return false
}
syscall.FDClr(fd, p.writeFds)
}
......@@ -73,6 +83,8 @@ func (p *pollster) DelFD(fd int, mode int) {
syscall.FDClr(fd, p.repeatFds)
// We don't worry about maxFd here.
return true
}
func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err error) {
......@@ -89,6 +101,10 @@ func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err erro
var e error
var tmpReadFds, tmpWriteFds syscall.FdSet
for {
if p.closed {
return -1, 0, errors.New("pollster closed")
}
// Temporary syscall.FdSet's into which the values are copied
// because select mutates the values.
tmpReadFds = *p.readFds
......@@ -161,5 +177,6 @@ func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err erro
}
func (p *pollster) Close() error {
p.closed = true
return nil
}
......@@ -110,16 +110,24 @@ func (s *pollServer) AddFD(fd *netFD, mode int) error {
// any I/O running on fd. The caller must have locked
// pollserver.
func (s *pollServer) Evict(fd *netFD) {
doWakeup := false
if s.pending[fd.sysfd<<1] == fd {
s.WakeFD(fd, 'r', errClosing)
s.poll.DelFD(fd.sysfd, 'r')
if s.poll.DelFD(fd.sysfd, 'r') {
doWakeup = true
}
delete(s.pending, fd.sysfd<<1)
}
if s.pending[fd.sysfd<<1|1] == fd {
s.WakeFD(fd, 'w', errClosing)
s.poll.DelFD(fd.sysfd, 'w')
if s.poll.DelFD(fd.sysfd, 'w') {
doWakeup = true
}
delete(s.pending, fd.sysfd<<1|1)
}
if doWakeup {
s.Wakeup()
}
}
var wakeupbuf [1]byte
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment