1
0
mirror of https://github.com/golang/go synced 2024-11-22 22:30:02 -07:00

io/ioutil: fix Discard data race

Fixes #4589

R=golang-dev, iant, dvyukov
CC=golang-dev
https://golang.org/cl/7011047
This commit is contained in:
Brad Fitzpatrick 2012-12-28 09:33:22 -08:00
parent 91484c6c48
commit eb43ce2d77
3 changed files with 15 additions and 17 deletions

View File

@ -2,12 +2,22 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !race
package ioutil
var blackHoleBuf = make([]byte, 8192)
var blackHoleBuf = make(chan []byte, 1)
func blackHole() []byte {
return blackHoleBuf
select {
case b := <-blackHoleBuf:
return b
default:
}
return make([]byte, 8192)
}
func blackHolePut(p []byte) {
select {
case blackHoleBuf <- p:
default:
}
}

View File

@ -1,13 +0,0 @@
// Copyright 2012 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.
// +build race
package ioutil
// Replaces the normal fast implementation with slower but formally correct one.
func blackHole() []byte {
return make([]byte, 8192)
}

View File

@ -132,6 +132,7 @@ func (devNull) Write(p []byte) (int, error) {
func (devNull) ReadFrom(r io.Reader) (n int64, err error) {
buf := blackHole()
defer blackHolePut(buf)
readSize := 0
for {
readSize, err = r.Read(buf)