Source file src/pkg/net/newpollserver.go
1 // Copyright 2010 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 // +build darwin freebsd linux netbsd openbsd
6
7 package net
8
9 import (
10 "os"
11 "syscall"
12 )
13
14 func newPollServer() (s *pollServer, err error) {
15 s = new(pollServer)
16 s.cr = make(chan *netFD, 1)
17 s.cw = make(chan *netFD, 1)
18 if s.pr, s.pw, err = os.Pipe(); err != nil {
19 return nil, err
20 }
21 if err = syscall.SetNonblock(int(s.pr.Fd()), true); err != nil {
22 goto Errno
23 }
24 if err = syscall.SetNonblock(int(s.pw.Fd()), true); err != nil {
25 goto Errno
26 }
27 if s.poll, err = newpollster(); err != nil {
28 goto Error
29 }
30 if _, err = s.poll.AddFD(int(s.pr.Fd()), 'r', true); err != nil {
31 s.poll.Close()
32 goto Error
33 }
34 s.pending = make(map[int]*netFD)
35 go s.Run()
36 return s, nil
37
38 Errno:
39 err = &os.PathError{
40 Op: "setnonblock",
41 Path: s.pr.Name(),
42 Err: err,
43 }
44 Error:
45 s.pr.Close()
46 s.pw.Close()
47 return nil, err
48 }