Debian Codegraph @

libgo/go/runtime/netpoll_epoll.go

raw
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.
4
5 //go:build linux
6
7 package runtime
8
9 import (
10 "runtime/internal/atomic"
11 "unsafe"
12 )
13
14 //extern epoll_create
15 func epollcreate(size int32) int32
16
17 //extern epoll_create1
18 func epollcreate1(flags int32) int32
19
20 //go:noescape
21 //extern epoll_ctl
22 func epollctl(epfd, op, fd int32, ev *epollevent) int32
23
24 //go:noescape
25 //extern epoll_wait
26 func epollwait(epfd int32, ev *epollevent, nev, timeout int32) int32
27
28 var (
29 epfd int32 = -1 // epoll descriptor
30
31 netpollBreakRd, netpollBreakWr uintptr // for netpollBreak
32
33 netpollWakeSig uint32 // used to avoid duplicate calls of netpollBreak
34 )
35
36 func netpollinit() {
37 epfd = epollcreate1(_EPOLL_CLOEXEC)
38 if epfd < 0 {
39 epfd = epollcreate(1024)
40 if epfd < 0 {
41 println("runtime: epollcreate failed with", -epfd)
42 throw("runtime: netpollinit failed")
43 }
44 closeonexec(epfd)
45 }
46 r, w, cerrno := nonblockingPipe()
47 if cerrno != 0 {
48 println("runtime: pipe failed with", cerrno)
49 throw("runtime: pipe failed")
50 }
51 ev := epollevent{
52 events: _EPOLLIN,
53 }
54 *(**uintptr)(unsafe.Pointer(&ev.data)) = &netpollBreakRd
55 if epollctl(epfd, _EPOLL_CTL_ADD, r, &ev) < 0 {
56 cerrno = int32(errno())
57 }
58 if cerrno != 0 {
59 println("runtime: epollctl failed with", cerrno)
60 throw("runtime: epollctl failed")
61 }
62 netpollBreakRd = uintptr(r)
63 netpollBreakWr = uintptr(w)
64 }
65
66 func netpollIsPollDescriptor(fd uintptr) bool {
67 return fd == uintptr(epfd) || fd == netpollBreakRd || fd == netpollBreakWr
68 }
69
70 func netpollopen(fd uintptr, pd *pollDesc) int32 {
71 var ev epollevent
72 ev.events = _EPOLLIN | _EPOLLOUT | _EPOLLRDHUP | _EPOLLETpos
73 *(**pollDesc)(unsafe.Pointer(&ev.data)) = pd
74 if epollctl(epfd, _EPOLL_CTL_ADD, int32(fd), &ev) < 0 {
75 return int32(errno())
76 }
77 return 0
78 }
79
80 func netpollclose(fd uintptr) int32 {
81 var ev epollevent
82 if epollctl(epfd, _EPOLL_CTL_DEL, int32(fd), &ev) < 0 {
83 return int32(errno())
84 }
85 return 0
86 }
87
88 func netpollarm(pd *pollDesc, mode int) {
89 throw("runtime: unused")
90 }
91
92 // netpollBreak interrupts an epollwait.
93 func netpollBreak() {
94 if atomic.Cas(&netpollWakeSig, 0, 1) {
95 for {
96 var b byte
97 n := write(netpollBreakWr, unsafe.Pointer(&b), 1)
98 if n == 1 {
99 break
100 }
101 if n == -_EINTR {
102 continue
103 }
104 if n == -_EAGAIN {
105 return
106 }
107 println("runtime: netpollBreak write failed with", -n)
108 throw("runtime: netpollBreak write failed")
109 }
110 }
111 }
112
113 // netpoll checks for ready network connections.
114 // Returns list of goroutines that become runnable.
115 // delay < 0: blocks indefinitely
116 // delay == 0: does not block, just polls
117 // delay > 0: block for up to that many nanoseconds
118 func netpoll(delay int64) gList {
119 if epfd == -1 {
120 return gList{}
121 }
122 var waitms int32
123 if delay < 0 {
124 waitms = -1
125 } else if delay == 0 {
126 waitms = 0
127 } else if delay < 1e6 {
128 waitms = 1
129 } else if delay < 1e15 {
130 waitms = int32(delay / 1e6)
131 } else {
132 // An arbitrary cap on how long to wait for a timer.
133 // 1e9 ms == ~11.5 days.
134 waitms = 1e9
135 }
136 var events [128]epollevent
137 retry:
138 n := epollwait(epfd, &events[0], int32(len(events)), waitms)
139 if n < 0 {
140 e := errno()
141 if e != _EINTR {
142 println("runtime: epollwait on fd", epfd, "failed with", e)
143 throw("runtime: netpoll failed")
144 }
145 // If a timed sleep was interrupted, just return to
146 // recalculate how long we should sleep now.
147 if waitms > 0 {
148 return gList{}
149 }
150 goto retry
151 }
152 var toRun gList
153 for i := int32(0); i < n; i++ {
154 ev := &events[i]
155 if ev.events == 0 {
156 continue
157 }
158
159 if *(**uintptr)(unsafe.Pointer(&ev.data)) == &netpollBreakRd {
160 if ev.events != _EPOLLIN {
161 println("runtime: netpoll: break fd ready for", ev.events)
162 throw("runtime: netpoll: break fd ready for something unexpected")
163 }
164 if delay != 0 {
165 // netpollBreak could be picked up by a
166 // nonblocking poll. Only read the byte
167 // if blocking.
168 var tmp [16]byte
169 read(int32(netpollBreakRd), noescape(unsafe.Pointer(&tmp[0])), int32(len(tmp)))
170 atomic.Store(&netpollWakeSig, 0)
171 }
172 continue
173 }
174
175 var mode int32
176 if ev.events&(_EPOLLIN|_EPOLLRDHUP|_EPOLLHUP|_EPOLLERR) != 0 {
177 mode += 'r'
178 }
179 if ev.events&(_EPOLLOUT|_EPOLLHUP|_EPOLLERR) != 0 {
180 mode += 'w'
181 }
182 if mode != 0 {
183 pd := *(**pollDesc)(unsafe.Pointer(&ev.data))
184 pd.setEventErr(ev.events == _EPOLLERR)
185 netpollready(&toRun, pd, mode)
186 }
187 }
188 return toRun
189 }