runtime: add go:noescape declaration for Solaris
[official-gcc.git] / libgo / go / runtime / netpoll_solaris.go
bloba960e93b8bf022a62d64baeeb01cedde698725c5
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 //go:noescape
88 //extern port_getn
89 func port_getn(port int32, evs *portevent, max uint32, nget *uint32, timeout *timespec) int32
91 var portfd int32 = -1
93 func netpollinit() {
94 portfd = port_create()
95 if portfd >= 0 {
96 fcntl(portfd, _F_SETFD, _FD_CLOEXEC)
97 return
100 print("runtime: port_create failed (errno=", errno(), ")\n")
101 throw("runtime: netpollinit failed")
104 func netpolldescriptor() uintptr {
105 return uintptr(portfd)
108 func netpollopen(fd uintptr, pd *pollDesc) int32 {
109 lock(&pd.lock)
110 // We don't register for any specific type of events yet, that's
111 // netpollarm's job. We merely ensure we call port_associate before
112 // asynchronous connect/accept completes, so when we actually want
113 // to do any I/O, the call to port_associate (from netpollarm,
114 // with the interested event set) will unblock port_getn right away
115 // because of the I/O readiness notification.
116 pd.user = 0
117 r := port_associate(portfd, _PORT_SOURCE_FD, fd, 0, uintptr(unsafe.Pointer(pd)))
118 unlock(&pd.lock)
119 if r < 0 {
120 return int32(errno())
122 return 0
125 func netpollclose(fd uintptr) int32 {
126 if port_dissociate(portfd, _PORT_SOURCE_FD, fd) < 0 {
127 return int32(errno())
129 return 0
132 // Updates the association with a new set of interested events. After
133 // this call, port_getn will return one and only one event for that
134 // particular descriptor, so this function needs to be called again.
135 func netpollupdate(pd *pollDesc, set, clear uint32) {
136 if pd.closing {
137 return
140 old := pd.user
141 events := (old & ^clear) | set
142 if old == events {
143 return
146 if events != 0 && port_associate(portfd, _PORT_SOURCE_FD, pd.fd, events, uintptr(unsafe.Pointer(pd))) != 0 {
147 print("runtime: port_associate failed (errno=", errno(), ")\n")
148 throw("runtime: netpollupdate failed")
150 pd.user = events
153 // subscribe the fd to the port such that port_getn will return one event.
154 func netpollarm(pd *pollDesc, mode int) {
155 lock(&pd.lock)
156 switch mode {
157 case 'r':
158 netpollupdate(pd, _POLLIN, 0)
159 case 'w':
160 netpollupdate(pd, _POLLOUT, 0)
161 default:
162 throw("runtime: bad mode")
164 unlock(&pd.lock)
167 // polls for ready network connections
168 // returns list of goroutines that become runnable
169 func netpoll(block bool) *g {
170 if portfd == -1 {
171 return nil
174 var wait *timespec
175 var zero timespec
176 if !block {
177 wait = &zero
180 var events [128]portevent
181 retry:
182 var n uint32 = 1
183 if port_getn(portfd, &events[0], uint32(len(events)), &n, wait) < 0 {
184 if e := errno(); e != _EINTR {
185 print("runtime: port_getn on fd ", portfd, " failed (errno=", e, ")\n")
186 throw("runtime: netpoll failed")
188 goto retry
191 var gp guintptr
192 for i := 0; i < int(n); i++ {
193 ev := &events[i]
195 if ev.portev_events == 0 {
196 continue
198 pd := (*pollDesc)(unsafe.Pointer(ev.portev_user))
200 var mode, clear int32
201 if (ev.portev_events & (_POLLIN | _POLLHUP | _POLLERR)) != 0 {
202 mode += 'r'
203 clear |= _POLLIN
205 if (ev.portev_events & (_POLLOUT | _POLLHUP | _POLLERR)) != 0 {
206 mode += 'w'
207 clear |= _POLLOUT
209 // To effect edge-triggered events, we need to be sure to
210 // update our association with whatever events were not
211 // set with the event. For example if we are registered
212 // for POLLIN|POLLOUT, and we get POLLIN, besides waking
213 // the goroutine interested in POLLIN we have to not forget
214 // about the one interested in POLLOUT.
215 if clear != 0 {
216 lock(&pd.lock)
217 netpollupdate(pd, 0, uint32(clear))
218 unlock(&pd.lock)
221 if mode != 0 {
222 netpollready(&gp, pd, mode)
226 if block && gp == 0 {
227 goto retry
229 return gp.ptr()