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.
8 #include "defs_GOOS_GOARCH.h"
10 // Integrated network poller (kqueue-based implementation).
12 int32 runtime_kqueue(void);
13 int32 runtime_kevent(int32, Kevent*, int32, Kevent*, int32, Timespec*);
14 void runtime_closeonexec(int32);
19 runtime_netpollinit(void)
21 kq = runtime_kqueue();
23 runtime_printf("netpollinit: kqueue failed with %d\n", -kq);
24 runtime_throw("netpollinit: kqueue failed");
26 runtime_closeonexec(kq);
30 runtime_netpollopen(int32 fd, PollDesc *pd)
35 // Arm both EVFILT_READ and EVFILT_WRITE in edge-triggered mode (EV_CLEAR)
36 // for the whole fd lifetime. The notifications are automatically unregistered
39 ev[0].filter = EVFILT_READ;
40 ev[0].flags = EV_ADD|EV_RECEIPT|EV_CLEAR;
43 ev[0].udata = (byte*)pd;
45 ev[1].filter = EVFILT_WRITE;
46 n = runtime_kevent(kq, ev, 2, ev, 2, nil);
50 (ev[0].flags&EV_ERROR) == 0 || ev[0].ident != fd || ev[0].filter != EVFILT_READ ||
51 (ev[1].flags&EV_ERROR) == 0 || ev[1].ident != fd || ev[1].filter != EVFILT_WRITE)
52 return EFAULT; // just to mark out from other errors
61 runtime_netpollclose(int32 fd)
63 // Don't need to unregister because calling close()
64 // on fd will remove any kevents that reference the descriptor.
69 // Polls for ready network connections.
70 // Returns list of goroutines that become runnable.
72 runtime_netpoll(bool block)
75 Kevent events[64], *ev;
90 n = runtime_kevent(kq, nil, 0, events, nelem(events), tp);
92 if(n != -EINTR && n != lasterr) {
94 runtime_printf("runtime: kevent on fd %d failed with %d\n", kq, -n);
98 for(i = 0; i < n; i++) {
100 if(ev->filter == EVFILT_READ)
101 runtime_netpollready(&gp, (PollDesc*)ev->udata, 'r');
102 if(ev->filter == EVFILT_WRITE)
103 runtime_netpollready(&gp, (PollDesc*)ev->udata, 'w');
105 if(block && gp == nil)