-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwaker.go
45 lines (39 loc) · 832 Bytes
/
waker.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
package poller
import (
"github.com/widaT/poller/interest"
"github.com/widaT/poller/pollopt"
"golang.org/x/sys/unix"
)
var _1u64 = []byte{1, 0, 0, 0, 0, 0, 0, 0}
var _0u64 = make([]byte, 8)
type Waker struct {
fd int
}
func NewWaker(s *Selector, token Token) (waker *Waker, err error) {
waker = new(Waker)
if waker.fd, err = unix.Eventfd(0, unix.EFD_NONBLOCK|unix.EFD_CLOEXEC); err != nil {
return
}
if err = s.Register(waker.fd, token, interest.READABLE, pollopt.Edge); err != nil {
return
}
return
}
func (w *Waker) Wake() error {
_, err := unix.Write(w.fd, _1u64)
if err != nil {
if err == unix.EAGAIN {
w.Reset()
return w.Wake()
}
return err
}
return nil
}
func (w *Waker) Reset() error {
_, err := unix.Read(w.fd, _0u64)
if err != nil && err != unix.EAGAIN {
return err
}
return nil
}