mirror of
https://github.com/golang/go
synced 2024-11-23 02:00:03 -07:00
io/ioutil: fix data race on rand
Fixes #4212. R=golang-dev, rsc CC=golang-dev https://golang.org/cl/6641050
This commit is contained in:
parent
d901808869
commit
49a5c28a18
@ -8,6 +8,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,18 +17,21 @@ import (
|
|||||||
// chance the file doesn't exist yet - keeps the number of tries in
|
// chance the file doesn't exist yet - keeps the number of tries in
|
||||||
// TempFile to a minimum.
|
// TempFile to a minimum.
|
||||||
var rand uint32
|
var rand uint32
|
||||||
|
var randmu sync.Mutex
|
||||||
|
|
||||||
func reseed() uint32 {
|
func reseed() uint32 {
|
||||||
return uint32(time.Now().UnixNano() + int64(os.Getpid()))
|
return uint32(time.Now().UnixNano() + int64(os.Getpid()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func nextSuffix() string {
|
func nextSuffix() string {
|
||||||
|
randmu.Lock()
|
||||||
r := rand
|
r := rand
|
||||||
if r == 0 {
|
if r == 0 {
|
||||||
r = reseed()
|
r = reseed()
|
||||||
}
|
}
|
||||||
r = r*1664525 + 1013904223 // constants from Numerical Recipes
|
r = r*1664525 + 1013904223 // constants from Numerical Recipes
|
||||||
rand = r
|
rand = r
|
||||||
|
randmu.Unlock()
|
||||||
return strconv.Itoa(int(1e9 + r%1e9))[1:]
|
return strconv.Itoa(int(1e9 + r%1e9))[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user