1
0
mirror of https://github.com/golang/go synced 2024-11-17 23:04:56 -07:00

net: Use preallocated buffer for kqueue/kevent.

R=rsc
CC=golang-dev
https://golang.org/cl/4293056
This commit is contained in:
Ian Lance Taylor 2011-03-18 12:48:12 -07:00
parent 3be0921e73
commit 000d60ac6f
2 changed files with 24 additions and 12 deletions

View File

@ -15,6 +15,10 @@ type pollster struct {
kq int
eventbuf [10]syscall.Kevent_t
events []syscall.Kevent_t
// An event buffer for AddFD/DelFD.
// Must hold pollServer lock.
kbuf [1]syscall.Kevent_t
}
func newpollster() (p *pollster, err os.Error) {
@ -28,14 +32,15 @@ func newpollster() (p *pollster, err os.Error) {
}
func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
// pollServer is locked.
var kmode int
if mode == 'r' {
kmode = syscall.EVFILT_READ
} else {
kmode = syscall.EVFILT_WRITE
}
var events [1]syscall.Kevent_t
ev := &events[0]
ev := &p.kbuf[0]
// EV_ADD - add event to kqueue list
// EV_RECEIPT - generate fake EV_ERROR as result of add,
// rather than waiting for real event
@ -46,7 +51,7 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
}
syscall.SetKevent(ev, fd, kmode, flags)
n, e := syscall.Kevent(p.kq, events[0:], events[0:], nil)
n, e := syscall.Kevent(p.kq, p.kbuf[0:], p.kbuf[0:], nil)
if e != 0 {
return os.NewSyscallError("kevent", e)
}
@ -60,19 +65,20 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
}
func (p *pollster) DelFD(fd int, mode int) {
// pollServer is locked.
var kmode int
if mode == 'r' {
kmode = syscall.EVFILT_READ
} else {
kmode = syscall.EVFILT_WRITE
}
var events [1]syscall.Kevent_t
ev := &events[0]
ev := &p.kbuf[0]
// EV_DELETE - delete event from kqueue list
// EV_RECEIPT - generate fake EV_ERROR as result of add,
// rather than waiting for real event
syscall.SetKevent(ev, fd, kmode, syscall.EV_DELETE|syscall.EV_RECEIPT)
syscall.Kevent(p.kq, events[0:], events[0:], nil)
syscall.Kevent(p.kq, p.kbuf[0:], p.kbuf[0:], nil)
}
func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err os.Error) {

View File

@ -15,6 +15,10 @@ type pollster struct {
kq int
eventbuf [10]syscall.Kevent_t
events []syscall.Kevent_t
// An event buffer for AddFD/DelFD.
// Must hold pollServer lock.
kbuf [1]syscall.Kevent_t
}
func newpollster() (p *pollster, err os.Error) {
@ -28,14 +32,15 @@ func newpollster() (p *pollster, err os.Error) {
}
func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
// pollServer is locked.
var kmode int
if mode == 'r' {
kmode = syscall.EVFILT_READ
} else {
kmode = syscall.EVFILT_WRITE
}
var events [1]syscall.Kevent_t
ev := &events[0]
ev := &p.kbuf[0]
// EV_ADD - add event to kqueue list
// EV_ONESHOT - delete the event the first time it triggers
flags := syscall.EV_ADD
@ -44,7 +49,7 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
}
syscall.SetKevent(ev, fd, kmode, flags)
n, e := syscall.Kevent(p.kq, events[:], nil, nil)
n, e := syscall.Kevent(p.kq, p.kbuf[:], nil, nil)
if e != 0 {
return os.NewSyscallError("kevent", e)
}
@ -58,17 +63,18 @@ func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
}
func (p *pollster) DelFD(fd int, mode int) {
// pollServer is locked.
var kmode int
if mode == 'r' {
kmode = syscall.EVFILT_READ
} else {
kmode = syscall.EVFILT_WRITE
}
var events [1]syscall.Kevent_t
ev := &events[0]
ev := &p.kbuf[0]
// EV_DELETE - delete event from kqueue list
syscall.SetKevent(ev, fd, kmode, syscall.EV_DELETE)
syscall.Kevent(p.kq, events[:], nil, nil)
syscall.Kevent(p.kq, p.kbuf[:], nil, nil)
}
func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err os.Error) {