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.
14 // Map gccgo field names to gc field names.
15 // Eface aka __go_empty_interface.
16 #define type __type_descriptor
19 // Integrated network poller (platform-independent part).
20 // A particular implementation (epoll/kqueue) must define the following functions:
21 // void runtime_netpollinit(void); // to initialize the poller
22 // int32 runtime_netpollopen(int32 fd, PollDesc *pd); // to arm edge-triggered notifications
23 // and associate fd with pd.
24 // An implementation must call the following function to denote that the pd is ready.
25 // void runtime_netpollready(G **gpp, PollDesc *pd, int32 mode);
31 PollDesc* link; // in pollcache, protected by pollcache.Lock
32 Lock; // protectes the following fields
35 uintptr seq; // protects from stale timers and ready notifications
36 G* rg; // G waiting for read or READY (binary semaphore)
37 Timer rt; // read deadline timer (set if rt.fv != nil)
38 int64 rd; // read deadline
39 G* wg; // the same for writes
48 // PollDesc objects must be type-stable,
49 // because we can get ready notification from epoll/kqueue
50 // after the descriptor is closed/reused.
51 // Stale notifications are detected using seq variable,
52 // seq is incremented when deadlines are changed or descriptor is reused.
55 static void netpollblock(PollDesc*, int32);
56 static G* netpollunblock(PollDesc*, int32);
57 static void deadline(int64, Eface);
58 static void readDeadline(int64, Eface);
59 static void writeDeadline(int64, Eface);
60 static PollDesc* allocPollDesc(void);
61 static intgo checkerr(PollDesc *pd, int32 mode);
63 static FuncVal deadlineFn = {(void(*)(void))deadline};
64 static FuncVal readDeadlineFn = {(void(*)(void))readDeadline};
65 static FuncVal writeDeadlineFn = {(void(*)(void))writeDeadline};
67 func runtime_pollServerInit() {
68 runtime_netpollinit();
71 func runtime_pollOpen(fd int) (pd *PollDesc, errno int) {
74 if(pd->wg != nil && pd->wg != READY)
75 runtime_throw("runtime_pollOpen: blocked write on free descriptor");
76 if(pd->rg != nil && pd->rg != READY)
77 runtime_throw("runtime_pollOpen: blocked read on free descriptor");
87 errno = runtime_netpollopen(fd, pd);
90 func runtime_pollClose(pd *PollDesc) {
92 runtime_throw("runtime_pollClose: close w/o unblock");
93 if(pd->wg != nil && pd->wg != READY)
94 runtime_throw("runtime_pollClose: blocked write on closing descriptor");
95 if(pd->rg != nil && pd->rg != READY)
96 runtime_throw("runtime_pollClose: blocked read on closing descriptor");
97 runtime_netpollclose(pd->fd);
98 runtime_lock(&pollcache);
99 pd->link = pollcache.first;
100 pollcache.first = pd;
101 runtime_unlock(&pollcache);
104 func runtime_pollReset(pd *PollDesc, mode int) (err int) {
106 err = checkerr(pd, mode);
117 func runtime_pollWait(pd *PollDesc, mode int) (err int) {
119 err = checkerr(pd, mode);
122 netpollblock(pd, mode);
123 err = checkerr(pd, mode);
128 func runtime_pollSetDeadline(pd *PollDesc, d int64, mode int) {
132 pd->seq++; // invalidate current timers
133 // Reset current timers.
135 runtime_deltimer(&pd->rt);
139 runtime_deltimer(&pd->wt);
143 if(d != 0 && d <= runtime_nanotime()) {
146 if(mode == 'r' || mode == 'r'+'w')
148 if(mode == 'w' || mode == 'r'+'w')
150 if(pd->rd > 0 && pd->rd == pd->wd) {
151 pd->rt.fv = &deadlineFn;
152 pd->rt.when = pd->rd;
153 // Copy current seq into the timer arg.
154 // Timer func will check the seq against current descriptor seq,
155 // if they differ the descriptor was reused or timers were reset.
156 pd->rt.arg.type = (Type*)pd->seq;
157 pd->rt.arg.data = pd;
158 runtime_addtimer(&pd->rt);
161 pd->rt.fv = &readDeadlineFn;
162 pd->rt.when = pd->rd;
163 pd->rt.arg.type = (Type*)pd->seq;
164 pd->rt.arg.data = pd;
165 runtime_addtimer(&pd->rt);
168 pd->wt.fv = &writeDeadlineFn;
169 pd->wt.when = pd->wd;
170 pd->wt.arg.type = (Type*)pd->seq;
171 pd->wt.arg.data = pd;
172 runtime_addtimer(&pd->wt);
179 func runtime_pollUnblock(pd *PollDesc) {
184 runtime_throw("runtime_pollUnblock: already closing");
187 rg = netpollunblock(pd, 'r');
188 wg = netpollunblock(pd, 'w');
190 runtime_deltimer(&pd->rt);
194 runtime_deltimer(&pd->wt);
204 // make pd ready, newly runnable goroutines (if any) are enqueued info gpp list
206 runtime_netpollready(G **gpp, PollDesc *pd, int32 mode)
212 if(mode == 'r' || mode == 'r'+'w')
213 rg = netpollunblock(pd, 'r');
214 if(mode == 'w' || mode == 'r'+'w')
215 wg = netpollunblock(pd, 'w');
218 rg->schedlink = *gpp;
222 wg->schedlink = *gpp;
228 checkerr(PollDesc *pd, int32 mode)
231 return 1; // errClosing
232 if((mode == 'r' && pd->rd < 0) || (mode == 'w' && pd->wd < 0))
233 return 2; // errTimeout
238 netpollblock(PollDesc *pd, int32 mode)
250 runtime_throw("epoll: double wait");
252 runtime_park(runtime_unlock, &pd->Lock, "IO wait");
257 netpollunblock(PollDesc *pd, int32 mode)
276 deadlineimpl(int64 now, Eface arg, bool read, bool write)
283 pd = (PollDesc*)arg.data;
284 // This is the seq when the timer was set.
285 // If it's stale, ignore the timer event.
286 seq = (uintptr)arg.type;
290 // The descriptor was reused or timers were reset.
295 if(pd->rd <= 0 || pd->rt.fv == nil)
296 runtime_throw("deadlineimpl: inconsistent read deadline");
299 rg = netpollunblock(pd, 'r');
302 if(pd->wd <= 0 || (pd->wt.fv == nil && !read))
303 runtime_throw("deadlineimpl: inconsistent write deadline");
306 wg = netpollunblock(pd, 'w');
316 deadline(int64 now, Eface arg)
318 deadlineimpl(now, arg, true, true);
322 readDeadline(int64 now, Eface arg)
324 deadlineimpl(now, arg, true, false);
328 writeDeadline(int64 now, Eface arg)
330 deadlineimpl(now, arg, false, true);
339 runtime_lock(&pollcache);
340 if(pollcache.first == nil) {
341 n = PageSize/sizeof(*pd);
344 // Must be in non-GC memory because can be referenced
345 // only from epoll/kqueue internals.
346 pd = runtime_SysAlloc(n*sizeof(*pd));
347 for(i = 0; i < n; i++) {
348 pd[i].link = pollcache.first;
349 pollcache.first = &pd[i];
352 pd = pollcache.first;
353 pollcache.first = pd->link;
354 runtime_unlock(&pollcache);