forked from xtaci/gaio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aio_bsd.go
174 lines (152 loc) · 3.54 KB
/
aio_bsd.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
// +build darwin netbsd freebsd openbsd dragonfly
package gaio
import (
"net"
"sync"
"syscall"
)
type poller struct {
mu sync.Mutex // mutex to protect fd closing
fd int // kqueue fd
// awaiting for poll
awaiting []int
awaitingMutex sync.Mutex
// closing signal
die chan struct{}
dieOnce sync.Once
}
// dupconn use RawConn to dup() file descriptor
func dupconn(conn net.Conn) (newfd int, err error) {
sc, ok := conn.(interface {
SyscallConn() (syscall.RawConn, error)
})
if !ok {
return -1, ErrUnsupported
}
rc, err := sc.SyscallConn()
if err != nil {
return -1, ErrUnsupported
}
// Control() guarantees the integrity of file descriptor
ec := rc.Control(func(fd uintptr) {
newfd, err = syscall.Dup(int(fd))
})
if ec != nil {
return -1, ec
}
return
}
func openPoll() (*poller, error) {
fd, err := syscall.Kqueue()
if err != nil {
return nil, err
}
_, err = syscall.Kevent(fd, []syscall.Kevent_t{{
Ident: 0,
Filter: syscall.EVFILT_USER,
Flags: syscall.EV_ADD | syscall.EV_CLEAR,
}}, nil, nil)
if err != nil {
return nil, err
}
p := new(poller)
p.fd = fd
p.die = make(chan struct{})
return p, nil
}
func (p *poller) Close() error {
p.dieOnce.Do(func() {
close(p.die)
})
return p.wakeup()
}
func (p *poller) Watch(fd int) error {
p.awaitingMutex.Lock()
p.awaiting = append(p.awaiting, fd)
p.awaitingMutex.Unlock()
return p.wakeup()
}
// wakeup interrupt kevent
func (p *poller) wakeup() error {
p.mu.Lock()
if p.fd != -1 {
// notify poller
_, err := syscall.Kevent(p.fd, []syscall.Kevent_t{{
Ident: 0,
Filter: syscall.EVFILT_USER,
Fflags: syscall.NOTE_TRIGGER,
}}, nil, nil)
p.mu.Unlock()
return err
}
p.mu.Unlock()
return ErrPollerClosed
}
func (p *poller) Wait(chEventNotify chan pollerEvents) {
events := make([]syscall.Kevent_t, maxEvents)
defer func() {
p.mu.Lock()
syscall.Close(p.fd)
p.fd = -1
p.mu.Unlock()
}()
// kqueue eventloop
var changes []syscall.Kevent_t
for {
select {
case <-p.die:
return
default:
p.awaitingMutex.Lock()
for _, fd := range p.awaiting {
changes = append(changes,
syscall.Kevent_t{Ident: uint64(fd), Flags: syscall.EV_ADD | syscall.EV_CLEAR, Filter: syscall.EVFILT_READ},
syscall.Kevent_t{Ident: uint64(fd), Flags: syscall.EV_ADD | syscall.EV_CLEAR, Filter: syscall.EVFILT_WRITE},
)
}
p.awaiting = p.awaiting[:0]
p.awaitingMutex.Unlock()
// poll
n, err := syscall.Kevent(p.fd, changes, events, nil)
if err == syscall.EINTR {
continue
}
if err != nil {
return
}
changes = changes[:0]
// event processing
var pe pollerEvents
for i := 0; i < n; i++ {
ev := &events[i]
if ev.Ident != 0 {
e := event{ident: int(ev.Ident)}
if ev.Filter == syscall.EVFILT_READ {
e.r = true
// https://golang.org/src/runtime/netpoll_kqueue.go
// On some systems when the read end of a pipe
// is closed the write end will not get a
// _EVFILT_WRITE event, but will get a
// _EVFILT_READ event with EV_EOF set.
// Note that setting 'w' here just means that we
// will wake up a goroutine waiting to write;
// that goroutine will try the write again,
// and the appropriate thing will happen based
// on what that write returns (success, EPIPE, EAGAIN).
if ev.Flags&syscall.EV_EOF != 0 {
e.w = true
}
} else if ev.Filter == syscall.EVFILT_WRITE {
e.w = true
}
pe = append(pe, e)
}
}
select {
case chEventNotify <- pe:
case <-p.die:
return
}
}
}
}