2008-09-26 15:11:26 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
// Waiting for FDs via epoll(7).
|
2008-09-26 15:11:26 -06:00
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2008-09-29 14:37:00 -06:00
|
|
|
"net";
|
2008-09-26 15:11:26 -06:00
|
|
|
"os";
|
|
|
|
"syscall";
|
|
|
|
)
|
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
const (
|
2009-01-16 15:16:31 -07:00
|
|
|
readFlags = syscall.EPOLLIN | syscall.EPOLLRDHUP;
|
|
|
|
writeFlags = syscall.EPOLLOUT
|
2008-09-29 14:37:00 -06:00
|
|
|
)
|
2008-09-26 15:11:26 -06:00
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
type pollster struct {
|
2009-06-01 23:14:39 -06:00
|
|
|
epfd int;
|
2008-09-26 15:11:26 -06:00
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
// Events we're already waiting for
|
2009-06-01 23:14:39 -06:00
|
|
|
events map[int] uint32;
|
2008-09-26 15:11:26 -06:00
|
|
|
}
|
|
|
|
|
2009-04-17 01:08:24 -06:00
|
|
|
func newpollster() (p *pollster, err os.Error) {
|
2009-03-05 16:48:12 -07:00
|
|
|
p = new(pollster);
|
2009-06-01 23:14:39 -06:00
|
|
|
var e int;
|
2008-09-26 15:11:26 -06:00
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
// The arg to epoll_create is a hint to the kernel
|
|
|
|
// about the number of FDs we will care about.
|
|
|
|
// We don't know.
|
2009-06-01 23:14:39 -06:00
|
|
|
if p.epfd, e = syscall.EpollCreate(16); e != 0 {
|
2008-09-29 14:37:00 -06:00
|
|
|
return nil, os.ErrnoToError(e)
|
|
|
|
}
|
2009-06-01 23:14:39 -06:00
|
|
|
p.events = make(map[int] uint32);
|
2008-09-29 14:37:00 -06:00
|
|
|
return p, nil
|
2008-09-26 15:11:26 -06:00
|
|
|
}
|
|
|
|
|
2009-06-01 23:14:39 -06:00
|
|
|
func (p *pollster) AddFD(fd int, mode int, repeat bool) os.Error {
|
2008-10-07 18:25:32 -06:00
|
|
|
var ev syscall.EpollEvent;
|
2008-09-29 14:37:00 -06:00
|
|
|
var already bool;
|
2009-01-16 15:16:31 -07:00
|
|
|
ev.Fd = int32(fd);
|
|
|
|
ev.Events, already = p.events[fd];
|
2008-09-29 14:37:00 -06:00
|
|
|
if !repeat {
|
2009-03-06 18:51:31 -07:00
|
|
|
ev.Events |= syscall.EPOLLONESHOT;
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|
|
|
|
if mode == 'r' {
|
2009-03-06 18:51:31 -07:00
|
|
|
ev.Events |= readFlags;
|
2008-09-29 14:37:00 -06:00
|
|
|
} else {
|
2009-03-06 18:51:31 -07:00
|
|
|
ev.Events |= writeFlags;
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|
2008-09-26 15:11:26 -06:00
|
|
|
|
2009-06-01 23:14:39 -06:00
|
|
|
var op int;
|
2008-09-29 14:37:00 -06:00
|
|
|
if already {
|
2009-03-06 18:51:31 -07:00
|
|
|
op = syscall.EPOLL_CTL_MOD;
|
2008-09-29 14:37:00 -06:00
|
|
|
} else {
|
2009-03-06 18:51:31 -07:00
|
|
|
op = syscall.EPOLL_CTL_ADD;
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|
2009-06-01 23:14:39 -06:00
|
|
|
if e := syscall.EpollCtl(p.epfd, op, fd, &ev); e != 0 {
|
2008-09-29 14:37:00 -06:00
|
|
|
return os.ErrnoToError(e)
|
|
|
|
}
|
2009-01-16 15:16:31 -07:00
|
|
|
p.events[fd] = ev.Events;
|
2008-09-29 14:37:00 -06:00
|
|
|
return nil
|
2008-09-26 15:11:26 -06:00
|
|
|
}
|
|
|
|
|
2009-06-01 23:14:39 -06:00
|
|
|
func (p *pollster) StopWaiting(fd int, bits uint) {
|
2008-10-07 13:31:31 -06:00
|
|
|
events, already := p.events[fd];
|
2008-09-29 14:37:00 -06:00
|
|
|
if !already {
|
2008-10-07 13:31:31 -06:00
|
|
|
print("Epoll unexpected fd=", fd, "\n");
|
2009-03-06 18:51:31 -07:00
|
|
|
return;
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|
2008-09-26 15:11:26 -06:00
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
// If syscall.EPOLLONESHOT is not set, the wait
|
|
|
|
// is a repeating wait, so don't change it.
|
|
|
|
if events & syscall.EPOLLONESHOT == 0 {
|
2009-03-06 18:51:31 -07:00
|
|
|
return;
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|
2008-09-26 15:11:26 -06:00
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
// Disable the given bits.
|
|
|
|
// If we're still waiting for other events, modify the fd
|
|
|
|
// event in the kernel. Otherwise, delete it.
|
2008-10-29 16:23:36 -06:00
|
|
|
events &= ^uint32(bits);
|
2008-09-29 14:37:00 -06:00
|
|
|
if int32(events) & ^syscall.EPOLLONESHOT != 0 {
|
|
|
|
var ev syscall.EpollEvent;
|
2009-01-16 15:16:31 -07:00
|
|
|
ev.Fd = int32(fd);
|
|
|
|
ev.Events = events;
|
2009-06-01 23:14:39 -06:00
|
|
|
if e := syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_MOD, fd, &ev); e != 0 {
|
2009-03-06 18:51:31 -07:00
|
|
|
print("Epoll modify fd=", fd, ": ", os.ErrnoToError(e).String(), "\n");
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|
2009-03-06 18:51:31 -07:00
|
|
|
p.events[fd] = events;
|
2008-09-29 14:37:00 -06:00
|
|
|
} else {
|
2009-06-01 23:14:39 -06:00
|
|
|
if e := syscall.EpollCtl(p.epfd, syscall.EPOLL_CTL_DEL, fd, nil); e != 0 {
|
2009-03-06 18:51:31 -07:00
|
|
|
print("Epoll delete fd=", fd, ": ", os.ErrnoToError(e).String(), "\n");
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|
2009-03-06 18:51:31 -07:00
|
|
|
p.events[fd] = 0, false;
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|
2008-09-26 15:11:26 -06:00
|
|
|
}
|
|
|
|
|
2009-06-01 23:14:39 -06:00
|
|
|
func (p *pollster) DelFD(fd int, mode int) {
|
2009-03-06 18:51:31 -07:00
|
|
|
if mode == 'r' {
|
|
|
|
p.StopWaiting(fd, readFlags);
|
|
|
|
} else {
|
|
|
|
p.StopWaiting(fd, writeFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-01 23:14:39 -06:00
|
|
|
func (p *pollster) WaitFD(nsec int64) (fd int, mode int, err os.Error) {
|
2008-09-29 14:37:00 -06:00
|
|
|
// Get an event.
|
|
|
|
var evarray [1]syscall.EpollEvent;
|
|
|
|
ev := &evarray[0];
|
2009-06-01 23:14:39 -06:00
|
|
|
var msec int = -1;
|
2009-03-06 18:51:31 -07:00
|
|
|
if nsec > 0 {
|
2009-06-01 23:14:39 -06:00
|
|
|
msec = int((nsec + 1e6 - 1)/1e6);
|
2009-03-06 18:51:31 -07:00
|
|
|
}
|
2009-06-01 23:14:39 -06:00
|
|
|
n, e := syscall.EpollWait(p.epfd, &evarray, msec);
|
2008-09-29 14:37:00 -06:00
|
|
|
for e == syscall.EAGAIN || e == syscall.EINTR {
|
2009-06-01 23:14:39 -06:00
|
|
|
n, e = syscall.EpollWait(p.epfd, &evarray, msec);
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|
2008-09-26 15:11:26 -06:00
|
|
|
if e != 0 {
|
2009-03-06 18:51:31 -07:00
|
|
|
return -1, 0, os.ErrnoToError(e);
|
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
return -1, 0, nil;
|
2008-09-26 15:11:26 -06:00
|
|
|
}
|
2009-06-01 23:14:39 -06:00
|
|
|
fd = int(ev.Fd);
|
2008-09-29 14:37:00 -06:00
|
|
|
|
2009-01-16 15:16:31 -07:00
|
|
|
if ev.Events & writeFlags != 0 {
|
|
|
|
p.StopWaiting(fd, writeFlags);
|
2009-03-06 18:51:31 -07:00
|
|
|
return fd, 'w', nil;
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|
2009-01-16 15:16:31 -07:00
|
|
|
if ev.Events & readFlags != 0 {
|
|
|
|
p.StopWaiting(fd, readFlags);
|
2009-03-06 18:51:31 -07:00
|
|
|
return fd, 'r', nil;
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Other events are error conditions - wake whoever is waiting.
|
2008-10-07 13:31:31 -06:00
|
|
|
events, already := p.events[fd];
|
2009-01-16 15:16:31 -07:00
|
|
|
if events & writeFlags != 0 {
|
|
|
|
p.StopWaiting(fd, writeFlags);
|
2009-03-06 18:51:31 -07:00
|
|
|
return fd, 'w', nil;
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|
2009-01-16 15:16:31 -07:00
|
|
|
p.StopWaiting(fd, readFlags);
|
2009-03-06 18:51:31 -07:00
|
|
|
return fd, 'r', nil;
|
2008-09-26 15:11:26 -06:00
|
|
|
}
|
|
|
|
|
2009-04-17 01:08:24 -06:00
|
|
|
func (p *pollster) Close() os.Error {
|
2009-06-01 23:14:39 -06:00
|
|
|
return os.ErrnoToError(syscall.Close(p.epfd));
|
2008-09-29 14:37:00 -06:00
|
|
|
}
|