1
0
mirror of https://github.com/golang/go synced 2024-11-21 15:44:44 -07:00

net: reuse channels during io

R=golang-dev, bsiegert, rsc, hectorchu
CC=golang-dev
https://golang.org/cl/5016043
This commit is contained in:
Alex Brainman 2011-09-16 13:40:02 +10:00
parent 96f9e8837e
commit 310e5fe60c
2 changed files with 33 additions and 19 deletions

View File

@ -52,15 +52,27 @@ type anOp struct {
// of the struct, as our code rely on it. // of the struct, as our code rely on it.
o syscall.Overlapped o syscall.Overlapped
resultc chan ioResult // io completion results resultc chan ioResult
errnoc chan int // io submit / cancel operation errors errnoc chan int
fd *netFD fd *netFD
} }
func (o *anOp) Init(fd *netFD) { func (o *anOp) Init(fd *netFD, mode int) {
o.fd = fd o.fd = fd
o.resultc = make(chan ioResult, 1) var i int
o.errnoc = make(chan int) if mode == 'r' {
i = 0
} else {
i = 1
}
if fd.resultc[i] == nil {
fd.resultc[i] = make(chan ioResult, 1)
}
o.resultc = fd.resultc[i]
if fd.errnoc[i] == nil {
fd.errnoc[i] = make(chan int)
}
o.errnoc = fd.errnoc[i]
} }
func (o *anOp) Op() *anOp { func (o *anOp) Op() *anOp {
@ -74,8 +86,8 @@ type bufOp struct {
buf syscall.WSABuf buf syscall.WSABuf
} }
func (o *bufOp) Init(fd *netFD, buf []byte) { func (o *bufOp) Init(fd *netFD, buf []byte, mode int) {
o.anOp.Init(fd) o.anOp.Init(fd, mode)
o.buf.Len = uint32(len(buf)) o.buf.Len = uint32(len(buf))
if len(buf) == 0 { if len(buf) == 0 {
o.buf.Buf = nil o.buf.Buf = nil
@ -208,12 +220,14 @@ type netFD struct {
closing bool closing bool
// immutable until Close // immutable until Close
sysfd syscall.Handle sysfd syscall.Handle
family int family int
proto int proto int
net string net string
laddr Addr laddr Addr
raddr Addr raddr Addr
resultc [2]chan ioResult // read/write completion results
errnoc [2]chan int // read/write submit or cancel operation errors
// owned by client // owned by client
rdeadline_delta int64 rdeadline_delta int64
@ -325,7 +339,7 @@ func (fd *netFD) Read(buf []byte) (n int, err os.Error) {
return 0, os.EINVAL return 0, os.EINVAL
} }
var o readOp var o readOp
o.Init(fd, buf) o.Init(fd, buf, 'r')
n, err = iosrv.ExecIO(&o, fd.rdeadline_delta) n, err = iosrv.ExecIO(&o, fd.rdeadline_delta)
if err == nil && n == 0 { if err == nil && n == 0 {
err = os.EOF err = os.EOF
@ -365,7 +379,7 @@ func (fd *netFD) ReadFrom(buf []byte) (n int, sa syscall.Sockaddr, err os.Error)
return 0, nil, os.EINVAL return 0, nil, os.EINVAL
} }
var o readFromOp var o readFromOp
o.Init(fd, buf) o.Init(fd, buf, 'r')
o.rsan = int32(unsafe.Sizeof(o.rsa)) o.rsan = int32(unsafe.Sizeof(o.rsa))
n, err = iosrv.ExecIO(&o, fd.rdeadline_delta) n, err = iosrv.ExecIO(&o, fd.rdeadline_delta)
if err != nil { if err != nil {
@ -402,7 +416,7 @@ func (fd *netFD) Write(buf []byte) (n int, err os.Error) {
return 0, os.EINVAL return 0, os.EINVAL
} }
var o writeOp var o writeOp
o.Init(fd, buf) o.Init(fd, buf, 'w')
return iosrv.ExecIO(&o, fd.wdeadline_delta) return iosrv.ExecIO(&o, fd.wdeadline_delta)
} }
@ -437,7 +451,7 @@ func (fd *netFD) WriteTo(buf []byte, sa syscall.Sockaddr) (n int, err os.Error)
return 0, os.EINVAL return 0, os.EINVAL
} }
var o writeToOp var o writeToOp
o.Init(fd, buf) o.Init(fd, buf, 'w')
o.sa = sa o.sa = sa
return iosrv.ExecIO(&o, fd.wdeadline_delta) return iosrv.ExecIO(&o, fd.wdeadline_delta)
} }
@ -487,7 +501,7 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err os.
// Submit accept request. // Submit accept request.
var o acceptOp var o acceptOp
o.Init(fd) o.Init(fd, 'r')
o.newsock = s o.newsock = s
_, err = iosrv.ExecIO(&o, 0) _, err = iosrv.ExecIO(&o, 0)
if err != nil { if err != nil {

View File

@ -54,7 +54,7 @@ func sendFile(c *netFD, r io.Reader) (written int64, err os.Error, handled bool)
defer c.decref() defer c.decref()
var o sendfileOp var o sendfileOp
o.Init(c) o.Init(c, 'w')
o.n = uint32(n) o.n = uint32(n)
o.src = f.Fd() o.src = f.Fd()
done, err := iosrv.ExecIO(&o, 0) done, err := iosrv.ExecIO(&o, 0)