1
0
mirror of https://github.com/golang/go synced 2024-11-18 03:44:46 -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 kq int
eventbuf [10]syscall.Kevent_t eventbuf [10]syscall.Kevent_t
events []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) { 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 { func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
// pollServer is locked.
var kmode int var kmode int
if mode == 'r' { if mode == 'r' {
kmode = syscall.EVFILT_READ kmode = syscall.EVFILT_READ
} else { } else {
kmode = syscall.EVFILT_WRITE kmode = syscall.EVFILT_WRITE
} }
var events [1]syscall.Kevent_t ev := &p.kbuf[0]
ev := &events[0]
// EV_ADD - add event to kqueue list // EV_ADD - add event to kqueue list
// EV_RECEIPT - generate fake EV_ERROR as result of add, // EV_RECEIPT - generate fake EV_ERROR as result of add,
// rather than waiting for real event // 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) 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 { if e != 0 {
return os.NewSyscallError("kevent", e) 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) { func (p *pollster) DelFD(fd int, mode int) {
// pollServer is locked.
var kmode int var kmode int
if mode == 'r' { if mode == 'r' {
kmode = syscall.EVFILT_READ kmode = syscall.EVFILT_READ
} else { } else {
kmode = syscall.EVFILT_WRITE kmode = syscall.EVFILT_WRITE
} }
var events [1]syscall.Kevent_t ev := &p.kbuf[0]
ev := &events[0]
// EV_DELETE - delete event from kqueue list // EV_DELETE - delete event from kqueue list
// EV_RECEIPT - generate fake EV_ERROR as result of add, // EV_RECEIPT - generate fake EV_ERROR as result of add,
// rather than waiting for real event // rather than waiting for real event
syscall.SetKevent(ev, fd, kmode, syscall.EV_DELETE|syscall.EV_RECEIPT) 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) { 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 kq int
eventbuf [10]syscall.Kevent_t eventbuf [10]syscall.Kevent_t
events []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) { 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 { func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
// pollServer is locked.
var kmode int var kmode int
if mode == 'r' { if mode == 'r' {
kmode = syscall.EVFILT_READ kmode = syscall.EVFILT_READ
} else { } else {
kmode = syscall.EVFILT_WRITE kmode = syscall.EVFILT_WRITE
} }
var events [1]syscall.Kevent_t ev := &p.kbuf[0]
ev := &events[0]
// EV_ADD - add event to kqueue list // EV_ADD - add event to kqueue list
// EV_ONESHOT - delete the event the first time it triggers // EV_ONESHOT - delete the event the first time it triggers
flags := syscall.EV_ADD 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) 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 { if e != 0 {
return os.NewSyscallError("kevent", e) 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) { func (p *pollster) DelFD(fd int, mode int) {
// pollServer is locked.
var kmode int var kmode int
if mode == 'r' { if mode == 'r' {
kmode = syscall.EVFILT_READ kmode = syscall.EVFILT_READ
} else { } else {
kmode = syscall.EVFILT_WRITE kmode = syscall.EVFILT_WRITE
} }
var events [1]syscall.Kevent_t ev := &p.kbuf[0]
ev := &events[0]
// EV_DELETE - delete event from kqueue list // EV_DELETE - delete event from kqueue list
syscall.SetKevent(ev, fd, kmode, syscall.EV_DELETE) 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) { func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err os.Error) {