-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselector.go
149 lines (125 loc) · 2.89 KB
/
selector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package poller
import (
"log"
"sync/atomic"
"syscall"
"unsafe"
"github.com/widaT/poller/interest"
"github.com/widaT/poller/pollopt"
"golang.org/x/sys/unix"
)
const POLLET uint32 = 1 << 31
const POLLONESHOT uint32 = 1 << 30
const DefaultEventLen = 128
type Token int32
const WakerToken = Token(0)
var token = Token(1)
func NextToken() (tk Token) {
tk = token
token++
return
}
type Selector struct {
id uint32
fd int
}
var NextId uint32 = 1
func NewSelector() (selector *Selector, err error) {
selector = new(Selector)
selector.fd, err = unix.EpollCreate1(unix.EPOLL_CLOEXEC)
if err != nil {
return nil, err
}
selector.id = atomic.AddUint32(&NextId, 1)
return
}
func (s *Selector) Id() uint32 {
return s.id
}
func (s *Selector) Fd() int {
return s.fd
}
func (s *Selector) Select(events []Event, timeout int) (int, error) {
n, err := epollWait(s.fd, events, timeout)
if err != nil {
return 0, err
}
return n, nil
}
func (s *Selector) Register(fd int, token Token, interests interest.Interest, opt pollopt.PollOpt) error {
return unix.EpollCtl(s.fd, syscall.EPOLL_CTL_ADD, fd,
&unix.EpollEvent{Events: interestsToEpoll(interests, opt), Fd: int32(fd), Pad: int32(token)})
}
func (s *Selector) Reregister(fd int, token Token, interests interest.Interest, opt pollopt.PollOpt) error {
return unix.EpollCtl(s.fd, syscall.EPOLL_CTL_MOD, fd,
&unix.EpollEvent{Events: interestsToEpoll(interests, opt), Fd: int32(fd), Pad: int32(token)})
}
func (s *Selector) Deregister(fd int) error {
return unix.EpollCtl(s.fd, syscall.EPOLL_CTL_DEL, fd, nil)
}
func Polling(s *Selector, f func(*Event) error) error {
events := MakeEvents(DefaultEventLen)
for {
n, err := s.Select(events, -1)
if err != nil && err != unix.EINTR {
log.Println(err)
continue
}
for i := 0; i < n; i++ {
ev := events[i]
if err := f(&ev); err != nil {
return err
}
}
if n == len(events) {
events = MakeEvents(n << 1)
}
}
}
func interestsToEpoll(interests interest.Interest, opts pollopt.PollOpt) uint32 {
var kind uint32 = 0
if interests.IsReadable() {
kind = kind | unix.POLLIN | unix.POLLHUP
}
if interests.IsWritable() {
kind |= unix.POLLOUT
}
if opts.IsEdge() {
kind |= POLLET
}
if opts.IsOneshot() {
kind |= POLLONESHOT
}
if opts.IsLevel() {
kind &= ^POLLET
}
return kind
}
var _zero uintptr
func epollWait(epfd int, events []Event, msec int) (n int, err error) {
var _p0 unsafe.Pointer
if len(events) > 0 {
_p0 = unsafe.Pointer(&events[0])
} else {
_p0 = unsafe.Pointer(&_zero)
}
r0, _, e1 := unix.Syscall6(unix.SYS_EPOLL_WAIT, uintptr(epfd), uintptr(_p0), uintptr(len(events)), uintptr(msec), 0, 0)
n = int(r0)
if e1 != 0 {
err = errnoErr(e1)
}
return
}
func errnoErr(e syscall.Errno) error {
switch e {
case 0:
return nil
case unix.EAGAIN:
return syscall.EAGAIN
case unix.EINVAL:
return syscall.EINVAL
case unix.ENOENT:
return syscall.ENOENT
}
return e
}