From e0334ab798b573b0dc138d0d91c32a010e1490c0 Mon Sep 17 00:00:00 2001 From: Vinu Rajashekhar Date: Mon, 28 Jun 2010 13:58:56 -0700 Subject: [PATCH] Move the functions, newPollServer and Run, from fd.go to a new source file. R=iant, rsc CC=golang-dev https://golang.org/cl/1670046 --- src/pkg/net/Makefile | 1 + src/pkg/net/fd.go | 75 ------------------------------- src/pkg/net/newpollserver.go | 87 ++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 75 deletions(-) create mode 100644 src/pkg/net/newpollserver.go diff --git a/src/pkg/net/Makefile b/src/pkg/net/Makefile index 466385766b5..7d8eadf241b 100644 --- a/src/pkg/net/Makefile +++ b/src/pkg/net/Makefile @@ -10,6 +10,7 @@ GOFILES=\ dnsclient.go\ dnsconfig.go\ dnsmsg.go\ + newpollserver.go\ fd.go\ fd_$(GOOS).go\ hosts.go\ diff --git a/src/pkg/net/fd.go b/src/pkg/net/fd.go index 2f5930bcc1f..b3713781705 100644 --- a/src/pkg/net/fd.go +++ b/src/pkg/net/fd.go @@ -93,37 +93,6 @@ type pollServer struct { deadline int64 // next deadline (nsec since 1970) } -func newPollServer() (s *pollServer, err os.Error) { - s = new(pollServer) - s.cr = make(chan *netFD, 1) - s.cw = make(chan *netFD, 1) - if s.pr, s.pw, err = os.Pipe(); err != nil { - return nil, err - } - var e int - if e = syscall.SetNonblock(s.pr.Fd(), true); e != 0 { - Errno: - err = &os.PathError{"setnonblock", s.pr.Name(), os.Errno(e)} - Error: - s.pr.Close() - s.pw.Close() - return nil, err - } - if e = syscall.SetNonblock(s.pw.Fd(), true); e != 0 { - goto Errno - } - 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[int]*netFD) - go s.Run() - return s, nil -} - func (s *pollServer) AddFD(fd *netFD, mode int) { intfd := fd.sysfd if intfd < 0 { @@ -225,50 +194,6 @@ func (s *pollServer) CheckDeadlines() { s.deadline = next_deadline } -func (s *pollServer) Run() { - var scratch [100]byte - for { - var t = s.deadline - if t > 0 { - t = t - s.Now() - if t <= 0 { - s.CheckDeadlines() - continue - } - } - fd, mode, err := s.poll.WaitFD(t) - if err != nil { - print("pollServer WaitFD: ", err.String(), "\n") - return - } - if fd < 0 { - // Timeout happened. - s.CheckDeadlines() - continue - } - if fd == s.pr.Fd() { - // Drain our wakeup pipe. - for nn, _ := s.pr.Read(scratch[0:]); nn > 0; { - nn, _ = s.pr.Read(scratch[0:]) - } - // 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 - } - s.WakeFD(netfd, mode) - } - } -} - var wakeupbuf [1]byte func (s *pollServer) Wakeup() { s.pw.Write(wakeupbuf[0:]) } diff --git a/src/pkg/net/newpollserver.go b/src/pkg/net/newpollserver.go new file mode 100644 index 00000000000..67355ae9a7f --- /dev/null +++ b/src/pkg/net/newpollserver.go @@ -0,0 +1,87 @@ +// Copyright 2010 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 ( + "os" + "syscall" +) + +func newPollServer() (s *pollServer, err os.Error) { + s = new(pollServer) + s.cr = make(chan *netFD, 1) + s.cw = make(chan *netFD, 1) + if s.pr, s.pw, err = os.Pipe(); err != nil { + return nil, err + } + var e int + if e = syscall.SetNonblock(s.pr.Fd(), true); e != 0 { + Errno: + err = &os.PathError{"setnonblock", s.pr.Name(), os.Errno(e)} + Error: + s.pr.Close() + s.pw.Close() + return nil, err + } + if e = syscall.SetNonblock(s.pw.Fd(), true); e != 0 { + goto Errno + } + 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[int]*netFD) + go s.Run() + return s, nil +} + +func (s *pollServer) Run() { + var scratch [100]byte + for { + var t = s.deadline + if t > 0 { + t = t - s.Now() + if t <= 0 { + s.CheckDeadlines() + continue + } + } + fd, mode, err := s.poll.WaitFD(t) + if err != nil { + print("pollServer WaitFD: ", err.String(), "\n") + return + } + if fd < 0 { + // Timeout happened. + s.CheckDeadlines() + continue + } + if fd == s.pr.Fd() { + // Drain our wakeup pipe. + for nn, _ := s.pr.Read(scratch[0:]); nn > 0; { + nn, _ = s.pr.Read(scratch[0:]) + } + // 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 + } + s.WakeFD(netfd, mode) + } + } +}