Commit 6a76bca3 authored by Mikio Hara's avatar Mikio Hara

net: separate pollster initialization from network file descriptor allocation

Unlike the existing net package own pollster, runtime-integrated
network pollster on BSD variants, actually kqueue, requires a socket
that has beed passed to syscall.Listen previously for a stream
listener.

This CL separates pollDesc.Init (actually runtime_pollOpen) from newFD
to allow control of each state of sockets and adds init method to netFD
instead. Upcoming CLs will rearrange the call order of runtime-integrated
pollster and syscall functions like the following;

- For dialers that open active connections, runtime_pollOpen will be
  called in between syscall.Bind and syscall.Connect.

- For stream listeners that open passive stream connections,
  runtime_pollOpen will be called just after syscall.Listen.

- For datagram listeners that open datagram connections,
  runtime_pollOpen will be called just after syscall.Bind.

This is in preparation for runtime-integrated network pollster for BSD
variants.

Update #5199

R=dvyukov, alex.brainman, minux.ma
CC=golang-dev
https://golang.org/cl/8608044
parent 33bd9694
...@@ -38,7 +38,11 @@ func (pd *pollDesc) Init(fd *netFD) error { ...@@ -38,7 +38,11 @@ func (pd *pollDesc) Init(fd *netFD) error {
} }
func (pd *pollDesc) Close() { func (pd *pollDesc) Close() {
if pd.runtimeCtx == 0 {
return
}
runtime_pollClose(pd.runtimeCtx) runtime_pollClose(pd.runtimeCtx)
pd.runtimeCtx = 0
} }
func (pd *pollDesc) Lock() { func (pd *pollDesc) Lock() {
...@@ -53,6 +57,9 @@ func (pd *pollDesc) Wakeup() { ...@@ -53,6 +57,9 @@ func (pd *pollDesc) Wakeup() {
// Evict evicts fd from the pending list, unblocking any I/O running on fd. // Evict evicts fd from the pending list, unblocking any I/O running on fd.
// Return value is whether the pollServer should be woken up. // Return value is whether the pollServer should be woken up.
func (pd *pollDesc) Evict() bool { func (pd *pollDesc) Evict() bool {
if pd.runtimeCtx == 0 {
return false
}
runtime_pollUnblock(pd.runtimeCtx) runtime_pollUnblock(pd.runtimeCtx)
return false return false
} }
......
...@@ -55,17 +55,15 @@ func resolveAndDial(net, addr string, localAddr Addr, deadline time.Time) (Conn, ...@@ -55,17 +55,15 @@ func resolveAndDial(net, addr string, localAddr Addr, deadline time.Time) (Conn,
return dial(net, addr, localAddr, ra, deadline) return dial(net, addr, localAddr, ra, deadline)
} }
func newFD(fd, family, sotype int, net string) (*netFD, error) { func newFD(sysfd, family, sotype int, net string) (*netFD, error) {
netfd := &netFD{ return &netFD{sysfd: sysfd, family: family, sotype: sotype, net: net}, nil
sysfd: fd, }
family: family,
sotype: sotype, func (fd *netFD) init() error {
net: net, if err := fd.pd.Init(fd); err != nil {
} return err
if err := netfd.pd.Init(netfd); err != nil {
return nil, err
} }
return netfd, nil return nil
} }
func (fd *netFD) setAddr(laddr, raddr Addr) { func (fd *netFD) setAddr(laddr, raddr Addr) {
...@@ -401,6 +399,10 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (netfd *netFD, err e ...@@ -401,6 +399,10 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (netfd *netFD, err e
closesocket(s) closesocket(s)
return nil, err return nil, err
} }
if err = netfd.init(); err != nil {
fd.Close()
return nil, err
}
lsa, _ := syscall.Getsockname(netfd.sysfd) lsa, _ := syscall.Getsockname(netfd.sysfd)
netfd.setAddr(toAddr(lsa), toAddr(rsa)) netfd.setAddr(toAddr(lsa), toAddr(rsa))
return netfd, nil return netfd, nil
......
...@@ -242,14 +242,12 @@ func newFD(sysfd syscall.Handle, family, sotype int, net string) (*netFD, error) ...@@ -242,14 +242,12 @@ func newFD(sysfd syscall.Handle, family, sotype int, net string) (*netFD, error)
return nil, initErr return nil, initErr
} }
onceStartServer.Do(startServer) onceStartServer.Do(startServer)
fd := &netFD{ return &netFD{sysfd: sysfd, family: family, sotype: sotype, net: net}, nil
sysfd: sysfd, }
family: family,
sotype: sotype, func (fd *netFD) init() error {
net: net,
}
if err := fd.pd.Init(fd); err != nil { if err := fd.pd.Init(fd); err != nil {
return nil, err return err
} }
fd.rop.mode = 'r' fd.rop.mode = 'r'
fd.wop.mode = 'w' fd.wop.mode = 'w'
...@@ -261,7 +259,7 @@ func newFD(sysfd syscall.Handle, family, sotype int, net string) (*netFD, error) ...@@ -261,7 +259,7 @@ func newFD(sysfd syscall.Handle, family, sotype int, net string) (*netFD, error)
fd.rop.errc = make(chan error) fd.rop.errc = make(chan error)
fd.rop.errc = make(chan error) fd.rop.errc = make(chan error)
} }
return fd, nil return nil
} }
func (fd *netFD) setAddr(laddr, raddr Addr) { func (fd *netFD) setAddr(laddr, raddr Addr) {
...@@ -473,6 +471,10 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (*netFD, error) { ...@@ -473,6 +471,10 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (*netFD, error) {
closesocket(s) closesocket(s)
return nil, &OpError{"accept", fd.net, fd.laddr, err} return nil, &OpError{"accept", fd.net, fd.laddr, err}
} }
if err := netfd.init(); err != nil {
fd.Close()
return nil, err
}
// Submit accept request. // Submit accept request.
o := &fd.rop o := &fd.rop
......
...@@ -67,6 +67,10 @@ func newFileFD(f *os.File) (*netFD, error) { ...@@ -67,6 +67,10 @@ func newFileFD(f *os.File) (*netFD, error) {
closesocket(fd) closesocket(fd)
return nil, err return nil, err
} }
if err := netfd.init(); err != nil {
netfd.Close()
return nil, err
}
netfd.setAddr(laddr, raddr) netfd.setAddr(laddr, raddr)
return netfd, nil return netfd, nil
} }
......
...@@ -93,6 +93,10 @@ func socket(net string, f, t, p int, ipv6only bool, laddr, raddr sockaddr, deadl ...@@ -93,6 +93,10 @@ func socket(net string, f, t, p int, ipv6only bool, laddr, raddr sockaddr, deadl
closesocket(s) closesocket(s)
return nil, err return nil, err
} }
if err := fd.init(); err != nil {
fd.Close()
return nil, err
}
var rsa syscall.Sockaddr var rsa syscall.Sockaddr
if raddr != nil { if raddr != nil {
......
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