PR go/83787
[official-gcc.git] / libgo / go / runtime / netpoll_solaris.go
blobe1e73857f4043c90a22f660bf44563fd25a6e524
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.
5 package runtime
7 import "unsafe"
9 // Solaris runtime-integrated network poller.
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.
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.
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.
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.
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.
59 // The open and arming mechanisms are serialized using the lock
60 // inside PollDesc. This is required because the netpoll loop runs
61 // asynchronously 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).
70 //extern __go_fcntl_uintptr
71 func fcntlUintptr(fd, cmd, arg uintptr) (uintptr, uintptr)
73 func fcntl(fd, cmd int32, arg uintptr) int32 {
74 r, _ := fcntlUintptr(uintptr(fd), uintptr(cmd), arg)
75 return int32(r)
78 //extern port_create
79 func port_create() int32
81 //extern port_associate
82 func port_associate(port, source int32, object uintptr, events uint32, user uintptr) int32
84 //extern port_dissociate
85 func port_dissociate(port, source int32, object uintptr) int32
87 //extern port_getn
88 func port_getn(port int32, evs *portevent, max uint32, nget *uint32, timeout *timespec) int32
90 var portfd int32 = -1
92 func netpollinit() {
93 portfd = port_create()
94 if portfd >= 0 {
95 fcntl(portfd, _F_SETFD, _FD_CLOEXEC)
96 return
99 print("runtime: port_create failed (errno=", errno(), ")\n")
100 throw("runtime: netpollinit failed")
103 func netpolldescriptor() uintptr {
104 return uintptr(portfd)
107 func netpollopen(fd uintptr, pd *pollDesc) int32 {
108 lock(&pd.lock)
109 // We don't register for any specific type of events yet, that's
110 // netpollarm's job. We merely ensure we call port_associate before
111 // asynchronous connect/accept completes, so when we actually want
112 // to do any I/O, the call to port_associate (from netpollarm,
113 // with the interested event set) will unblock port_getn right away
114 // because of the I/O readiness notification.
115 pd.user = 0
116 r := port_associate(portfd, _PORT_SOURCE_FD, fd, 0, uintptr(unsafe.Pointer(pd)))
117 unlock(&pd.lock)
118 if r < 0 {
119 return int32(errno())
121 return 0
124 func netpollclose(fd uintptr) int32 {
125 if port_dissociate(portfd, _PORT_SOURCE_FD, fd) < 0 {
126 return int32(errno())
128 return 0
131 // Updates the association with a new set of interested events. After
132 // this call, port_getn will return one and only one event for that
133 // particular descriptor, so this function needs to be called again.
134 func netpollupdate(pd *pollDesc, set, clear uint32) {
135 if pd.closing {
136 return
139 old := pd.user
140 events := (old & ^clear) | set
141 if old == events {
142 return
145 if events != 0 && port_associate(portfd, _PORT_SOURCE_FD, pd.fd, events, uintptr(unsafe.Pointer(pd))) != 0 {
146 print("runtime: port_associate failed (errno=", errno(), ")\n")
147 throw("runtime: netpollupdate failed")
149 pd.user = events
152 // subscribe the fd to the port such that port_getn will return one event.
153 func netpollarm(pd *pollDesc, mode int) {
154 lock(&pd.lock)
155 switch mode {
156 case 'r':
157 netpollupdate(pd, _POLLIN, 0)
158 case 'w':
159 netpollupdate(pd, _POLLOUT, 0)
160 default:
161 throw("runtime: bad mode")
163 unlock(&pd.lock)
166 // polls for ready network connections
167 // returns list of goroutines that become runnable
168 func netpoll(block bool) *g {
169 if portfd == -1 {
170 return nil
173 var wait *timespec
174 var zero timespec
175 if !block {
176 wait = &zero
179 var events [128]portevent
180 retry:
181 var n uint32 = 1
182 if port_getn(portfd, &events[0], uint32(len(events)), &n, wait) < 0 {
183 if e := errno(); e != _EINTR {
184 print("runtime: port_getn on fd ", portfd, " failed (errno=", e, ")\n")
185 throw("runtime: netpoll failed")
187 goto retry
190 var gp guintptr
191 for i := 0; i < int(n); i++ {
192 ev := &events[i]
194 if ev.portev_events == 0 {
195 continue
197 pd := (*pollDesc)(unsafe.Pointer(ev.portev_user))
199 var mode, clear int32
200 if (ev.portev_events & (_POLLIN | _POLLHUP | _POLLERR)) != 0 {
201 mode += 'r'
202 clear |= _POLLIN
204 if (ev.portev_events & (_POLLOUT | _POLLHUP | _POLLERR)) != 0 {
205 mode += 'w'
206 clear |= _POLLOUT
208 // To effect edge-triggered events, we need to be sure to
209 // update our association with whatever events were not
210 // set with the event. For example if we are registered
211 // for POLLIN|POLLOUT, and we get POLLIN, besides waking
212 // the goroutine interested in POLLIN we have to not forget
213 // about the one interested in POLLOUT.
214 if clear != 0 {
215 lock(&pd.lock)
216 netpollupdate(pd, 0, uint32(clear))
217 unlock(&pd.lock)
220 if mode != 0 {
221 netpollready(&gp, pd, mode)
225 if block && gp == 0 {
226 goto retry
228 return gp.ptr()