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.
12 func epollcreate(size
int32) int32
14 //extern epoll_create1
15 func epollcreate1(flags
int32) int32
19 func epollctl(epfd
, op
, fd
int32, ev
*epollevent
) int32
23 func epollwait(epfd
int32, ev
*epollevent
, nev
, timeout
int32) int32
25 //extern __go_fcntl_uintptr
26 func fcntlUintptr(fd
, cmd
, arg
uintptr) (uintptr, uintptr)
28 func closeonexec(fd
int32) {
29 fcntlUintptr(uintptr(fd
), _F_SETFD
, _FD_CLOEXEC
)
33 epfd
int32 = -1 // epoll descriptor
37 epfd
= epollcreate1(_EPOLL_CLOEXEC
)
41 epfd
= epollcreate(1024)
46 println("netpollinit: failed to create epoll descriptor", errno())
47 throw("netpollinit: failed to create descriptor")
50 func netpolldescriptor() uintptr {
54 func netpollopen(fd
uintptr, pd
*pollDesc
) int32 {
56 ev
.events
= _EPOLLIN | _EPOLLOUT | _EPOLLRDHUP | _EPOLLETpos
57 *(**pollDesc
)(unsafe
.Pointer(&ev
.data
)) = pd
58 if epollctl(epfd
, _EPOLL_CTL_ADD
, int32(fd
), &ev
) < 0 {
64 func netpollclose(fd
uintptr) int32 {
66 if epollctl(epfd
, _EPOLL_CTL_DEL
, int32(fd
), &ev
) < 0 {
72 func netpollarm(pd
*pollDesc
, mode
int) {
73 throw("runtime: unused")
76 // polls for ready network connections
77 // returns list of goroutines that become runnable
78 func netpoll(block
bool) *g
{
86 var events
[128]epollevent
88 n
:= epollwait(epfd
, &events
[0], int32(len(events
)), waitms
)
92 println("runtime: epollwait on fd", epfd
, "failed with", e
)
93 throw("runtime: netpoll failed")
98 for i
:= int32(0); i
< n
; i
++ {
104 if ev
.events
&(_EPOLLIN|_EPOLLRDHUP|_EPOLLHUP|_EPOLLERR
) != 0 {
107 if ev
.events
&(_EPOLLOUT|_EPOLLHUP|_EPOLLERR
) != 0 {
111 pd
:= *(**pollDesc
)(unsafe
.Pointer(&ev
.data
))
113 netpollready(&gp
, pd
, mode
)
116 if block
&& gp
== 0 {