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 (
|
|
|
|
Read = syscall.EPOLLIN | syscall.EPOLLRDHUP;
|
|
|
|
Write = syscall.EPOLLOUT
|
|
|
|
)
|
2008-09-26 15:11:26 -06:00
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
export type Pollster struct {
|
|
|
|
epfd int64;
|
2008-09-26 15:11:26 -06:00
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
// Events we're already waiting for
|
|
|
|
events *map[int64] uint;
|
2008-09-26 15:11:26 -06:00
|
|
|
}
|
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
export func NewPollster() (p *Pollster, err *os.Error) {
|
|
|
|
p = new(Pollster);
|
|
|
|
var e int64;
|
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.
|
|
|
|
if p.epfd, e = syscall.epoll_create(16); e != 0 {
|
|
|
|
return nil, os.ErrnoToError(e)
|
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
p.events = new(map[int64] uint);
|
2008-09-29 14:37:00 -06:00
|
|
|
return p, nil
|
2008-09-26 15:11:26 -06:00
|
|
|
}
|
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
func (p *Pollster) AddFD(fd int64, mode int, repeat bool) *os.Error {
|
|
|
|
var ev syscall.EpollEvent
|
|
|
|
var already bool;
|
|
|
|
ev.fd = int32(fd);
|
2008-10-07 13:31:31 -06:00
|
|
|
ev.events, already = p.events[fd];
|
2008-09-29 14:37:00 -06:00
|
|
|
if !repeat {
|
|
|
|
ev.events |= syscall.EPOLLONESHOT
|
|
|
|
}
|
|
|
|
if mode == 'r' {
|
|
|
|
ev.events |= Read
|
|
|
|
} else {
|
|
|
|
ev.events |= Write
|
|
|
|
}
|
2008-09-26 15:11:26 -06:00
|
|
|
|
2008-10-07 13:31:31 -06:00
|
|
|
var op int64;
|
2008-09-29 14:37:00 -06:00
|
|
|
if already {
|
|
|
|
op = syscall.EPOLL_CTL_MOD
|
|
|
|
} else {
|
|
|
|
op = syscall.EPOLL_CTL_ADD
|
|
|
|
}
|
|
|
|
if e := syscall.epoll_ctl(p.epfd, op, fd, &ev); e != 0 {
|
|
|
|
return os.ErrnoToError(e)
|
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
p.events[fd] = ev.events;
|
2008-09-29 14:37:00 -06:00
|
|
|
return nil
|
2008-09-26 15:11:26 -06:00
|
|
|
}
|
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
func (p *Pollster) StopWaiting(fd int64, 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");
|
2008-09-29 14:37:00 -06:00
|
|
|
return
|
|
|
|
}
|
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 {
|
|
|
|
return
|
|
|
|
}
|
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-07 13:31:31 -06:00
|
|
|
events &= ^bits;
|
2008-09-29 14:37:00 -06:00
|
|
|
if int32(events) & ^syscall.EPOLLONESHOT != 0 {
|
|
|
|
var ev syscall.EpollEvent;
|
|
|
|
ev.fd = int32(fd);
|
|
|
|
ev.events = events;
|
|
|
|
if e := syscall.epoll_ctl(p.epfd, syscall.EPOLL_CTL_MOD, fd, &ev); e != 0 {
|
|
|
|
print("Epoll modify fd=", fd, ": ", os.ErrnoToError(e).String(), "\n")
|
|
|
|
}
|
|
|
|
p.events[fd] = events
|
|
|
|
} else {
|
|
|
|
if e := syscall.epoll_ctl(p.epfd, syscall.EPOLL_CTL_DEL, fd, nil); e != 0 {
|
|
|
|
print("Epoll delete fd=", fd, ": ", os.ErrnoToError(e).String(), "\n")
|
|
|
|
}
|
|
|
|
p.events[fd] = 0, false
|
|
|
|
}
|
2008-09-26 15:11:26 -06:00
|
|
|
}
|
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
func (p *Pollster) WaitFD() (fd int64, mode int, err *os.Error) {
|
|
|
|
// Get an event.
|
|
|
|
var evarray [1]syscall.EpollEvent;
|
|
|
|
ev := &evarray[0];
|
2008-10-07 13:31:31 -06:00
|
|
|
n, e := syscall.epoll_wait(p.epfd, &evarray, -1);
|
2008-09-29 14:37:00 -06:00
|
|
|
for e == syscall.EAGAIN || e == syscall.EINTR {
|
|
|
|
n, e = syscall.epoll_wait(p.epfd, &evarray, -1)
|
|
|
|
}
|
2008-09-26 15:11:26 -06:00
|
|
|
if e != 0 {
|
2008-09-29 14:37:00 -06:00
|
|
|
return -1, 0, os.ErrnoToError(e)
|
2008-09-26 15:11:26 -06:00
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
fd = int64(ev.fd);
|
2008-09-29 14:37:00 -06:00
|
|
|
|
|
|
|
if ev.events & Write != 0 {
|
2008-10-07 13:31:31 -06:00
|
|
|
p.StopWaiting(fd, Write);
|
2008-09-29 14:37:00 -06:00
|
|
|
return fd, 'w', nil
|
|
|
|
}
|
|
|
|
if ev.events & Read != 0 {
|
2008-10-07 13:31:31 -06:00
|
|
|
p.StopWaiting(fd, Read);
|
2008-09-29 14:37:00 -06:00
|
|
|
return fd, 'r', nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other events are error conditions - wake whoever is waiting.
|
2008-10-07 13:31:31 -06:00
|
|
|
events, already := p.events[fd];
|
2008-09-29 14:37:00 -06:00
|
|
|
if events & Write != 0 {
|
2008-10-07 13:31:31 -06:00
|
|
|
p.StopWaiting(fd, Write);
|
2008-09-29 14:37:00 -06:00
|
|
|
return fd, 'w', nil
|
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
p.StopWaiting(fd, Read);
|
2008-09-29 14:37:00 -06:00
|
|
|
return fd, 'r', nil
|
2008-09-26 15:11:26 -06:00
|
|
|
}
|
|
|
|
|
2008-09-29 14:37:00 -06:00
|
|
|
func (p *Pollster) Close() *os.Error {
|
2008-10-07 13:31:31 -06:00
|
|
|
r, e := syscall.close(p.epfd);
|
2008-09-29 14:37:00 -06:00
|
|
|
return os.ErrnoToError(e)
|
|
|
|
}
|