-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsys_kqueue.go
62 lines (54 loc) · 1.17 KB
/
sys_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
//go:build darwin || freebsd
package ddio
import (
"golang.org/x/sys/unix"
"time"
)
type kqueue struct {
kqfd int
}
func NewKqueue() (*kqueue, error) {
kq := &kqueue{}
kqfd, err := unix.Kqueue()
if err != nil {
return nil, err
}
kq.kqfd = kqfd
return kq, nil
}
func (k *kqueue) AddEvent(ev *Event) error {
var events [1]unix.Kevent_t
events[0] = unix.Kevent_t{
Ident: uint64(ev.fd()),
Filter: int16(ev.Flags()),
Flags: unix.EV_ADD | unix.EV_ENABLE,
Fflags: 0,
Data: 0,
Udata: nil,
}
_, err := unix.Kevent(k.kqfd, events[:], nil, nil)
return err
}
func (k *kqueue) ModifyEvent(ev *Event) error {
return k.AddEvent(ev)
}
func (k *kqueue) DelEvent(ev *Event) error {
var events [1]unix.Kevent_t
events[0] = unix.Kevent_t{
Ident: uint64(ev.fd()),
Filter: int16(ev.Flags()),
Flags: unix.EV_DELETE | unix.EV_ONESHOT,
Fflags: 0,
Data: 0,
Udata: nil,
}
_, err := unix.Kevent(k.kqfd, events[:], nil, nil)
return err
}
func (k *kqueue) Wait(events []unix.Kevent_t, timeOut time.Duration) (n int, err error) {
timeSpec := &unix.Timespec{
Sec: timeOut.Milliseconds() / 1000,
Nsec: 0,
}
return unix.Kevent(k.kqfd, nil, events, timeSpec)
}