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"
|
|
|
|
)
|
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 {
|
2011-03-25 00:46:17 +01:00
|
|
|
rl sync.Mutex // gates readers one at a time
|
|
|
|
wl sync.Mutex // gates writers one at a time
|
|
|
|
l sync.Mutex // protects remaining fields
|
|
|
|
data []byte // data remaining in pending write
|
|
|
|
rwait sync.Cond // waiting reader
|
|
|
|
wwait sync.Cond // waiting writer
|
2011-12-03 03:17:34 +01:00
|
|
|
rerr error // if reader closed, error to give writes
|
|
|
|
werr error // if writer closed, error to give reads
|
2011-03-25 00:46:17 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (p *pipe) read(b []byte) (n int, err error) {
|
2011-03-25 00:46:17 +01:00
|
|
|
// One reader at a time.
|
|
|
|
p.rl.Lock()
|
|
|
|
defer p.rl.Unlock()
|
|
|
|
|
|
|
|
p.l.Lock()
|
|
|
|
defer p.l.Unlock()
|
2010-12-03 05:34:57 +01:00
|
|
|
for {
|
2011-03-25 00:46:17 +01:00
|
|
|
if p.rerr != nil {
|
2011-12-03 03:17:34 +01:00
|
|
|
return 0, ErrClosedPipe
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-03-25 00:46:17 +01:00
|
|
|
if p.data != nil {
|
|
|
|
break
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-03-25 00:46:17 +01:00
|
|
|
if p.werr != nil {
|
|
|
|
return 0, p.werr
|
|
|
|
}
|
|
|
|
p.rwait.Wait()
|
|
|
|
}
|
|
|
|
n = copy(b, p.data)
|
|
|
|
p.data = p.data[n:]
|
|
|
|
if len(p.data) == 0 {
|
|
|
|
p.data = nil
|
|
|
|
p.wwait.Signal()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-03-25 00:46:17 +01:00
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-03-25 00:46:17 +01:00
|
|
|
var zero [0]byte
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (p *pipe) write(b []byte) (n int, err error) {
|
2011-03-25 00:46:17 +01:00
|
|
|
// pipe uses nil to mean not available
|
|
|
|
if b == nil {
|
|
|
|
b = zero[:]
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-03-25 00:46:17 +01:00
|
|
|
// One writer at a time.
|
|
|
|
p.wl.Lock()
|
|
|
|
defer p.wl.Unlock()
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-03-25 00:46:17 +01:00
|
|
|
p.l.Lock()
|
|
|
|
defer p.l.Unlock()
|
2013-11-06 20:49:01 +01:00
|
|
|
if p.werr != nil {
|
|
|
|
err = ErrClosedPipe
|
|
|
|
return
|
|
|
|
}
|
2011-03-25 00:46:17 +01:00
|
|
|
p.data = b
|
|
|
|
p.rwait.Signal()
|
|
|
|
for {
|
|
|
|
if p.data == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if p.rerr != nil {
|
|
|
|
err = p.rerr
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if p.werr != nil {
|
2011-12-03 03:17:34 +01:00
|
|
|
err = ErrClosedPipe
|
2011-03-25 00:46:17 +01:00
|
|
|
}
|
|
|
|
p.wwait.Wait()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-03-25 00:46:17 +01:00
|
|
|
n = len(b) - len(p.data)
|
|
|
|
p.data = nil // in case of rerr or werr
|
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (p *pipe) rclose(err error) {
|
2011-03-25 00:46:17 +01:00
|
|
|
if err == nil {
|
2011-12-03 03:17:34 +01:00
|
|
|
err = ErrClosedPipe
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-03-25 00:46:17 +01:00
|
|
|
p.l.Lock()
|
|
|
|
defer p.l.Unlock()
|
|
|
|
p.rerr = err
|
|
|
|
p.rwait.Signal()
|
|
|
|
p.wwait.Signal()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (p *pipe) wclose(err 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
|
|
|
}
|
|
|
|
p.l.Lock()
|
|
|
|
defer p.l.Unlock()
|
|
|
|
p.werr = err
|
|
|
|
p.rwait.Signal()
|
|
|
|
p.wwait.Signal()
|
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) {
|
2011-03-25 00:46:17 +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 {
|
2011-03-25 00:46:17 +01:00
|
|
|
r.p.rclose(err)
|
|
|
|
return nil
|
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:
|
|
|
|
// it writes data to the pipe, blocking until readers
|
|
|
|
// 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) {
|
2011-03-25 00:46:17 +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 {
|
2011-03-25 00:46:17 +01:00
|
|
|
w.p.wclose(err)
|
|
|
|
return nil
|
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.
|
|
|
|
// Reads on one end are matched with writes on the other,
|
|
|
|
// copying data directly between the two; there is no internal buffering.
|
2012-03-06 18:57:23 +01:00
|
|
|
// It is safe to call Read and Write in parallel with each other or with
|
|
|
|
// Close. Close will complete once pending I/O is done. Parallel calls to
|
|
|
|
// Read, and parallel calls to Write, are also safe:
|
|
|
|
// the individual calls will be gated sequentially.
|
2010-12-03 05:34:57 +01:00
|
|
|
func Pipe() (*PipeReader, *PipeWriter) {
|
2011-03-25 00:46:17 +01:00
|
|
|
p := new(pipe)
|
|
|
|
p.rwait.L = &p.l
|
|
|
|
p.wwait.L = &p.l
|
|
|
|
r := &PipeReader{p}
|
|
|
|
w := &PipeWriter{p}
|
2010-12-03 05:34:57 +01:00
|
|
|
return r, w
|
|
|
|
}
|