-
Notifications
You must be signed in to change notification settings - Fork 2
/
notify_evfd.go
53 lines (43 loc) · 1.03 KB
/
notify_evfd.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
// +build linux
package liblpc
import (
"golang.org/x/sys/unix"
)
var __notifyWatcherSendBuf []byte
func init() {
__notifyWatcherSendBuf = make([]byte, 8)
__notifyWatcherSendBuf[7] = 0x01
}
type NotifyWatcher struct {
*FdWatcher
wakeupCb func()
readBuf []byte
}
func NewNotifyWatcher(loop EventLoop, wakeupCb func()) (LoopNotify, error) {
eventFd, err := unix.Eventfd(0, unix.EFD_CLOEXEC|unix.EFD_NONBLOCK)
if err != nil {
return nil, err
}
watcher := new(NotifyWatcher)
watcher.FdWatcher = NewFdWatcher(loop, eventFd, watcher)
watcher.wakeupCb = wakeupCb
watcher.readBuf = make([]byte, 8)
return watcher, nil
}
func (this *NotifyWatcher) OnEvent(event EventSizeType) {
_, err := unix.Read(this.GetFd(), this.readBuf)
if err != nil {
return
}
if this.wakeupCb != nil {
this.wakeupCb()
}
}
func (this *NotifyWatcher) GetEvent() EventSizeType {
return Readable
}
func (this *NotifyWatcher) SetEvent(event EventSizeType) {
}
func (this *NotifyWatcher) Notify() {
_, _ = unix.Write(this.GetFd(), __notifyWatcherSendBuf)
}