| 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 linux |
| 6 |
|
| 7 |
package runtime |
| 8 |
|
| 9 |
import ( |
| 10 |
"internal/runtime/atomic" |
| 11 |
"internal/runtime/syscall/linux" |
| 12 |
"unsafe" |
| 13 |
) |
| 14 |
|
| 15 |
var ( |
| 16 |
epfd int32 = -1 // epoll descriptor |
| 17 |
netpollEventFd uintptr // eventfd for netpollBreak |
| 18 |
netpollWakeSig atomic.Uint32 // used to avoid duplicate calls of netpollBreak |
| 19 |
) |
| 20 |
|
| 21 |
func netpollinit() { |
| 22 |
var errno uintptr |
| 23 |
epfd, errno = linux.EpollCreate1(linux.EPOLL_CLOEXEC) |
| 24 |
if errno != 0 { |
| 25 |
println("runtime: epollcreate failed with", errno) |
| 26 |
throw("runtime: netpollinit failed") |
| 27 |
} |
| 28 |
efd, errno := linux.Eventfd(0, linux.EFD_CLOEXEC|linux.EFD_NONBLOCK) |
| 29 |
if errno != 0 { |
| 30 |
println("runtime: eventfd failed with", errno) |
| 31 |
throw("runtime: eventfd failed") |
| 32 |
} |
| 33 |
ev := linux.EpollEvent{ |
| 34 |
Events: linux.EPOLLIN, |
| 35 |
} |
| 36 |
*(**uintptr)(unsafe.Pointer(&ev.Data)) = &netpollEventFd |
| 37 |
errno = linux.EpollCtl(epfd, linux.EPOLL_CTL_ADD, efd, &ev) |
| 38 |
if errno != 0 { |
| 39 |
println("runtime: epollctl failed with", errno) |
| 40 |
throw("runtime: epollctl failed") |
| 41 |
} |
| 42 |
netpollEventFd = uintptr(efd) |
| 43 |
} |
| 44 |
|
| 45 |
func netpollIsPollDescriptor(fd uintptr) bool { |
| 46 |
return fd == uintptr(epfd) || fd == netpollEventFd |
| 47 |
} |
| 48 |
|
| 49 |
func netpollopen(fd uintptr, pd *pollDesc) uintptr { |
| 50 |
var ev linux.EpollEvent |
| 51 |
ev.Events = linux.EPOLLIN | linux.EPOLLOUT | linux.EPOLLRDHUP | linux.EPOLLET |
| 52 |
tp := taggedPointerPack(unsafe.Pointer(pd), pd.fdseq.Load()) |
| 53 |
*(*taggedPointer)(unsafe.Pointer(&ev.Data)) = tp |
| 54 |
return linux.EpollCtl(epfd, linux.EPOLL_CTL_ADD, int32(fd), &ev) |
| 55 |
} |
| 56 |
|
| 57 |
func netpollclose(fd uintptr) uintptr { |
| 58 |
var ev linux.EpollEvent |
| 59 |
return linux.EpollCtl(epfd, linux.EPOLL_CTL_DEL, int32(fd), &ev) |
| 60 |
} |
| 61 |
|
| 62 |
func netpollarm(pd *pollDesc, mode int) { |
| 63 |
throw("runtime: unused") |
| 64 |
} |
| 65 |
|
| 66 |
// netpollBreak interrupts an epollwait. |
| 67 |
func netpollBreak() { |
| 68 |
// Failing to cas indicates there is an in-flight wakeup, so we're done here. |
| 69 |
if !netpollWakeSig.CompareAndSwap(0, 1) { |
| 70 |
return |
| 71 |
} |
| 72 |
|
| 73 |
var one uint64 = 1 |
| 74 |
oneSize := int32(unsafe.Sizeof(one)) |
| 75 |
for { |
| 76 |
n := write(netpollEventFd, noescape(unsafe.Pointer(&one)), oneSize) |
| 77 |
if n == oneSize { |
| 78 |
break |
| 79 |
} |
| 80 |
if n == -_EINTR { |
| 81 |
continue |
| 82 |
} |
| 83 |
if n == -_EAGAIN { |
| 84 |
return |
| 85 |
} |
| 86 |
println("runtime: netpollBreak write failed with", -n) |
| 87 |
throw("runtime: netpollBreak write failed") |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// netpoll checks for ready network connections. |
| 92 |
// Returns a list of goroutines that become runnable, |
| 93 |
// and a delta to add to netpollWaiters. |
| 94 |
// This must never return an empty list with a non-zero delta. |
| 95 |
// |
| 96 |
// delay < 0: blocks indefinitely |
| 97 |
// delay == 0: does not block, just polls |
| 98 |
// delay > 0: block for up to that many nanoseconds |
| 99 |
func netpoll(delay int64) (gList, int32) { |
| 100 |
if epfd == -1 { |
| 101 |
return gList{}, 0 |
| 102 |
} |
| 103 |
var waitms int32 |
| 104 |
if delay < 0 { |
| 105 |
waitms = -1 |
| 106 |
} else if delay == 0 { |
| 107 |
waitms = 0 |
| 108 |
} else if delay < 1e6 { |
| 109 |
waitms = 1 |
| 110 |
} else if delay < 1e15 { |
| 111 |
waitms = int32(delay / 1e6) |
| 112 |
} else { |
| 113 |
// An arbitrary cap on how long to wait for a timer. |
| 114 |
// 1e9 ms == ~11.5 days. |
| 115 |
waitms = 1e9 |
| 116 |
} |
| 117 |
var events [128]linux.EpollEvent |
| 118 |
retry: |
| 119 |
n, errno := linux.EpollWait(epfd, events[:], int32(len(events)), waitms) |
| 120 |
if errno != 0 { |
| 121 |
if errno != _EINTR { |
| 122 |
println("runtime: epollwait on fd", epfd, "failed with", errno) |
| 123 |
throw("runtime: netpoll failed") |
| 124 |
} |
| 125 |
// If a timed sleep was interrupted, just return to |
| 126 |
// recalculate how long we should sleep now. |
| 127 |
if waitms > 0 { |
| 128 |
return gList{}, 0 |
| 129 |
} |
| 130 |
goto retry |
| 131 |
} |
| 132 |
var toRun gList |
| 133 |
delta := int32(0) |
| 134 |
for i := int32(0); i < n; i++ { |
| 135 |
ev := events[i] |
| 136 |
if ev.Events == 0 { |
| 137 |
continue |
| 138 |
} |
| 139 |
|
| 140 |
if *(**uintptr)(unsafe.Pointer(&ev.Data)) == &netpollEventFd { |
| 141 |
if ev.Events != linux.EPOLLIN { |
| 142 |
println("runtime: netpoll: eventfd ready for", ev.Events) |
| 143 |
throw("runtime: netpoll: eventfd ready for something unexpected") |
| 144 |
} |
| 145 |
if delay != 0 { |
| 146 |
// netpollBreak could be picked up by a |
| 147 |
// nonblocking poll. Only read the 8-byte |
| 148 |
// integer if blocking. |
| 149 |
// Since EFD_SEMAPHORE was not specified, |
| 150 |
// the eventfd counter will be reset to 0. |
| 151 |
var one uint64 |
| 152 |
read(int32(netpollEventFd), noescape(unsafe.Pointer(&one)), int32(unsafe.Sizeof(one))) |
| 153 |
netpollWakeSig.Store(0) |
| 154 |
} |
| 155 |
continue |
| 156 |
} |
| 157 |
|
| 158 |
var mode int32 |
| 159 |
if ev.Events&(linux.EPOLLIN|linux.EPOLLRDHUP|linux.EPOLLHUP|linux.EPOLLERR) != 0 { |
| 160 |
mode += 'r' |
| 161 |
} |
| 162 |
if ev.Events&(linux.EPOLLOUT|linux.EPOLLHUP|linux.EPOLLERR) != 0 { |
| 163 |
mode += 'w' |
| 164 |
} |
| 165 |
if mode != 0 { |
| 166 |
tp := *(*taggedPointer)(unsafe.Pointer(&ev.Data)) |
| 167 |
pd := (*pollDesc)(tp.pointer()) |
| 168 |
tag := tp.tag() |
| 169 |
if pd.fdseq.Load() == tag { |
| 170 |
pd.setEventErr(ev.Events == linux.EPOLLERR, tag) |
| 171 |
delta += netpollready(&toRun, pd, mode) |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
return toRun, delta |
| 176 |
} |