]> Cypherpunks repositories - gostls13.git/commitdiff
io/ioutil: fix Discard data race
authorBrad Fitzpatrick <bradfitz@golang.org>
Fri, 28 Dec 2012 17:33:22 +0000 (09:33 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 28 Dec 2012 17:33:22 +0000 (09:33 -0800)
Fixes #4589

R=golang-dev, iant, dvyukov
CC=golang-dev
https://golang.org/cl/7011047

src/pkg/io/ioutil/blackhole.go
src/pkg/io/ioutil/blackhole_race.go [deleted file]
src/pkg/io/ioutil/ioutil.go

index c127bdb71c08c9aba6f338e0d154c7b8f028c1b8..101d2c12153e6538bd33c3dea8fd65497d6a502c 100644 (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:
+       }
 }
diff --git a/src/pkg/io/ioutil/blackhole_race.go b/src/pkg/io/ioutil/blackhole_race.go
deleted file mode 100644 (file)
index eb640e0..0000000
+++ /dev/null
@@ -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)
-}
index 31c77299eeef7bc6a1d42d100a6af23644a89bb1..0eb146c0ab75598d082aeb4d247d0bd48629874a 100644 (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)