-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathepoll_linux.go
73 lines (58 loc) · 1.37 KB
/
epoll_linux.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
// ©Hayabusa Cloud Co., Ltd. 2022. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build linux
package sox
import (
"golang.org/x/sys/unix"
"time"
"unsafe"
)
type epoll struct {
fd int
evts []unix.EpollEvent
}
func newPoller(n int) (*epoll, error) {
if n < 1 {
return nil, ErrInvalidParam
}
evts := make([]unix.EpollEvent, n)
fd, err := unix.EpollCreate1(unix.EPOLL_CLOEXEC)
if err != nil {
return nil, errFromUnixErrno(err)
}
return &epoll{fd: fd, evts: evts}, nil
}
func (ep *epoll) FD() int {
return ep.fd
}
func (ep *epoll) add(fd int, events uint32) error {
evt := &unix.EpollEvent{
Events: events | unix.EPOLLET,
Fd: int32(fd),
}
err := unix.EpollCtl(ep.fd, unix.EPOLL_CTL_ADD, fd, evt)
if err != nil {
return errFromUnixErrno(err)
}
return nil
}
func (ep *epoll) del(fd int) error {
err := unix.EpollCtl(ep.fd, unix.EPOLL_CTL_DEL, fd, nil)
if err != nil {
return errFromUnixErrno(err)
}
return nil
}
func (ep *epoll) wait(d time.Duration) (events []pollerEvent, err error) {
n, err := unix.EpollWait(ep.fd, ep.evts, int(d.Milliseconds()))
if err != nil {
return events, errFromUnixErrno(err)
}
ptr := (*pollerEvent)(unsafe.Pointer(&ep.evts[0]))
events = unsafe.Slice(ptr, n)
return
}
func (ep *epoll) Close() error {
return unix.Close(ep.fd)
}