-
Notifications
You must be signed in to change notification settings - Fork 2
/
kqueue.go
193 lines (177 loc) · 4.05 KB
/
kqueue.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// +build darwin,!disable_kqueue
package liblpc
import (
"github.com/gen-iot/std"
"golang.org/x/sys/unix"
)
type Kqueue struct {
kfd int
watchers *FdWatcherMap
evtBuf []unix.Kevent_t
}
func NewKqueue(pollSize int) (Poller, error) {
kfd, err := unix.Kqueue()
if err != nil {
return nil, err
}
return &Kqueue{
kfd: kfd,
watchers: NewFdWatcherMap(),
evtBuf: make([]unix.Kevent_t, pollSize),
}, nil
}
func (this *Kqueue) Close() error {
std.CloseIgnoreErr(this.watchers)
return unix.Close(this.kfd)
}
func (this *Kqueue) handleReadyFd(pfd *unix.PollFd) {
defer func() { pfd.Revents = 0 }()
fd := pfd.Fd
watcher := this.watchers.GetWatcher(int(fd))
if watcher == nil {
// disable any event...
pfd.Events = 0
stdLog("poll unknown fd = ", fd, ", watcher not found")
return
}
watcher.OnEvent(pfd.Revents)
}
func (this *Kqueue) WatcherCtl(action PollerAction, watcher EventWatcher) error {
switch action {
case Add:
return this.kqueueEvtAdd(watcher)
case Mod:
return this.kqueueEvtMod(watcher)
case Del:
return this.kqueueEvtDel(watcher)
}
return nil
}
func (this *Kqueue) Poll(msec int) error {
var tout *unix.Timespec = nil
if msec <= 0 {
tout = nil
} else {
tout = &unix.Timespec{
Sec: 0,
Nsec: int64(msec * 10e6),
}
}
nEvent, err := unix.Kevent(this.kfd, nil, this.evtBuf, tout)
if err != nil {
return err
}
for idx := 0; idx < nEvent; idx++ {
kEv := this.evtBuf[idx]
fd := int(kEv.Ident)
watcher := this.watchers.GetWatcher(fd)
if watcher == nil {
continue
}
switch kEv.Filter {
case unix.EVFILT_WRITE:
watcher.OnEvent(Writeable)
default:
watcher.OnEvent(Readable) // trigger read...
}
}
return nil
}
type kqEvtHelper EventSizeType
func (this kqEvtHelper) Flag(testBit EventSizeType) uint16 {
if EventSizeType(this)&testBit == 0 {
return unix.EV_DELETE
}
return unix.EV_ADD
}
func (this *Kqueue) kqueueEvtAdd(watcher EventWatcher) error {
kesAdd := make([]unix.Kevent_t, 0, 2)
fd := watcher.GetFd()
evt := watcher.GetEvent()
if evt&Readable != 0 {
kesAdd = append(kesAdd, unix.Kevent_t{
Ident: uint64(fd),
Filter: unix.EVFILT_READ,
Flags: unix.EV_ADD,
})
}
if evt&Writeable != 0 {
kesAdd = append(kesAdd, unix.Kevent_t{
Ident: uint64(fd),
Filter: unix.EVFILT_WRITE,
Flags: unix.EV_ADD,
})
}
this.watchers.SetFd(fd, watcher)
if len(kesAdd) != 0 {
if _, err := unix.Kevent(this.kfd, kesAdd, nil, nil); err != nil {
return err
}
}
return nil
}
func (this *Kqueue) kqueueEvtMod(watcher EventWatcher) error {
fd := watcher.GetFd()
evt := watcher.GetEvent()
evtOut := make([]unix.Kevent_t, 2)
nErr, _ := unix.Kevent(this.kfd, []unix.Kevent_t{
{
Ident: uint64(fd),
Filter: unix.EVFILT_READ,
Flags: kqEvtHelper(evt).Flag(Readable),
},
{
Ident: uint64(fd),
Filter: unix.EVFILT_WRITE,
Flags: kqEvtHelper(evt).Flag(Writeable),
},
}, evtOut, nil)
if nErr == 0 {
return nil
}
cmErrs := make(std.CombinedErrors, 0, 2)
for idx := 0; idx < nErr; idx++ {
if evtOut[idx].Flags&unix.EV_ERROR != 0 &&
!unix.Errno(evtOut[idx].Data).Is(unix.ENOENT) {
cmErrs = append(cmErrs, unix.Errno(evtOut[idx].Data))
}
}
if len(cmErrs) == 0 {
return nil
}
return cmErrs
}
func (this *Kqueue) kqueueEvtDel(watcher EventWatcher) error {
kes := make([]unix.Kevent_t, 0, 2)
fd := watcher.GetFd()
evtOut := make([]unix.Kevent_t, 0, 2)
kes = append(kes, unix.Kevent_t{
Ident: uint64(fd),
Filter: unix.EVFILT_READ,
Flags: unix.EV_DISABLE | unix.EV_DELETE,
})
kes = append(kes, unix.Kevent_t{
Ident: uint64(fd),
Filter: unix.EVFILT_WRITE,
Flags: unix.EV_DISABLE | unix.EV_DELETE,
})
this.watchers.RmFd(fd)
nErr, _ := unix.Kevent(this.kfd, kes, evtOut, nil)
if nErr == 0 {
return nil
}
cmErrs := make(std.CombinedErrors, 0, 2)
for idx := 0; idx < nErr; idx++ {
if evtOut[idx].Flags&unix.EV_ERROR != 0 &&
!unix.Errno(evtOut[idx].Data).Is(unix.ENOENT) {
cmErrs = append(cmErrs, unix.Errno(evtOut[idx].Data))
}
}
if len(cmErrs) == 0 {
return nil
}
return cmErrs
}
func init() {
DefaultPollerCreator = NewKqueue
}