-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoller.go
94 lines (83 loc) · 1.71 KB
/
poller.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
package poller
import (
"log"
"github.com/widaT/poller/interest"
"github.com/widaT/poller/pollopt"
"golang.org/x/sys/unix"
)
type Poller struct {
waker *Waker
selector *Selector
tasks []func()
lock *Locker
}
func NewPoller() (p *Poller, err error) {
p = new(Poller)
p.selector, err = NewSelector()
if err != nil {
return
}
p.lock = &Locker{}
p.waker, err = NewWaker(p.selector, WakerToken)
if err != nil {
return
}
return
}
func (p *Poller) AddTask(fn func()) {
p.lock.Lock()
p.tasks = append(p.tasks, fn)
p.lock.Unlock()
}
func (p *Poller) Wake() error {
return p.waker.Wake()
}
func (p *Poller) Polling(f func(*Event) error) error {
events := MakeEvents(DefaultEventLen)
wake := false
for {
n, err := p.selector.Select(events, -1)
if err != nil && err != unix.EINTR {
log.Println(err)
continue
}
for i := 0; i < n; i++ {
ev := events[i]
switch ev.Token() {
case WakerToken:
p.waker.Reset()
wake = true
default:
if err := f(&ev); err != nil {
return err
}
}
}
if wake {
wake = false
p.runTask()
}
if n == len(events) {
events = MakeEvents(n << 1)
}
}
}
func (p *Poller) runTask() {
p.lock.Lock()
tasks := p.tasks
p.tasks = nil
p.lock.Unlock()
length := len(tasks)
for i := 0; i < length; i++ {
tasks[i]()
}
}
func (p *Poller) Register(fd int, token Token, interests interest.Interest, opt pollopt.PollOpt) error {
return p.selector.Register(fd, token, interests, opt)
}
func (p *Poller) Reregister(fd int, token Token, interests interest.Interest, opt pollopt.PollOpt) error {
return p.selector.Reregister(fd, token, interests, opt)
}
func (p *Poller) Deregister(fd int) error {
return p.selector.Deregister(fd)
}