From eb43ce2d7711ad963de4860b70495a7aba3271c5 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 28 Dec 2012 09:33:22 -0800 Subject: [PATCH] io/ioutil: fix Discard data race Fixes #4589 R=golang-dev, iant, dvyukov CC=golang-dev https://golang.org/cl/7011047 --- src/pkg/io/ioutil/blackhole.go | 18 ++++++++++++++---- src/pkg/io/ioutil/blackhole_race.go | 13 ------------- src/pkg/io/ioutil/ioutil.go | 1 + 3 files changed, 15 insertions(+), 17 deletions(-) delete mode 100644 src/pkg/io/ioutil/blackhole_race.go diff --git a/src/pkg/io/ioutil/blackhole.go b/src/pkg/io/ioutil/blackhole.go index c127bdb71c0..101d2c12153 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 eb640e05cfb..00000000000 --- 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 31c77299eee..0eb146c0ab7 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)