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.
5 //go:build darwin || dragonfly || freebsd || netbsd || openbsd
9 // Integrated network poller (kqueue-based implementation).
13 "runtime/internal/atomic"
20 netpollBreakRd, netpollBreakWr uintptr // for netpollBreak
22 netpollWakeSig atomic.Uint32 // used to avoid duplicate calls of netpollBreak
28 println("runtime: kqueue failed with", -kq)
29 throw("runtime: netpollinit failed")
32 r, w, errno := nonblockingPipe()
34 println("runtime: pipe failed with", -errno)
35 throw("runtime: pipe failed")
41 *(*uintptr)(unsafe.Pointer(&ev.ident)) = uintptr(r)
42 n := kevent(kq, &ev, 1, nil, 0, nil)
44 println("runtime: kevent failed with", -n)
45 throw("runtime: kevent failed")
47 netpollBreakRd = uintptr(r)
48 netpollBreakWr = uintptr(w)
51 func netpollIsPollDescriptor(fd uintptr) bool {
52 return fd == uintptr(kq) || fd == netpollBreakRd || fd == netpollBreakWr
55 func netpollopen(fd uintptr, pd *pollDesc) int32 {
56 // Arm both EVFILT_READ and EVFILT_WRITE in edge-triggered mode (EV_CLEAR)
57 // for the whole fd lifetime. The notifications are automatically unregistered
60 *(*uintptr)(unsafe.Pointer(&ev[0].ident)) = fd
61 ev[0].filter = _EVFILT_READ
62 ev[0].flags = _EV_ADD | _EV_CLEAR
66 if goarch.PtrSize == 4 {
67 // We only have a pointer-sized field to store into,
68 // so on a 32-bit system we get no sequence protection.
69 // TODO(iant): If we notice any problems we could at least
70 // steal the low-order 2 bits for a tiny sequence number.
71 ev[0].udata = (*byte)(unsafe.Pointer(pd))
73 tp := taggedPointerPack(unsafe.Pointer(pd), pd.fdseq.Load())
74 ev[0].udata = (*byte)(unsafe.Pointer(uintptr(tp)))
77 ev[1].filter = _EVFILT_WRITE
78 n := kevent(kq, &ev[0], 2, nil, 0, nil)
85 func netpollclose(fd uintptr) int32 {
86 // Don't need to unregister because calling close()
87 // on fd will remove any kevents that reference the descriptor.
91 func netpollarm(pd *pollDesc, mode int) {
92 throw("runtime: unused")
95 // netpollBreak interrupts a kevent.
97 // Failing to cas indicates there is an in-flight wakeup, so we're done here.
98 if !netpollWakeSig.CompareAndSwap(0, 1) {
104 n := write(netpollBreakWr, unsafe.Pointer(&b), 1)
105 if n == 1 || n == -_EAGAIN {
111 println("runtime: netpollBreak write failed with", -n)
112 throw("runtime: netpollBreak write failed")
116 // netpoll checks for ready network connections.
117 // Returns list of goroutines that become runnable.
118 // delay < 0: blocks indefinitely
119 // delay == 0: does not block, just polls
120 // delay > 0: block for up to that many nanoseconds
121 func netpoll(delay int64) (gList, int32) {
129 } else if delay == 0 {
134 // Darwin returns EINVAL if the sleep time is too long.
139 var events [64]keventt
141 n := kevent(kq, nil, 0, &events[0], int32(len(events)), tp)
143 // Ignore the ETIMEDOUT error for now, but try to dive deep and
144 // figure out what really happened with n == ETIMEOUT,
145 // see https://go.dev/issue/59679 for details.
146 if n != -_EINTR && n != -_ETIMEDOUT {
147 println("runtime: kevent on fd", kq, "failed with", -n)
148 throw("runtime: netpoll failed")
150 // If a timed sleep was interrupted, just return to
151 // recalculate how long we should sleep now.
159 for i := 0; i < int(n); i++ {
162 if uintptr(ev.ident) == netpollBreakRd {
163 if ev.filter != _EVFILT_READ {
164 println("runtime: netpoll: break fd ready for", ev.filter)
165 throw("runtime: netpoll: break fd ready for something unexpected")
168 // netpollBreak could be picked up by a
169 // nonblocking poll. Only read the byte
172 read(int32(netpollBreakRd), noescape(unsafe.Pointer(&tmp[0])), int32(len(tmp)))
173 netpollWakeSig.Store(0)
183 // On some systems when the read end of a pipe
184 // is closed the write end will not get a
185 // _EVFILT_WRITE event, but will get a
186 // _EVFILT_READ event with EV_EOF set.
187 // Note that setting 'w' here just means that we
188 // will wake up a goroutine waiting to write;
189 // that goroutine will try the write again,
190 // and the appropriate thing will happen based
191 // on what that write returns (success, EPIPE, EAGAIN).
192 if ev.flags&_EV_EOF != 0 {
201 if goarch.PtrSize == 4 {
202 // No sequence protection on 32-bit systems.
203 // See netpollopen for details.
204 pd = (*pollDesc)(unsafe.Pointer(ev.udata))
207 tp := taggedPointer(uintptr(unsafe.Pointer(ev.udata)))
208 pd = (*pollDesc)(tp.pointer())
210 if pd.fdseq.Load() != tag {
214 pd.setEventErr(ev.flags == _EV_ERROR, tag)
215 delta += netpollready(&toRun, pd, mode)