Debian Codegraph @

libgo/go/runtime/netpoll_kqueue.go

raw
1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build darwin || dragonfly || freebsd || netbsd || openbsd
6
7 package runtime
8
9 // Integrated network poller (kqueue-based implementation).
10
11 import (
12 "runtime/internal/atomic"
13 "unsafe"
14 )
15
16 //extern-sysinfo kqueue
17 func kqueue() int32
18
19 //go:noescape
20 //extern-sysinfo kevent
21 func kevent(kq int32, ch *keventt, nch uintptr, ev *keventt, nev uintptr, ts *timespec) int32
22
23 var (
24 kq int32 = -1
25
26 netpollBreakRd, netpollBreakWr uintptr // for netpollBreak
27
28 netpollWakeSig uint32 // used to avoid duplicate calls of netpollBreak
29 )
30
31 func netpollinit() {
32 kq = kqueue()
33 if kq < 0 {
34 println("netpollinit: kqueue failed with", errno())
35 throw("runtime: netpollinit failed")
36 }
37 closeonexec(kq)
38 r, w, errno := nonblockingPipe()
39 if errno != 0 {
40 println("runtime: pipe failed with", -errno)
41 throw("runtime: pipe failed")
42 }
43 ev := keventt{
44 filter: _EVFILT_READ,
45 flags: _EV_ADD,
46 }
47 *(*uintptr)(unsafe.Pointer(&ev.ident)) = uintptr(r)
48 n := kevent(kq, &ev, 1, nil, 0, nil)
49 if n < 0 {
50 println("runtime: kevent failed with", -n)
51 throw("runtime: kevent failed")
52 }
53 netpollBreakRd = uintptr(r)
54 netpollBreakWr = uintptr(w)
55 }
56
57 func netpollIsPollDescriptor(fd uintptr) bool {
58 return fd == uintptr(kq) || fd == netpollBreakRd || fd == netpollBreakWr
59 }
60
61 func netpollopen(fd uintptr, pd *pollDesc) int32 {
62 // Arm both EVFILT_READ and EVFILT_WRITE in edge-triggered mode (EV_CLEAR)
63 // for the whole fd lifetime. The notifications are automatically unregistered
64 // when fd is closed.
65 var ev [2]keventt
66 *(*uintptr)(unsafe.Pointer(&ev[0].ident)) = fd
67 ev[0].filter = _EVFILT_READ
68 ev[0].flags = _EV_ADD | _EV_CLEAR
69 ev[0].fflags = 0
70 ev[0].data = 0
71 ev[0].udata = (*byte)(unsafe.Pointer(pd))
72 ev[1] = ev[0]
73 ev[1].filter = _EVFILT_WRITE
74 n := kevent(kq, &ev[0], 2, nil, 0, nil)
75 if n < 0 {
76 return int32(errno())
77 }
78 return 0
79 }
80
81 func netpollclose(fd uintptr) int32 {
82 // Don't need to unregister because calling close()
83 // on fd will remove any kevents that reference the descriptor.
84 return 0
85 }
86
87 func netpollarm(pd *pollDesc, mode int) {
88 throw("runtime: unused")
89 }
90
91 // netpollBreak interrupts a kevent.
92 func netpollBreak() {
93 if atomic.Cas(&netpollWakeSig, 0, 1) {
94 for {
95 var b byte
96 n := write(netpollBreakWr, unsafe.Pointer(&b), 1)
97 if n == 1 || n == -_EAGAIN {
98 break
99 }
100 if n == -_EINTR {
101 continue
102 }
103 println("runtime: netpollBreak write failed with", -n)
104 throw("runtime: netpollBreak write failed")
105 }
106 }
107 }
108
109 // netpoll checks for ready network connections.
110 // Returns list of goroutines that become runnable.
111 // delay < 0: blocks indefinitely
112 // delay == 0: does not block, just polls
113 // delay > 0: block for up to that many nanoseconds
114 func netpoll(delay int64) gList {
115 if kq == -1 {
116 return gList{}
117 }
118 var tp *timespec
119 var ts timespec
120 if delay < 0 {
121 tp = nil
122 } else if delay == 0 {
123 tp = &ts
124 } else {
125 ts.setNsec(delay)
126 if ts.tv_sec > 1e6 {
127 // Darwin returns EINVAL if the sleep time is too long.
128 ts.tv_sec = 1e6
129 }
130 tp = &ts
131 }
132 var events [64]keventt
133 retry:
134 n := kevent(kq, nil, 0, &events[0], uintptr(len(events)), tp)
135 if n < 0 {
136 e := errno()
137 if e != _EINTR {
138 println("runtime: kevent on fd", kq, "failed with", e)
139 throw("runtime: netpoll failed")
140 }
141 // If a timed sleep was interrupted, just return to
142 // recalculate how long we should sleep now.
143 if delay > 0 {
144 return gList{}
145 }
146 goto retry
147 }
148 var toRun gList
149 for i := 0; i < int(n); i++ {
150 ev := &events[i]
151
152 if uintptr(ev.ident) == netpollBreakRd {
153 if ev.filter != _EVFILT_READ {
154 println("runtime: netpoll: break fd ready for", ev.filter)
155 throw("runtime: netpoll: break fd ready for something unexpected")
156 }
157 if delay != 0 {
158 // netpollBreak could be picked up by a
159 // nonblocking poll. Only read the byte
160 // if blocking.
161 var tmp [16]byte
162 read(int32(netpollBreakRd), noescape(unsafe.Pointer(&tmp[0])), int32(len(tmp)))
163 atomic.Store(&netpollWakeSig, 0)
164 }
165 continue
166 }
167
168 var mode int32
169 switch ev.filter {
170 case _EVFILT_READ:
171 mode += 'r'
172
173 // On some systems when the read end of a pipe
174 // is closed the write end will not get a
175 // _EVFILT_WRITE event, but will get a
176 // _EVFILT_READ event with EV_EOF set.
177 // Note that setting 'w' here just means that we
178 // will wake up a goroutine waiting to write;
179 // that goroutine will try the write again,
180 // and the appropriate thing will happen based
181 // on what that write returns (success, EPIPE, EAGAIN).
182 if ev.flags&_EV_EOF != 0 {
183 mode += 'w'
184 }
185 case _EVFILT_WRITE:
186 mode += 'w'
187 }
188 if mode != 0 {
189 pd := (*pollDesc)(unsafe.Pointer(ev.udata))
190 pd.setEventErr(ev.flags == _EV_ERROR)
191 netpollready(&toRun, pd, mode)
192 }
193 }
194 return toRun
195 }