From: Brad Fitzpatrick Date: Fri, 28 Dec 2012 17:33:22 +0000 (-0800) Subject: io/ioutil: fix Discard data race X-Git-Tag: go1.1rc2~1522 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=eb43ce2d7711ad963de4860b70495a7aba3271c5;p=gostls13.git io/ioutil: fix Discard data race Fixes #4589 R=golang-dev, iant, dvyukov CC=golang-dev https://golang.org/cl/7011047 --- diff --git a/src/pkg/io/ioutil/blackhole.go b/src/pkg/io/ioutil/blackhole.go index c127bdb71c..101d2c1215 100644 --- a/src/pkg/io/ioutil/blackhole.go +++ b/src/pkg/io/ioutil/blackhole.go @@ -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 index eb640e05cf..0000000000 --- a/src/pkg/io/ioutil/blackhole_race.go +++ /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) -} diff --git a/src/pkg/io/ioutil/ioutil.go b/src/pkg/io/ioutil/ioutil.go index 31c77299ee..0eb146c0ab 100644 --- a/src/pkg/io/ioutil/ioutil.go +++ b/src/pkg/io/ioutil/ioutil.go @@ -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)