Commit c78be462 authored by Rob Pike's avatar Rob Pike

once: replace all uses of package once with sync.Once.

package once remains for now; will be deleted after next release.

R=golang-dev, brainman
CC=golang-dev
https://golang.org/cl/1914046
parent 75f6a0c7
...@@ -9,7 +9,7 @@ import ( ...@@ -9,7 +9,7 @@ import (
"crypto/rsa" "crypto/rsa"
"io" "io"
"io/ioutil" "io/ioutil"
"once" "sync"
"time" "time"
) )
...@@ -127,6 +127,8 @@ func mutualVersion(vers uint16) (uint16, bool) { ...@@ -127,6 +127,8 @@ func mutualVersion(vers uint16) (uint16, bool) {
// The defaultConfig is used in place of a nil *Config in the TLS server and client. // The defaultConfig is used in place of a nil *Config in the TLS server and client.
var varDefaultConfig *Config var varDefaultConfig *Config
var once sync.Once
func defaultConfig() *Config { func defaultConfig() *Config {
once.Do(initDefaultConfig) once.Do(initDefaultConfig)
return varDefaultConfig return varDefaultConfig
......
...@@ -7,7 +7,6 @@ package mime ...@@ -7,7 +7,6 @@ package mime
import ( import (
"bufio" "bufio"
"once"
"os" "os"
"strings" "strings"
"sync" "sync"
...@@ -69,6 +68,8 @@ func initMime() { ...@@ -69,6 +68,8 @@ func initMime() {
} }
} }
var once sync.Once
// TypeByExtension returns the MIME type associated with the file extension ext. // TypeByExtension returns the MIME type associated with the file extension ext.
// The extension ext should begin with a leading dot, as in ".html". // The extension ext should begin with a leading dot, as in ".html".
// When ext has no associated type, TypeByExtension returns "". // When ext has no associated type, TypeByExtension returns "".
......
...@@ -15,9 +15,9 @@ ...@@ -15,9 +15,9 @@
package net package net
import ( import (
"once"
"os" "os"
"rand" "rand"
"sync"
"time" "time"
) )
...@@ -235,11 +235,13 @@ func isDomainName(s string) bool { ...@@ -235,11 +235,13 @@ func isDomainName(s string) bool {
return ok return ok
} }
var onceLoadConfig sync.Once
func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err os.Error) { func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err os.Error) {
if !isDomainName(name) { if !isDomainName(name) {
return name, nil, &DNSError{Error: "invalid domain name", Name: name} return name, nil, &DNSError{Error: "invalid domain name", Name: name}
} }
once.Do(loadConfig) onceLoadConfig.Do(loadConfig)
if dnserr != nil || cfg == nil { if dnserr != nil || cfg == nil {
err = dnserr err = dnserr
return return
...@@ -293,7 +295,7 @@ func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err os.Erro ...@@ -293,7 +295,7 @@ func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err os.Erro
// It returns the canonical name for the host and an array of that // It returns the canonical name for the host and an array of that
// host's addresses. // host's addresses.
func LookupHost(name string) (cname string, addrs []string, err os.Error) { func LookupHost(name string) (cname string, addrs []string, err os.Error) {
once.Do(loadConfig) onceLoadConfig.Do(loadConfig)
if dnserr != nil || cfg == nil { if dnserr != nil || cfg == nil {
err = dnserr err = dnserr
return return
......
...@@ -8,7 +8,6 @@ package net ...@@ -8,7 +8,6 @@ package net
import ( import (
"io" "io"
"once"
"os" "os"
"sync" "sync"
"syscall" "syscall"
...@@ -258,6 +257,7 @@ func (s *pollServer) WaitWrite(fd *netFD) { ...@@ -258,6 +257,7 @@ func (s *pollServer) WaitWrite(fd *netFD) {
// All the network FDs use a single pollServer. // All the network FDs use a single pollServer.
var pollserver *pollServer var pollserver *pollServer
var onceStartServer sync.Once
func startServer() { func startServer() {
p, err := newPollServer() p, err := newPollServer()
...@@ -268,7 +268,7 @@ func startServer() { ...@@ -268,7 +268,7 @@ func startServer() {
} }
func newFD(fd, family, proto int, net string, laddr, raddr Addr) (f *netFD, err os.Error) { func newFD(fd, family, proto int, net string, laddr, raddr Addr) (f *netFD, err os.Error) {
once.Do(startServer) onceStartServer.Do(startServer)
if e := syscall.SetNonblock(fd, true); e != 0 { if e := syscall.SetNonblock(fd, true); e != 0 {
return nil, &OpError{"setnonblock", net, laddr, os.Errno(e)} return nil, &OpError{"setnonblock", net, laddr, os.Errno(e)}
} }
......
...@@ -5,13 +5,14 @@ ...@@ -5,13 +5,14 @@
package net package net
import ( import (
"once"
"os" "os"
"sync" "sync"
"syscall" "syscall"
"unsafe" "unsafe"
) )
var onceStartServer sync.Once
// BUG(brainman): The Windows implementation does not implement SetTimeout. // BUG(brainman): The Windows implementation does not implement SetTimeout.
// IO completion result parameters. // IO completion result parameters.
...@@ -119,6 +120,7 @@ func (s *pollServer) Run() { ...@@ -119,6 +120,7 @@ func (s *pollServer) Run() {
// All the network FDs use a single pollServer. // All the network FDs use a single pollServer.
var pollserver *pollServer var pollserver *pollServer
var onceStartServer sync.Once
func startServer() { func startServer() {
p, err := newPollServer() p, err := newPollServer()
...@@ -134,7 +136,7 @@ func newFD(fd, family, proto int, net string, laddr, raddr Addr) (f *netFD, err ...@@ -134,7 +136,7 @@ func newFD(fd, family, proto int, net string, laddr, raddr Addr) (f *netFD, err
if initErr != nil { if initErr != nil {
return nil, initErr return nil, initErr
} }
once.Do(startServer) onceStartServer.Do(startServer)
// Associate our socket with pollserver.iocp. // Associate our socket with pollserver.iocp.
if _, e := syscall.CreateIoCompletionPort(int32(fd), pollserver.iocp, 0, 0); e != 0 { if _, e := syscall.CreateIoCompletionPort(int32(fd), pollserver.iocp, 0, 0); e != 0 {
return nil, &OpError{"CreateIoCompletionPort", net, laddr, os.Errno(e)} return nil, &OpError{"CreateIoCompletionPort", net, laddr, os.Errno(e)}
...@@ -303,7 +305,7 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err os. ...@@ -303,7 +305,7 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (nfd *netFD, err os.
syscall.ForkLock.RUnlock() syscall.ForkLock.RUnlock()
// Associate our new socket with IOCP. // Associate our new socket with IOCP.
once.Do(startServer) onceStartServer.Do(startServer)
if _, e = syscall.CreateIoCompletionPort(int32(s), pollserver.iocp, 0, 0); e != 0 { if _, e = syscall.CreateIoCompletionPort(int32(s), pollserver.iocp, 0, 0); e != 0 {
return nil, &OpError{"CreateIoCompletionPort", fd.net, fd.laddr, os.Errno(e)} return nil, &OpError{"CreateIoCompletionPort", fd.net, fd.laddr, os.Errno(e)}
} }
......
...@@ -7,11 +7,13 @@ ...@@ -7,11 +7,13 @@
package net package net
import ( import (
"once"
"os" "os"
"sync"
"syscall" "syscall"
) )
var onceReadProtocols sync.Once
func sockaddrToIP(sa syscall.Sockaddr) Addr { func sockaddrToIP(sa syscall.Sockaddr) Addr {
switch sa := sa.(type) { switch sa := sa.(type) {
case *syscall.SockaddrInet4: case *syscall.SockaddrInet4:
...@@ -284,7 +286,7 @@ func readProtocols() { ...@@ -284,7 +286,7 @@ func readProtocols() {
} }
func netProtoSplit(netProto string) (net string, proto int, err os.Error) { func netProtoSplit(netProto string) (net string, proto int, err os.Error) {
once.Do(readProtocols) onceReadProtocols.Do(readProtocols)
i := last(netProto, ':') i := last(netProto, ':')
if i+1 >= len(netProto) { // no colon if i+1 >= len(netProto) { // no colon
return "", 0, os.ErrorString("no IP protocol specified") return "", 0, os.ErrorString("no IP protocol specified")
......
...@@ -7,12 +7,13 @@ ...@@ -7,12 +7,13 @@
package net package net
import ( import (
"once"
"os" "os"
"sync"
) )
var services map[string]map[string]int var services map[string]map[string]int
var servicesError os.Error var servicesError os.Error
var onceReadServices sync.Once
func readServices() { func readServices() {
services = make(map[string]map[string]int) services = make(map[string]map[string]int)
...@@ -49,7 +50,7 @@ func readServices() { ...@@ -49,7 +50,7 @@ func readServices() {
// LookupPort looks up the port for the given network and service. // LookupPort looks up the port for the given network and service.
func LookupPort(network, service string) (port int, err os.Error) { func LookupPort(network, service string) (port int, err os.Error) {
once.Do(readServices) onceReadServices.Do(readServices)
switch network { switch network {
case "tcp4", "tcp6": case "tcp4", "tcp6":
......
...@@ -7,13 +7,14 @@ ...@@ -7,13 +7,14 @@
package os package os
import ( import (
"once" "sync"
) )
// ENOENV is the Error indicating that an environment variable does not exist. // ENOENV is the Error indicating that an environment variable does not exist.
var ENOENV = NewError("no such environment variable") var ENOENV = NewError("no such environment variable")
var env map[string]string var env map[string]string
var once sync.Once
func copyenv() { func copyenv() {
......
...@@ -9,14 +9,15 @@ import ( ...@@ -9,14 +9,15 @@ import (
"http" "http"
"log" "log"
"net" "net"
"once"
"os" "os"
"strings" "strings"
"sync"
"testing" "testing"
) )
var serverAddr string var serverAddr string
var httpServerAddr string var httpServerAddr string
var once sync.Once
const second = 1e9 const second = 1e9
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
package time package time
import ( import (
"once" "sync"
) )
// A Ticker holds a synchronous channel that delivers `ticks' of a clock // A Ticker holds a synchronous channel that delivers `ticks' of a clock
...@@ -156,6 +156,8 @@ func tickerLoop() { ...@@ -156,6 +156,8 @@ func tickerLoop() {
} }
} }
var onceStartTickerLoop sync.Once
// NewTicker returns a new Ticker containing a channel that will // NewTicker returns a new Ticker containing a channel that will
// send the time, in nanoseconds, every ns nanoseconds. It adjusts the // send the time, in nanoseconds, every ns nanoseconds. It adjusts the
// intervals to make up for pauses in delivery of the ticks. // intervals to make up for pauses in delivery of the ticks.
...@@ -165,7 +167,7 @@ func NewTicker(ns int64) *Ticker { ...@@ -165,7 +167,7 @@ func NewTicker(ns int64) *Ticker {
} }
c := make(chan int64, 1) // See comment on send in tickerLoop c := make(chan int64, 1) // See comment on send in tickerLoop
t := &Ticker{c, c, ns, false, Nanoseconds() + ns, nil} t := &Ticker{c, c, ns, false, Nanoseconds() + ns, nil}
once.Do(startTickerLoop) onceStartTickerLoop.Do(startTickerLoop)
// must be run in background so global Tickers can be created // must be run in background so global Tickers can be created
go func() { newTicker <- t }() go func() { newTicker <- t }()
return t return t
......
...@@ -203,6 +203,7 @@ func readinfofile(name string) ([]zonetime, bool) { ...@@ -203,6 +203,7 @@ func readinfofile(name string) ([]zonetime, bool) {
} }
var zones []zonetime var zones []zonetime
var onceSetupZone sync.Once
func setupZone() { func setupZone() {
// consult $TZ to find the time zone to use. // consult $TZ to find the time zone to use.
...@@ -223,7 +224,7 @@ func setupZone() { ...@@ -223,7 +224,7 @@ func setupZone() {
// Look up the correct time zone (daylight savings or not) for the given unix time, in the current location. // Look up the correct time zone (daylight savings or not) for the given unix time, in the current location.
func lookupTimezone(sec int64) (zone string, offset int) { func lookupTimezone(sec int64) (zone string, offset int) {
once.Do(setupZone) onceSetupZone.Do(setupZone)
if len(zones) == 0 { if len(zones) == 0 {
return "UTC", 0 return "UTC", 0
} }
......
...@@ -11,8 +11,8 @@ package time ...@@ -11,8 +11,8 @@ package time
import ( import (
"io/ioutil" "io/ioutil"
"once"
"os" "os"
"sync"
) )
const ( const (
...@@ -203,6 +203,7 @@ func readinfofile(name string) ([]zonetime, bool) { ...@@ -203,6 +203,7 @@ func readinfofile(name string) ([]zonetime, bool) {
} }
var zones []zonetime var zones []zonetime
var onceSetupZone sync.Once
func setupZone() { func setupZone() {
// consult $TZ to find the time zone to use. // consult $TZ to find the time zone to use.
...@@ -223,7 +224,7 @@ func setupZone() { ...@@ -223,7 +224,7 @@ func setupZone() {
// Look up the correct time zone (daylight savings or not) for the given unix time, in the current location. // Look up the correct time zone (daylight savings or not) for the given unix time, in the current location.
func lookupTimezone(sec int64) (zone string, offset int) { func lookupTimezone(sec int64) (zone string, offset int) {
once.Do(setupZone) onceSetupZone.Do(setupZone)
if len(zones) == 0 { if len(zones) == 0 {
return "UTC", 0 return "UTC", 0
} }
...@@ -251,7 +252,7 @@ func lookupTimezone(sec int64) (zone string, offset int) { ...@@ -251,7 +252,7 @@ func lookupTimezone(sec int64) (zone string, offset int) {
// For a system in Sydney, "EST" and "EDT", though they have // For a system in Sydney, "EST" and "EDT", though they have
// different meanings than they do in New York. // different meanings than they do in New York.
func lookupByName(name string) (off int, found bool) { func lookupByName(name string) (off int, found bool) {
once.Do(setupZone) onceSetupZone.Do(setupZone)
for _, z := range zones { for _, z := range zones {
if name == z.zone.name { if name == z.zone.name {
return z.zone.utcoff, true return z.zone.utcoff, true
......
...@@ -6,8 +6,8 @@ package time ...@@ -6,8 +6,8 @@ package time
import ( import (
"syscall" "syscall"
"os"
"once" "once"
"os"
) )
// BUG(brainman): The Windows implementation assumes that // BUG(brainman): The Windows implementation assumes that
...@@ -121,6 +121,7 @@ func (zi *zoneinfo) pickZone(t *Time) *zone { ...@@ -121,6 +121,7 @@ func (zi *zoneinfo) pickZone(t *Time) *zone {
var tz zoneinfo var tz zoneinfo
var initError os.Error var initError os.Error
var onceSetupZone sync.Once
func setupZone() { func setupZone() {
var i syscall.Timezoneinformation var i syscall.Timezoneinformation
...@@ -145,7 +146,7 @@ func setupZone() { ...@@ -145,7 +146,7 @@ func setupZone() {
// Look up the correct time zone (daylight savings or not) for the given unix time, in the current location. // Look up the correct time zone (daylight savings or not) for the given unix time, in the current location.
func lookupTimezone(sec int64) (zone string, offset int) { func lookupTimezone(sec int64) (zone string, offset int) {
once.Do(setupZone) onceSetupZone.Do(setupZone)
if initError != nil { if initError != nil {
return "", 0 return "", 0
} }
...@@ -174,7 +175,7 @@ func lookupTimezone(sec int64) (zone string, offset int) { ...@@ -174,7 +175,7 @@ func lookupTimezone(sec int64) (zone string, offset int) {
// time zone with the given abbreviation. It only considers // time zone with the given abbreviation. It only considers
// time zones that apply to the current system. // time zones that apply to the current system.
func lookupByName(name string) (off int, found bool) { func lookupByName(name string) (off int, found bool) {
once.Do(setupZone) onceSetupZone.Do(setupZone)
if initError != nil { if initError != nil {
return 0, false return 0, false
} }
......
...@@ -11,11 +11,12 @@ import ( ...@@ -11,11 +11,12 @@ import (
"io" "io"
"log" "log"
"net" "net"
"once" "sync"
"testing" "testing"
) )
var serverAddr string var serverAddr string
var once sync.Once
func echoServer(ws *Conn) { io.Copy(ws, ws) } func echoServer(ws *Conn) { io.Copy(ws, ws) }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment