1
0
mirror of https://github.com/golang/go synced 2024-11-24 23:07:56 -07:00

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
This commit is contained in:
Rob Pike 2010-08-06 06:14:41 +10:00
parent 75f6a0c759
commit c78be462a0
14 changed files with 44 additions and 26 deletions

View File

@ -9,7 +9,7 @@ import (
"crypto/rsa"
"io"
"io/ioutil"
"once"
"sync"
"time"
)
@ -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.
var varDefaultConfig *Config
var once sync.Once
func defaultConfig() *Config {
once.Do(initDefaultConfig)
return varDefaultConfig

View File

@ -7,7 +7,6 @@ package mime
import (
"bufio"
"once"
"os"
"strings"
"sync"
@ -69,6 +68,8 @@ func initMime() {
}
}
var once sync.Once
// TypeByExtension returns the MIME type associated with the file extension ext.
// The extension ext should begin with a leading dot, as in ".html".
// When ext has no associated type, TypeByExtension returns "".

View File

@ -15,9 +15,9 @@
package net
import (
"once"
"os"
"rand"
"sync"
"time"
)
@ -235,11 +235,13 @@ func isDomainName(s string) bool {
return ok
}
var onceLoadConfig sync.Once
func lookup(name string, qtype uint16) (cname string, addrs []dnsRR, err os.Error) {
if !isDomainName(name) {
return name, nil, &DNSError{Error: "invalid domain name", Name: name}
}
once.Do(loadConfig)
onceLoadConfig.Do(loadConfig)
if dnserr != nil || cfg == nil {
err = dnserr
return
@ -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
// host's addresses.
func LookupHost(name string) (cname string, addrs []string, err os.Error) {
once.Do(loadConfig)
onceLoadConfig.Do(loadConfig)
if dnserr != nil || cfg == nil {
err = dnserr
return

View File

@ -8,7 +8,6 @@ package net
import (
"io"
"once"
"os"
"sync"
"syscall"
@ -258,6 +257,7 @@ func (s *pollServer) WaitWrite(fd *netFD) {
// All the network FDs use a single pollServer.
var pollserver *pollServer
var onceStartServer sync.Once
func startServer() {
p, err := newPollServer()
@ -268,7 +268,7 @@ func startServer() {
}
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 {
return nil, &OpError{"setnonblock", net, laddr, os.Errno(e)}
}

View File

@ -5,13 +5,14 @@
package net
import (
"once"
"os"
"sync"
"syscall"
"unsafe"
)
var onceStartServer sync.Once
// BUG(brainman): The Windows implementation does not implement SetTimeout.
// IO completion result parameters.
@ -119,6 +120,7 @@ func (s *pollServer) Run() {
// All the network FDs use a single pollServer.
var pollserver *pollServer
var onceStartServer sync.Once
func startServer() {
p, err := newPollServer()
@ -134,7 +136,7 @@ func newFD(fd, family, proto int, net string, laddr, raddr Addr) (f *netFD, err
if initErr != nil {
return nil, initErr
}
once.Do(startServer)
onceStartServer.Do(startServer)
// Associate our socket with pollserver.iocp.
if _, e := syscall.CreateIoCompletionPort(int32(fd), pollserver.iocp, 0, 0); e != 0 {
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.
syscall.ForkLock.RUnlock()
// Associate our new socket with IOCP.
once.Do(startServer)
onceStartServer.Do(startServer)
if _, e = syscall.CreateIoCompletionPort(int32(s), pollserver.iocp, 0, 0); e != 0 {
return nil, &OpError{"CreateIoCompletionPort", fd.net, fd.laddr, os.Errno(e)}
}

View File

@ -7,11 +7,13 @@
package net
import (
"once"
"os"
"sync"
"syscall"
)
var onceReadProtocols sync.Once
func sockaddrToIP(sa syscall.Sockaddr) Addr {
switch sa := sa.(type) {
case *syscall.SockaddrInet4:
@ -284,7 +286,7 @@ func readProtocols() {
}
func netProtoSplit(netProto string) (net string, proto int, err os.Error) {
once.Do(readProtocols)
onceReadProtocols.Do(readProtocols)
i := last(netProto, ':')
if i+1 >= len(netProto) { // no colon
return "", 0, os.ErrorString("no IP protocol specified")

View File

@ -7,12 +7,13 @@
package net
import (
"once"
"os"
"sync"
)
var services map[string]map[string]int
var servicesError os.Error
var onceReadServices sync.Once
func readServices() {
services = make(map[string]map[string]int)
@ -49,7 +50,7 @@ func readServices() {
// LookupPort looks up the port for the given network and service.
func LookupPort(network, service string) (port int, err os.Error) {
once.Do(readServices)
onceReadServices.Do(readServices)
switch network {
case "tcp4", "tcp6":

View File

@ -7,13 +7,14 @@
package os
import (
"once"
"sync"
)
// ENOENV is the Error indicating that an environment variable does not exist.
var ENOENV = NewError("no such environment variable")
var env map[string]string
var once sync.Once
func copyenv() {

View File

@ -9,14 +9,15 @@ import (
"http"
"log"
"net"
"once"
"os"
"strings"
"sync"
"testing"
)
var serverAddr string
var httpServerAddr string
var once sync.Once
const second = 1e9

View File

@ -5,7 +5,7 @@
package time
import (
"once"
"sync"
)
// A Ticker holds a synchronous channel that delivers `ticks' of a clock
@ -156,6 +156,8 @@ func tickerLoop() {
}
}
var onceStartTickerLoop sync.Once
// NewTicker returns a new Ticker containing a channel that will
// send the time, in nanoseconds, every ns nanoseconds. It adjusts the
// intervals to make up for pauses in delivery of the ticks.
@ -165,7 +167,7 @@ func NewTicker(ns int64) *Ticker {
}
c := make(chan int64, 1) // See comment on send in tickerLoop
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
go func() { newTicker <- t }()
return t

View File

@ -203,6 +203,7 @@ func readinfofile(name string) ([]zonetime, bool) {
}
var zones []zonetime
var onceSetupZone sync.Once
func setupZone() {
// consult $TZ to find the time zone to use.
@ -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.
func lookupTimezone(sec int64) (zone string, offset int) {
once.Do(setupZone)
onceSetupZone.Do(setupZone)
if len(zones) == 0 {
return "UTC", 0
}

View File

@ -11,8 +11,8 @@ package time
import (
"io/ioutil"
"once"
"os"
"sync"
)
const (
@ -203,6 +203,7 @@ func readinfofile(name string) ([]zonetime, bool) {
}
var zones []zonetime
var onceSetupZone sync.Once
func setupZone() {
// consult $TZ to find the time zone to use.
@ -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.
func lookupTimezone(sec int64) (zone string, offset int) {
once.Do(setupZone)
onceSetupZone.Do(setupZone)
if len(zones) == 0 {
return "UTC", 0
}
@ -251,7 +252,7 @@ func lookupTimezone(sec int64) (zone string, offset int) {
// For a system in Sydney, "EST" and "EDT", though they have
// different meanings than they do in New York.
func lookupByName(name string) (off int, found bool) {
once.Do(setupZone)
onceSetupZone.Do(setupZone)
for _, z := range zones {
if name == z.zone.name {
return z.zone.utcoff, true

View File

@ -6,8 +6,8 @@ package time
import (
"syscall"
"os"
"once"
"os"
)
// BUG(brainman): The Windows implementation assumes that
@ -121,6 +121,7 @@ func (zi *zoneinfo) pickZone(t *Time) *zone {
var tz zoneinfo
var initError os.Error
var onceSetupZone sync.Once
func setupZone() {
var i syscall.Timezoneinformation
@ -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.
func lookupTimezone(sec int64) (zone string, offset int) {
once.Do(setupZone)
onceSetupZone.Do(setupZone)
if initError != nil {
return "", 0
}
@ -174,7 +175,7 @@ func lookupTimezone(sec int64) (zone string, offset int) {
// time zone with the given abbreviation. It only considers
// time zones that apply to the current system.
func lookupByName(name string) (off int, found bool) {
once.Do(setupZone)
onceSetupZone.Do(setupZone)
if initError != nil {
return 0, false
}

View File

@ -11,11 +11,12 @@ import (
"io"
"log"
"net"
"once"
"sync"
"testing"
)
var serverAddr string
var once sync.Once
func echoServer(ws *Conn) { io.Copy(ws, ws) }