github.com/mtsmfm/go/src@v0.0.0-20221020090648-44bdcb9f8fde/runtime/netpoll_solaris.go (about) 1 // Copyright 2014 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 package runtime 6 7 import ( 8 "runtime/internal/atomic" 9 "unsafe" 10 ) 11 12 // Solaris runtime-integrated network poller. 13 // 14 // Solaris uses event ports for scalable network I/O. Event 15 // ports are level-triggered, unlike epoll and kqueue which 16 // can be configured in both level-triggered and edge-triggered 17 // mode. Level triggering means we have to keep track of a few things 18 // ourselves. After we receive an event for a file descriptor, 19 // it's our responsibility to ask again to be notified for future 20 // events for that descriptor. When doing this we must keep track of 21 // what kind of events the goroutines are currently interested in, 22 // for example a fd may be open both for reading and writing. 23 // 24 // A description of the high level operation of this code 25 // follows. Networking code will get a file descriptor by some means 26 // and will register it with the netpolling mechanism by a code path 27 // that eventually calls runtime·netpollopen. runtime·netpollopen 28 // calls port_associate with an empty event set. That means that we 29 // will not receive any events at this point. The association needs 30 // to be done at this early point because we need to process the I/O 31 // readiness notification at some point in the future. If I/O becomes 32 // ready when nobody is listening, when we finally care about it, 33 // nobody will tell us anymore. 34 // 35 // Beside calling runtime·netpollopen, the networking code paths 36 // will call runtime·netpollarm each time goroutines are interested 37 // in doing network I/O. Because now we know what kind of I/O we 38 // are interested in (reading/writing), we can call port_associate 39 // passing the correct type of event set (POLLIN/POLLOUT). As we made 40 // sure to have already associated the file descriptor with the port, 41 // when we now call port_associate, we will unblock the main poller 42 // loop (in runtime·netpoll) right away if the socket is actually 43 // ready for I/O. 44 // 45 // The main poller loop runs in its own thread waiting for events 46 // using port_getn. When an event happens, it will tell the scheduler 47 // about it using runtime·netpollready. Besides doing this, it must 48 // also re-associate the events that were not part of this current 49 // notification with the file descriptor. Failing to do this would 50 // mean each notification will prevent concurrent code using the 51 // same file descriptor in parallel. 52 // 53 // The logic dealing with re-associations is encapsulated in 54 // runtime·netpollupdate. This function takes care to associate the 55 // descriptor only with the subset of events that were previously 56 // part of the association, except the one that just happened. We 57 // can't re-associate with that right away, because event ports 58 // are level triggered so it would cause a busy loop. Instead, that 59 // association is effected only by the runtime·netpollarm code path, 60 // when Go code actually asks for I/O. 61 // 62 // The open and arming mechanisms are serialized using the lock 63 // inside PollDesc. This is required because the netpoll loop runs 64 // asynchronously in respect to other Go code and by the time we get 65 // to call port_associate to update the association in the loop, the 66 // file descriptor might have been closed and reopened already. The 67 // lock allows runtime·netpollupdate to be called synchronously from 68 // the loop thread while preventing other threads operating to the 69 // same PollDesc, so once we unblock in the main loop, until we loop 70 // again we know for sure we are always talking about the same file 71 // descriptor and can safely access the data we want (the event set). 72 73 //go:cgo_import_dynamic libc_port_create port_create "libc.so" 74 //go:cgo_import_dynamic libc_port_associate port_associate "libc.so" 75 //go:cgo_import_dynamic libc_port_dissociate port_dissociate "libc.so" 76 //go:cgo_import_dynamic libc_port_getn port_getn "libc.so" 77 //go:cgo_import_dynamic libc_port_alert port_alert "libc.so" 78 79 //go:linkname libc_port_create libc_port_create 80 //go:linkname libc_port_associate libc_port_associate 81 //go:linkname libc_port_dissociate libc_port_dissociate 82 //go:linkname libc_port_getn libc_port_getn 83 //go:linkname libc_port_alert libc_port_alert 84 85 var ( 86 libc_port_create, 87 libc_port_associate, 88 libc_port_dissociate, 89 libc_port_getn, 90 libc_port_alert libcFunc 91 netpollWakeSig atomic.Uint32 // used to avoid duplicate calls of netpollBreak 92 ) 93 94 func errno() int32 { 95 return *getg().m.perrno 96 } 97 98 func fcntl(fd, cmd, arg int32) int32 { 99 return int32(sysvicall3(&libc_fcntl, uintptr(fd), uintptr(cmd), uintptr(arg))) 100 } 101 102 func port_create() int32 { 103 return int32(sysvicall0(&libc_port_create)) 104 } 105 106 func port_associate(port, source int32, object uintptr, events uint32, user uintptr) int32 { 107 return int32(sysvicall5(&libc_port_associate, uintptr(port), uintptr(source), object, uintptr(events), user)) 108 } 109 110 func port_dissociate(port, source int32, object uintptr) int32 { 111 return int32(sysvicall3(&libc_port_dissociate, uintptr(port), uintptr(source), object)) 112 } 113 114 func port_getn(port int32, evs *portevent, max uint32, nget *uint32, timeout *timespec) int32 { 115 return int32(sysvicall5(&libc_port_getn, uintptr(port), uintptr(unsafe.Pointer(evs)), uintptr(max), uintptr(unsafe.Pointer(nget)), uintptr(unsafe.Pointer(timeout)))) 116 } 117 118 func port_alert(port int32, flags, events uint32, user uintptr) int32 { 119 return int32(sysvicall4(&libc_port_alert, uintptr(port), uintptr(flags), uintptr(events), user)) 120 } 121 122 var portfd int32 = -1 123 124 func netpollinit() { 125 portfd = port_create() 126 if portfd >= 0 { 127 fcntl(portfd, _F_SETFD, _FD_CLOEXEC) 128 return 129 } 130 131 print("runtime: port_create failed (errno=", errno(), ")\n") 132 throw("runtime: netpollinit failed") 133 } 134 135 func netpollIsPollDescriptor(fd uintptr) bool { 136 return fd == uintptr(portfd) 137 } 138 139 func netpollopen(fd uintptr, pd *pollDesc) int32 { 140 lock(&pd.lock) 141 // We don't register for any specific type of events yet, that's 142 // netpollarm's job. We merely ensure we call port_associate before 143 // asynchronous connect/accept completes, so when we actually want 144 // to do any I/O, the call to port_associate (from netpollarm, 145 // with the interested event set) will unblock port_getn right away 146 // because of the I/O readiness notification. 147 pd.user = 0 148 r := port_associate(portfd, _PORT_SOURCE_FD, fd, 0, uintptr(unsafe.Pointer(pd))) 149 unlock(&pd.lock) 150 return r 151 } 152 153 func netpollclose(fd uintptr) int32 { 154 return port_dissociate(portfd, _PORT_SOURCE_FD, fd) 155 } 156 157 // Updates the association with a new set of interested events. After 158 // this call, port_getn will return one and only one event for that 159 // particular descriptor, so this function needs to be called again. 160 func netpollupdate(pd *pollDesc, set, clear uint32) { 161 if pd.info().closing() { 162 return 163 } 164 165 old := pd.user 166 events := (old & ^clear) | set 167 if old == events { 168 return 169 } 170 171 if events != 0 && port_associate(portfd, _PORT_SOURCE_FD, pd.fd, events, uintptr(unsafe.Pointer(pd))) != 0 { 172 print("runtime: port_associate failed (errno=", errno(), ")\n") 173 throw("runtime: netpollupdate failed") 174 } 175 pd.user = events 176 } 177 178 // subscribe the fd to the port such that port_getn will return one event. 179 func netpollarm(pd *pollDesc, mode int) { 180 lock(&pd.lock) 181 switch mode { 182 case 'r': 183 netpollupdate(pd, _POLLIN, 0) 184 case 'w': 185 netpollupdate(pd, _POLLOUT, 0) 186 default: 187 throw("runtime: bad mode") 188 } 189 unlock(&pd.lock) 190 } 191 192 // netpollBreak interrupts a port_getn wait. 193 func netpollBreak() { 194 // Failing to cas indicates there is an in-flight wakeup, so we're done here. 195 if !netpollWakeSig.CompareAndSwap(0, 1) { 196 return 197 } 198 199 // Use port_alert to put portfd into alert mode. 200 // This will wake up all threads sleeping in port_getn on portfd, 201 // and cause their calls to port_getn to return immediately. 202 // Further, until portfd is taken out of alert mode, 203 // all calls to port_getn will return immediately. 204 if port_alert(portfd, _PORT_ALERT_UPDATE, _POLLHUP, uintptr(unsafe.Pointer(&portfd))) < 0 { 205 if e := errno(); e != _EBUSY { 206 println("runtime: port_alert failed with", e) 207 throw("runtime: netpoll: port_alert failed") 208 } 209 } 210 } 211 212 // netpoll checks for ready network connections. 213 // Returns list of goroutines that become runnable. 214 // delay < 0: blocks indefinitely 215 // delay == 0: does not block, just polls 216 // delay > 0: block for up to that many nanoseconds 217 func netpoll(delay int64) gList { 218 if portfd == -1 { 219 return gList{} 220 } 221 222 var wait *timespec 223 var ts timespec 224 if delay < 0 { 225 wait = nil 226 } else if delay == 0 { 227 wait = &ts 228 } else { 229 ts.setNsec(delay) 230 if ts.tv_sec > 1e6 { 231 // An arbitrary cap on how long to wait for a timer. 232 // 1e6 s == ~11.5 days. 233 ts.tv_sec = 1e6 234 } 235 wait = &ts 236 } 237 238 var events [128]portevent 239 retry: 240 var n uint32 = 1 241 r := port_getn(portfd, &events[0], uint32(len(events)), &n, wait) 242 e := errno() 243 if r < 0 && e == _ETIME && n > 0 { 244 // As per port_getn(3C), an ETIME failure does not preclude the 245 // delivery of some number of events. Treat a timeout failure 246 // with delivered events as a success. 247 r = 0 248 } 249 if r < 0 { 250 if e != _EINTR && e != _ETIME { 251 print("runtime: port_getn on fd ", portfd, " failed (errno=", e, ")\n") 252 throw("runtime: netpoll failed") 253 } 254 // If a timed sleep was interrupted and there are no events, 255 // just return to recalculate how long we should sleep now. 256 if delay > 0 { 257 return gList{} 258 } 259 goto retry 260 } 261 262 var toRun gList 263 for i := 0; i < int(n); i++ { 264 ev := &events[i] 265 266 if ev.portev_source == _PORT_SOURCE_ALERT { 267 if ev.portev_events != _POLLHUP || unsafe.Pointer(ev.portev_user) != unsafe.Pointer(&portfd) { 268 throw("runtime: netpoll: bad port_alert wakeup") 269 } 270 if delay != 0 { 271 // Now that a blocking call to netpoll 272 // has seen the alert, take portfd 273 // back out of alert mode. 274 // See the comment in netpollBreak. 275 if port_alert(portfd, 0, 0, 0) < 0 { 276 e := errno() 277 println("runtime: port_alert failed with", e) 278 throw("runtime: netpoll: port_alert failed") 279 } 280 netpollWakeSig.Store(0) 281 } 282 continue 283 } 284 285 if ev.portev_events == 0 { 286 continue 287 } 288 pd := (*pollDesc)(unsafe.Pointer(ev.portev_user)) 289 290 var mode, clear int32 291 if (ev.portev_events & (_POLLIN | _POLLHUP | _POLLERR)) != 0 { 292 mode += 'r' 293 clear |= _POLLIN 294 } 295 if (ev.portev_events & (_POLLOUT | _POLLHUP | _POLLERR)) != 0 { 296 mode += 'w' 297 clear |= _POLLOUT 298 } 299 // To effect edge-triggered events, we need to be sure to 300 // update our association with whatever events were not 301 // set with the event. For example if we are registered 302 // for POLLIN|POLLOUT, and we get POLLIN, besides waking 303 // the goroutine interested in POLLIN we have to not forget 304 // about the one interested in POLLOUT. 305 if clear != 0 { 306 lock(&pd.lock) 307 netpollupdate(pd, 0, uint32(clear)) 308 unlock(&pd.lock) 309 } 310 311 if mode != 0 { 312 // TODO(mikio): Consider implementing event 313 // scanning error reporting once we are sure 314 // about the event port on SmartOS. 315 // 316 // See golang.org/x/issue/30840. 317 netpollready(&toRun, pd, mode) 318 } 319 } 320 321 return toRun 322 }