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
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
);
20 runtime_netpollinit(void)
22 kq
= runtime_kqueue();
24 runtime_printf("netpollinit: kqueue failed with %d\n", -kq
);
25 runtime_throw("netpollinit: kqueue failed");
27 runtime_closeonexec(kq
);
31 runtime_netpollopen(uintptr fd
, PollDesc
*pd
)
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
39 ev
[0].ident
= (uint32
)fd
;
40 ev
[0].filter
= EVFILT_READ
;
41 ev
[0].flags
= EV_ADD
|EV_CLEAR
;
44 ev
[0].udata
= (kevent_udata
)pd
;
46 ev
[1].filter
= EVFILT_WRITE
;
47 n
= runtime_kevent(kq
, ev
, 2, nil
, 0, nil
);
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.
63 runtime_netpollarm(PollDesc
* pd
, int32 mode
)
66 runtime_throw("unused");
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
++) {
101 if(ev
->filter
== EVFILT_READ
)
103 if(ev
->filter
== EVFILT_WRITE
)
106 runtime_netpollready(&gp
, (PollDesc
*)ev
->udata
, mode
);
108 if(block
&& gp
== nil
)
114 runtime_netpoll_scan(struct Workbuf
** wbufp
, void (*enqueue1
)(struct Workbuf
**, Obj
))