1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// TODO(rsc): All the prints in this file should go to standard error.
package net
import (
"net";
"once";
"os";
"syscall";
)
// Network file descriptor. Only intended to be used internally,
// but have to export to make it available in other files implementing package net.
export type FD struct {
// immutable until Close
fd int64;
osfd *os.FD;
cr chan *FD;
cw chan *FD;
// owned by fd wait server
ncr, ncw int;
}
// Make reads and writes on fd return EAGAIN instead of blocking.
func SetNonblock(fd int64) *os.Error {
flags, e := syscall.fcntl(fd, syscall.F_GETFL, 0);
if e != 0 {
return os.ErrnoToError(e)
}
flags, e = syscall.fcntl(fd, syscall.F_SETFL, flags | syscall.O_NONBLOCK);
if e != 0 {
return os.ErrnoToError(e)
}
return nil
}
// A PollServer helps FDs determine when to retry a non-blocking
// read or write after they get EAGAIN. When an FD needs to wait,
// send the fd on s.cr (for a read) or s.cw (for a write) to pass the
// request to the poll server. Then receive on fd.cr/fd.cw.
// When the PollServer finds that i/o on FD should be possible
// again, it will send fd on fd.cr/fd.cw to wake any waiting processes.
// This protocol is implemented as s.WaitRead() and s.WaitWrite().
//
// There is one subtlety: when sending on s.cr/s.cw, the
// poll server is probably in a system call, waiting for an fd
// to become ready. It's not looking at the request channels.
// To resolve this, the poll server waits not just on the FDs it has
// been given but also its own pipe. After sending on the
// buffered channel s.cr/s.cw, WaitRead/WaitWrite writes a
// byte to the pipe, causing the PollServer's poll system call to
// return. In response to the pipe being readable, the PollServer
// re-polls its request channels.
//
// Note that the ordering is "send request" and then "wake up server".
// If the operations were reversed, there would be a race: the poll
// server might wake up and look at the request channel, see that it
// was empty, and go back to sleep, all before the requester managed
// to send the request. Because the send must complete before the wakeup,
// the request channel must be buffered. A buffer of size 1 is sufficient
// for any request load. If many processes are trying to submit requests,
// one will succeed, the PollServer will read the request, and then the
// channel will be empty for the next process's request. A larger buffer
// might help batch requests.
type PollServer struct {
cr, cw chan *FD; // buffered >= 1
pr, pw *os.FD;
pending map[int64] *FD;
poll *Pollster; // low-level OS hooks
}
func (s *PollServer) Run();
func NewPollServer() (s *PollServer, err *os.Error) {
s = new(PollServer);
s.cr = make(chan *FD, 1);
s.cw = make(chan *FD, 1);
if s.pr, s.pw, err = os.Pipe(); err != nil {
return nil, err
}
if err = SetNonblock(s.pr.fd); err != nil {
Error:
s.pr.Close();
s.pw.Close();
return nil, err
}
if err = SetNonblock(s.pw.fd); err != nil {
goto Error
}
if s.poll, err = NewPollster(); err != nil {
goto Error
}
if err = s.poll.AddFD(s.pr.fd, 'r', true); err != nil {
s.poll.Close();
goto Error
}
s.pending = make(map[int64] *FD);
go s.Run();
return s, nil
}
func (s *PollServer) AddFD(fd *FD, mode int) {
if err := s.poll.AddFD(fd.fd, mode, false); err != nil {
print("PollServer AddFD: ", err.String(), "\n");
return
}
key := fd.fd << 1;
if mode == 'r' {
fd.ncr++;
} else {
fd.ncw++;
key++;
}
s.pending[key] = fd
}
func (s *PollServer) LookupFD(fd int64, mode int) *FD {
key := fd << 1;
if mode == 'w' {
key++;
}
netfd, ok := s.pending[key];
if !ok {
return nil
}
s.pending[key] = nil, false;
return netfd
}
func (s *PollServer) Run() {
var scratch [100]byte;
for {
fd, mode, err := s.poll.WaitFD();
if err != nil {
print("PollServer WaitFD: ", err.String(), "\n");
return
}
if fd == s.pr.fd {
// Drain our wakeup pipe.
for nn, e := s.pr.Read(scratch); nn > 0; {
nn, e = s.pr.Read(scratch)
}
// Read from channels
for fd, ok := <-s.cr; ok; fd, ok = <-s.cr {
s.AddFD(fd, 'r')
}
for fd, ok := <-s.cw; ok; fd, ok = <-s.cw {
s.AddFD(fd, 'w')
}
} else {
netfd := s.LookupFD(fd, mode);
if netfd == nil {
print("PollServer: unexpected wakeup for fd=", netfd, " mode=", string(mode), "\n");
continue
}
if mode == 'r' {
for netfd.ncr > 0 {
netfd.ncr--;
netfd.cr <- netfd
}
} else {
for netfd.ncw > 0 {
netfd.ncw--;
netfd.cw <- netfd
}
}
}
}
}
func (s *PollServer) Wakeup() {
var b [1]byte;
s.pw.Write(b)
}
func (s *PollServer) WaitRead(fd *FD) {
s.cr <- fd;
s.Wakeup();
<-fd.cr
}
func (s *PollServer) WaitWrite(fd *FD) {
s.cr <- fd;
s.Wakeup();
<-fd.cr
}
// Network FD methods.
// All the network FDs use a single PollServer.
var pollserver *PollServer
func StartServer() {
p, err := NewPollServer();
if err != nil {
print("Start PollServer: ", err.String(), "\n")
}
pollserver = p
}
export func NewFD(fd int64) (f *FD, err *os.Error) {
if pollserver == nil {
once.Do(&StartServer);
}
if err = SetNonblock(fd); err != nil {
return nil, err
}
f = new(FD);
f.fd = fd;
f.osfd = os.NewFD(fd);
f.cr = make(chan *FD, 1);
f.cw = make(chan *FD, 1);
return f, nil
}
func (fd *FD) Close() *os.Error {
if fd == nil || fd.osfd == nil {
return os.EINVAL
}
e := fd.osfd.Close();
fd.osfd = nil;
fd.fd = -1;
return e
}
func (fd *FD) Read(p []byte) (n int, err *os.Error) {
if fd == nil || fd.osfd == nil {
return -1, os.EINVAL
}
n, err = fd.osfd.Read(p);
for err == os.EAGAIN {
pollserver.WaitRead(fd);
n, err = fd.osfd.Read(p)
}
return n, err
}
func (fd *FD) Write(p []byte) (n int, err *os.Error) {
if fd == nil || fd.osfd == nil {
return -1, os.EINVAL
}
// TODO(rsc): Lock fd while writing to avoid interlacing writes.
err = nil;
nn := 0;
for nn < len(p) && err == nil {
// TODO(rsc): If os.FD.Write loops, have to use syscall instead.
n, err = fd.osfd.Write(p[nn:len(p)]);
for err == os.EAGAIN {
pollserver.WaitWrite(fd);
n, err = fd.osfd.Write(p[nn:len(p)])
}
if n > 0 {
nn += n
}
if n == 0 {
break
}
}
return nn, err
}
func (fd *FD) Accept(sa *syscall.Sockaddr) (nfd *FD, err *os.Error) {
if fd == nil || fd.osfd == nil {
return nil, os.EINVAL
}
s, e := syscall.accept(fd.fd, sa);
for e == syscall.EAGAIN {
pollserver.WaitRead(fd);
s, e = syscall.accept(fd.fd, sa)
}
if e != 0 {
return nil, os.ErrnoToError(e)
}
if nfd, err = NewFD(s); err != nil {
syscall.close(s);
return nil, err
}
return nfd, nil
}
|