2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 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.
|
|
|
|
|
|
|
|
// Pipe adapter to connect code expecting an io.Reader
|
|
|
|
// with code expecting an io.Writer.
|
|
|
|
|
|
|
|
package io
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
2018-01-09 02:23:08 +01:00
|
|
|
"sync/atomic"
|
2012-02-09 09:19:58 +01:00
|
|
|
)
|
2011-12-03 03:17:34 +01:00
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
// atomicError is a type-safe atomic value for errors.
|
|
|
|
// We use a struct{ error } to ensure consistent use of a concrete type.
|
|
|
|
type atomicError struct{ v atomic.Value }
|
|
|
|
|
|
|
|
func (a *atomicError) Store(err error) {
|
|
|
|
a.v.Store(struct{ error }{err})
|
|
|
|
}
|
|
|
|
func (a *atomicError) Load() error {
|
|
|
|
err, _ := a.v.Load().(struct{ error })
|
|
|
|
return err.error
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
// ErrClosedPipe is the error used for read or write operations on a closed pipe.
|
2012-02-09 09:19:58 +01:00
|
|
|
var ErrClosedPipe = errors.New("io: read/write on closed pipe")
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-03-25 00:46:17 +01:00
|
|
|
// A pipe is the shared pipe structure underlying PipeReader and PipeWriter.
|
2010-12-03 05:34:57 +01:00
|
|
|
type pipe struct {
|
2018-01-09 02:23:08 +01:00
|
|
|
wrMu sync.Mutex // Serializes Write operations
|
|
|
|
wrCh chan []byte
|
|
|
|
rdCh chan int
|
|
|
|
|
|
|
|
once sync.Once // Protects closing done
|
|
|
|
done chan struct{}
|
|
|
|
rerr atomicError
|
|
|
|
werr atomicError
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
func (p *pipe) Read(b []byte) (n int, err error) {
|
|
|
|
select {
|
|
|
|
case <-p.done:
|
|
|
|
return 0, p.readCloseError()
|
|
|
|
default:
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
select {
|
|
|
|
case bw := <-p.wrCh:
|
|
|
|
nr := copy(b, bw)
|
|
|
|
p.rdCh <- nr
|
|
|
|
return nr, nil
|
|
|
|
case <-p.done:
|
|
|
|
return 0, p.readCloseError()
|
2011-03-25 00:46:17 +01:00
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
func (p *pipe) readCloseError() error {
|
|
|
|
rerr := p.rerr.Load()
|
|
|
|
if werr := p.werr.Load(); rerr == nil && werr != nil {
|
|
|
|
return werr
|
|
|
|
}
|
|
|
|
return ErrClosedPipe
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
func (p *pipe) CloseRead(err error) error {
|
|
|
|
if err == nil {
|
2013-11-06 20:49:01 +01:00
|
|
|
err = ErrClosedPipe
|
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
p.rerr.Store(err)
|
|
|
|
p.once.Do(func() { close(p.done) })
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pipe) Write(b []byte) (n int, err error) {
|
|
|
|
select {
|
|
|
|
case <-p.done:
|
|
|
|
return 0, p.writeCloseError()
|
|
|
|
default:
|
|
|
|
p.wrMu.Lock()
|
|
|
|
defer p.wrMu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
for once := true; once || len(b) > 0; once = false {
|
|
|
|
select {
|
|
|
|
case p.wrCh <- b:
|
|
|
|
nw := <-p.rdCh
|
|
|
|
b = b[nw:]
|
|
|
|
n += nw
|
|
|
|
case <-p.done:
|
|
|
|
return n, p.writeCloseError()
|
2011-03-25 00:46:17 +01:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
return n, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
func (p *pipe) writeCloseError() error {
|
|
|
|
werr := p.werr.Load()
|
|
|
|
if rerr := p.rerr.Load(); werr == nil && rerr != nil {
|
|
|
|
return rerr
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
return ErrClosedPipe
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-09 02:23:08 +01:00
|
|
|
func (p *pipe) CloseWrite(err error) error {
|
2011-03-25 00:46:17 +01:00
|
|
|
if err == nil {
|
2011-12-03 03:17:34 +01:00
|
|
|
err = EOF
|
2011-03-25 00:46:17 +01:00
|
|
|
}
|
2018-01-09 02:23:08 +01:00
|
|
|
p.werr.Store(err)
|
|
|
|
p.once.Do(func() { close(p.done) })
|
|
|
|
return nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A PipeReader is the read half of a pipe.
|
|
|
|
type PipeReader struct {
|
2011-03-25 00:46:17 +01:00
|
|
|
p *pipe
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements the standard Read interface:
|
|
|
|
// it reads data from the pipe, blocking until a writer
|
|
|
|
// arrives or the write end is closed.
|
|
|
|
// If the write end is closed with an error, that error is
|
2011-12-03 03:17:34 +01:00
|
|
|
// returned as err; otherwise err is EOF.
|
|
|
|
func (r *PipeReader) Read(data []byte) (n int, err error) {
|
2018-01-09 02:23:08 +01:00
|
|
|
return r.p.Read(data)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the reader; subsequent writes to the
|
2011-12-03 03:17:34 +01:00
|
|
|
// write half of the pipe will return the error ErrClosedPipe.
|
|
|
|
func (r *PipeReader) Close() error {
|
2010-12-03 05:34:57 +01:00
|
|
|
return r.CloseWithError(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseWithError closes the reader; subsequent writes
|
|
|
|
// to the write half of the pipe will return the error err.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (r *PipeReader) CloseWithError(err error) error {
|
2018-01-09 02:23:08 +01:00
|
|
|
return r.p.CloseRead(err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// A PipeWriter is the write half of a pipe.
|
|
|
|
type PipeWriter struct {
|
2011-03-25 00:46:17 +01:00
|
|
|
p *pipe
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements the standard Write interface:
|
2017-01-14 01:05:42 +01:00
|
|
|
// it writes data to the pipe, blocking until one or more readers
|
2010-12-03 05:34:57 +01:00
|
|
|
// have consumed all the data or the read end is closed.
|
|
|
|
// If the read end is closed with an error, that err is
|
2011-12-03 03:17:34 +01:00
|
|
|
// returned as err; otherwise err is ErrClosedPipe.
|
|
|
|
func (w *PipeWriter) Write(data []byte) (n int, err error) {
|
2018-01-09 02:23:08 +01:00
|
|
|
return w.p.Write(data)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the writer; subsequent reads from the
|
2011-12-03 03:17:34 +01:00
|
|
|
// read half of the pipe will return no bytes and EOF.
|
|
|
|
func (w *PipeWriter) Close() error {
|
2010-12-03 05:34:57 +01:00
|
|
|
return w.CloseWithError(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseWithError closes the writer; subsequent reads from the
|
2015-10-31 01:59:47 +01:00
|
|
|
// read half of the pipe will return no bytes and the error err,
|
|
|
|
// or EOF if err is nil.
|
|
|
|
//
|
|
|
|
// CloseWithError always returns nil.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (w *PipeWriter) CloseWithError(err error) error {
|
2018-01-09 02:23:08 +01:00
|
|
|
return w.p.CloseWrite(err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pipe creates a synchronous in-memory pipe.
|
|
|
|
// It can be used to connect code expecting an io.Reader
|
|
|
|
// with code expecting an io.Writer.
|
2017-01-14 01:05:42 +01:00
|
|
|
//
|
|
|
|
// Reads and Writes on the pipe are matched one to one
|
|
|
|
// except when multiple Reads are needed to consume a single Write.
|
|
|
|
// That is, each Write to the PipeWriter blocks until it has satisfied
|
|
|
|
// one or more Reads from the PipeReader that fully consume
|
|
|
|
// the written data.
|
|
|
|
// The data is copied directly from the Write to the corresponding
|
|
|
|
// Read (or Reads); there is no internal buffering.
|
|
|
|
//
|
|
|
|
// It is safe to call Read and Write in parallel with each other or with Close.
|
|
|
|
// Parallel calls to Read and parallel calls to Write are also safe:
|
2012-03-06 18:57:23 +01:00
|
|
|
// the individual calls will be gated sequentially.
|
2010-12-03 05:34:57 +01:00
|
|
|
func Pipe() (*PipeReader, *PipeWriter) {
|
2018-01-09 02:23:08 +01:00
|
|
|
p := &pipe{
|
|
|
|
wrCh: make(chan []byte),
|
|
|
|
rdCh: make(chan int),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
return &PipeReader{p}, &PipeWriter{p}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|