-
Notifications
You must be signed in to change notification settings - Fork 1
/
epoll.go
161 lines (134 loc) · 3.36 KB
/
epoll.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
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright © 2018 Timothy E. Peoples <eng@toolman.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package rawio
import (
"sort"
"strings"
"syscall"
"golang.org/x/sys/unix"
"toolman.org/base/log/v2"
)
type epoll struct {
pollFD int
commFD int32
events uint32
}
func prepEpoll(fd int, events uint32) (*epoll, error) {
pfd, err := unix.EpollCreate1(0)
if err != nil {
return nil, err
}
p := &epoll{
pollFD: pfd,
commFD: int32(fd),
events: events,
}
if err := unix.EpollCtl(pfd, unix.EPOLL_CTL_ADD, fd, p.event()); err != nil {
unix.Close(pfd)
return nil, err
}
return p, nil
}
func (p *epoll) rearm() error {
return unix.EpollCtl(p.pollFD, unix.EPOLL_CTL_MOD, int(p.commFD), p.event())
}
func (p *epoll) Close() error {
return unix.Close(p.pollFD)
}
func logEpollEvents(evts []unix.EpollEvent) {
log.Info("EpollEvents:")
for i, e := range evts {
log.Infof(" %d) &{Fd: %d, Events: %v}", i, e.Fd, events(e.Events))
}
}
func (p *epoll) latchWait(l *threadLatch) (uint32, error) {
l.latch()
defer l.unlatch()
return p.wait()
}
func (p *epoll) wait() (uint32, error) {
if log.V(1) {
log.Infof("Calling epollWait for fd=%d events=%v", p.commFD, events(p.events))
}
evts := make([]unix.EpollEvent, 1)
n, err := unix.EpollWait(p.pollFD, evts, -1)
if err != nil {
return 0, err
}
if log.V(1) {
logEpollEvents(evts)
}
if n != 1 {
return 0, unix.EBADFD
}
if evts[0].Fd != p.commFD {
return 0, unix.EBADE
}
return evts[0].Events, nil
}
func (p *epoll) event() *unix.EpollEvent {
return &unix.EpollEvent{
Fd: p.commFD,
Events: unix.EPOLLET | unix.EPOLLONESHOT | p.events,
}
}
// IsEAGAIN returns true if err represents a system error indicating that an
// operation should be tried again.
func IsEAGAIN(err error) bool {
if err == nil {
return false
}
if errno, ok := err.(syscall.Errno); ok && (errno == syscall.EAGAIN || errno == syscall.EINTR) {
return true
}
return false
}
var eventNames = map[uint32]string{
unix.EPOLLIN: "EPOLLIN",
unix.EPOLLPRI: "EPOLLPRI",
unix.EPOLLOUT: "EPOLLOUT",
unix.EPOLLERR: "EPOLLERR",
unix.EPOLLHUP: "EPOLLHUP",
unix.EPOLLRDHUP: "EPOLLRDHUP",
unix.EPOLLET: "EPOLLLET",
unix.EPOLLONESHOT: "EPOLLONESHOT",
unix.EPOLLWAKEUP: "EPOLLWAKEUP",
}
var eventNameList = func() []string {
list := make([]string, len(eventNames))
var i int
for _, n := range eventNames {
list[i] = n
i++
}
sort.Strings(list)
return list
}()
type events uint32
func (e events) has(v uint32) bool {
return uint32(e)&v != 0
}
func (e events) String() string {
var list []string
for evt, name := range eventNames {
if evt&uint32(e) > 0 {
list = append(list, name)
}
}
return strings.Join(list, "|")
}
func eventString(e uint32) string {
return events(e).String()
}