* config/i386/i386.c (ix86_avoid_lea_for_addr): Return false
[official-gcc.git] / libgo / runtime / netpoll_kqueue.c
blob5d3f85617b6d1325d9c7aa61563bd14b9946c493
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.
5 // +build darwin dragonfly freebsd netbsd openbsd
7 #include "runtime.h"
8 #include "defs.h"
9 #include "malloc.h"
11 // Integrated network poller (kqueue-based implementation).
13 int32 runtime_kqueue(void);
14 int32 runtime_kevent(int32, Kevent*, int32, Kevent*, int32, Timespec*);
15 void runtime_closeonexec(int32);
17 static int32 kq = -1;
19 void
20 runtime_netpollinit(void)
22 kq = runtime_kqueue();
23 if(kq < 0) {
24 runtime_printf("netpollinit: kqueue failed with %d\n", -kq);
25 runtime_throw("netpollinit: kqueue failed");
27 runtime_closeonexec(kq);
30 int32
31 runtime_netpollopen(uintptr fd, PollDesc *pd)
33 Kevent ev[2];
34 int32 n;
36 // Arm both EVFILT_READ and EVFILT_WRITE in edge-triggered mode (EV_CLEAR)
37 // for the whole fd lifetime. The notifications are automatically unregistered
38 // when fd is closed.
39 ev[0].ident = (uint32)fd;
40 ev[0].filter = EVFILT_READ;
41 ev[0].flags = EV_ADD|EV_CLEAR;
42 ev[0].fflags = 0;
43 ev[0].data = 0;
44 ev[0].udata = (kevent_udata)pd;
45 ev[1] = ev[0];
46 ev[1].filter = EVFILT_WRITE;
47 n = runtime_kevent(kq, ev, 2, nil, 0, nil);
48 if(n < 0)
49 return -n;
50 return 0;
53 int32
54 runtime_netpollclose(uintptr fd)
56 // Don't need to unregister because calling close()
57 // on fd will remove any kevents that reference the descriptor.
58 USED(fd);
59 return 0;
62 // Polls for ready network connections.
63 // Returns list of goroutines that become runnable.
65 runtime_netpoll(bool block)
67 static int32 lasterr;
68 Kevent events[64], *ev;
69 Timespec ts, *tp;
70 int32 n, i, mode;
71 G *gp;
73 if(kq == -1)
74 return nil;
75 tp = nil;
76 if(!block) {
77 ts.tv_sec = 0;
78 ts.tv_nsec = 0;
79 tp = &ts;
81 gp = nil;
82 retry:
83 n = runtime_kevent(kq, nil, 0, events, nelem(events), tp);
84 if(n < 0) {
85 if(n != -EINTR && n != lasterr) {
86 lasterr = n;
87 runtime_printf("runtime: kevent on fd %d failed with %d\n", kq, -n);
89 goto retry;
91 for(i = 0; i < n; i++) {
92 ev = &events[i];
93 mode = 0;
94 if(ev->filter == EVFILT_READ)
95 mode += 'r';
96 if(ev->filter == EVFILT_WRITE)
97 mode += 'w';
98 if(mode)
99 runtime_netpollready(&gp, (PollDesc*)ev->udata, mode);
101 if(block && gp == nil)
102 goto retry;
103 return gp;
106 void
107 runtime_netpoll_scan(void (*addroot)(Obj))
109 USED(addroot);