github.com/aloncn/graphics-go@v0.0.1/src/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 "unsafe" 8 9 // Solaris runtime-integrated network poller. 10 // 11 // Solaris uses event ports for scalable network I/O. Event 12 // ports are level-triggered, unlike epoll and kqueue which 13 // can be configured in both level-triggered and edge-triggered 14 // mode. Level triggering means we have to keep track of a few things 15 // ourselves. After we receive an event for a file descriptor, 16 // it's our responsibility to ask again to be notified for future 17 // events for that descriptor. When doing this we must keep track of 18 // what kind of events the goroutines are currently interested in, 19 // for example a fd may be open both for reading and writing. 20 // 21 // A description of the high level operation of this code 22 // follows. Networking code will get a file descriptor by some means 23 // and will register it with the netpolling mechanism by a code path 24 // that eventually calls runtime·netpollopen. runtime·netpollopen 25 // calls port_associate with an empty event set. That means that we 26 // will not receive any events at this point. The association needs 27 // to be done at this early point because we need to process the I/O 28 // readiness notification at some point in the future. If I/O becomes 29 // ready when nobody is listening, when we finally care about it, 30 // nobody will tell us anymore. 31 // 32 // Beside calling runtime·netpollopen, the networking code paths 33 // will call runtime·netpollarm each time goroutines are interested 34 // in doing network I/O. Because now we know what kind of I/O we 35 // are interested in (reading/writing), we can call port_associate 36 // passing the correct type of event set (POLLIN/POLLOUT). As we made 37 // sure to have already associated the file descriptor with the port, 38 // when we now call port_associate, we will unblock the main poller 39 // loop (in runtime·netpoll) right away if the socket is actually 40 // ready for I/O. 41 // 42 // The main poller loop runs in its own thread waiting for events 43 // using port_getn. When an event happens, it will tell the scheduler 44 // about it using runtime·netpollready. Besides doing this, it must 45 // also re-associate the events that were not part of this current 46 // notification with the file descriptor. Failing to do this would 47 // mean each notification will prevent concurrent code using the 48 // same file descriptor in parallel. 49 // 50 // The logic dealing with re-associations is encapsulated in 51 // runtime·netpollupdate. This function takes care to associate the 52 // descriptor only with the subset of events that were previously 53 // part of the association, except the one that just happened. We 54 // can't re-associate with that right away, because event ports 55 // are level triggered so it would cause a busy loop. Instead, that 56 // association is effected only by the runtime·netpollarm code path, 57 // when Go code actually asks for I/O. 58 // 59 // The open and arming mechanisms are serialized using the lock 60 // inside PollDesc. This is required because the netpoll loop runs 61 // asynchonously in respect to other Go code and by the time we get 62 // to call port_associate to update the association in the loop, the 63 // file descriptor might have been closed and reopened already. The 64 // lock allows runtime·netpollupdate to be called synchronously from 65 // the loop thread while preventing other threads operating to the 66 // same PollDesc, so once we unblock in the main loop, until we loop 67 // again we know for sure we are always talking about the same file 68 // descriptor and can safely access the data we want (the event set). 69 70 //go:cgo_import_dynamic libc_port_create port_create "libc.so" 71 //go:cgo_import_dynamic libc_port_associate port_associate "libc.so" 72 //go:cgo_import_dynamic libc_port_dissociate port_dissociate "libc.so" 73 //go:cgo_import_dynamic libc_port_getn port_getn "libc.so" 74 75 //go:linkname libc_port_create libc_port_create 76 //go:linkname libc_port_associate libc_port_associate 77 //go:linkname libc_port_dissociate libc_port_dissociate 78 //go:linkname libc_port_getn libc_port_getn 79 80 var ( 81 libc_port_create, 82 libc_port_associate, 83 libc_port_dissociate, 84 libc_port_getn libcFunc 85 ) 86 87 func errno() int32 { 88 return *getg().m.perrno 89 } 90 91 func fcntl(fd, cmd int32, arg uintptr) int32 { 92 return int32(sysvicall3(&libc_fcntl, uintptr(fd), uintptr(cmd), arg)) 93 } 94 95 func port_create() int32 { 96 return int32(sysvicall0(&libc_port_create)) 97 } 98 99 func port_associate(port, source int32, object uintptr, events uint32, user uintptr) int32 { 100 return int32(sysvicall5(&libc_port_associate, uintptr(port), uintptr(source), object, uintptr(events), user)) 101 } 102 103 func port_dissociate(port, source int32, object uintptr) int32 { 104 return int32(sysvicall3(&libc_port_dissociate, uintptr(port), uintptr(source), object)) 105 } 106 107 func port_getn(port int32, evs *portevent, max uint32, nget *uint32, timeout *timespec) int32 { 108 return int32(sysvicall5(&libc_port_getn, uintptr(port), uintptr(unsafe.Pointer(evs)), uintptr(max), uintptr(unsafe.Pointer(nget)), uintptr(unsafe.Pointer(timeout)))) 109 } 110 111 var portfd int32 = -1 112 113 func netpollinit() { 114 portfd = port_create() 115 if portfd >= 0 { 116 fcntl(portfd, _F_SETFD, _FD_CLOEXEC) 117 return 118 } 119 120 print("netpollinit: failed to create port (", errno(), ")\n") 121 throw("netpollinit: failed to create port") 122 } 123 124 func netpollopen(fd uintptr, pd *pollDesc) int32 { 125 lock(&pd.lock) 126 // We don't register for any specific type of events yet, that's 127 // netpollarm's job. We merely ensure we call port_associate before 128 // asynchonous connect/accept completes, so when we actually want 129 // to do any I/O, the call to port_associate (from netpollarm, 130 // with the interested event set) will unblock port_getn right away 131 // because of the I/O readiness notification. 132 pd.user = 0 133 r := port_associate(portfd, _PORT_SOURCE_FD, fd, 0, uintptr(unsafe.Pointer(pd))) 134 unlock(&pd.lock) 135 return r 136 } 137 138 func netpollclose(fd uintptr) int32 { 139 return port_dissociate(portfd, _PORT_SOURCE_FD, fd) 140 } 141 142 // Updates the association with a new set of interested events. After 143 // this call, port_getn will return one and only one event for that 144 // particular descriptor, so this function needs to be called again. 145 func netpollupdate(pd *pollDesc, set, clear uint32) { 146 if pd.closing { 147 return 148 } 149 150 old := pd.user 151 events := (old & ^clear) | set 152 if old == events { 153 return 154 } 155 156 if events != 0 && port_associate(portfd, _PORT_SOURCE_FD, pd.fd, events, uintptr(unsafe.Pointer(pd))) != 0 { 157 print("netpollupdate: failed to associate (", errno(), ")\n") 158 throw("netpollupdate: failed to associate") 159 } 160 pd.user = events 161 } 162 163 // subscribe the fd to the port such that port_getn will return one event. 164 func netpollarm(pd *pollDesc, mode int) { 165 lock(&pd.lock) 166 switch mode { 167 case 'r': 168 netpollupdate(pd, _POLLIN, 0) 169 case 'w': 170 netpollupdate(pd, _POLLOUT, 0) 171 default: 172 throw("netpollarm: bad mode") 173 } 174 unlock(&pd.lock) 175 } 176 177 // polls for ready network connections 178 // returns list of goroutines that become runnable 179 func netpoll(block bool) *g { 180 if portfd == -1 { 181 return nil 182 } 183 184 var wait *timespec 185 var zero timespec 186 if !block { 187 wait = &zero 188 } 189 190 var events [128]portevent 191 retry: 192 var n uint32 = 1 193 if port_getn(portfd, &events[0], uint32(len(events)), &n, wait) < 0 { 194 if e := errno(); e != _EINTR { 195 print("runtime: port_getn on fd ", portfd, " failed with ", e, "\n") 196 throw("port_getn failed") 197 } 198 goto retry 199 } 200 201 var gp guintptr 202 for i := 0; i < int(n); i++ { 203 ev := &events[i] 204 205 if ev.portev_events == 0 { 206 continue 207 } 208 pd := (*pollDesc)(unsafe.Pointer(ev.portev_user)) 209 210 var mode, clear int32 211 if (ev.portev_events & (_POLLIN | _POLLHUP | _POLLERR)) != 0 { 212 mode += 'r' 213 clear |= _POLLIN 214 } 215 if (ev.portev_events & (_POLLOUT | _POLLHUP | _POLLERR)) != 0 { 216 mode += 'w' 217 clear |= _POLLOUT 218 } 219 // To effect edge-triggered events, we need to be sure to 220 // update our association with whatever events were not 221 // set with the event. For example if we are registered 222 // for POLLIN|POLLOUT, and we get POLLIN, besides waking 223 // the goroutine interested in POLLIN we have to not forget 224 // about the one interested in POLLOUT. 225 if clear != 0 { 226 lock(&pd.lock) 227 netpollupdate(pd, 0, uint32(clear)) 228 unlock(&pd.lock) 229 } 230 231 if mode != 0 { 232 netpollready(&gp, pd, mode) 233 } 234 } 235 236 if block && gp == 0 { 237 goto retry 238 } 239 return gp.ptr() 240 }