Source File
pipe.go
Belonging Package
io
package io
import (
)
var ErrClosedPipe = errors.New("io: read/write on closed pipe")
type pipe struct {
wrMu sync.Mutex // Serializes Write operations
wrCh chan []byte
rdCh chan int
once sync.Once // Protects closing done
done chan struct{}
rerr onceError
werr onceError
}
func ( *pipe) ( []byte) ( int, error) {
select {
case <-.done:
return 0, .readCloseError()
default:
}
select {
case := <-.wrCh:
:= copy(, )
.rdCh <-
return , nil
case <-.done:
return 0, .readCloseError()
}
}
func ( *pipe) () error {
:= .rerr.Load()
if := .werr.Load(); == nil && != nil {
return
}
return ErrClosedPipe
}
func ( *pipe) ( error) error {
if == nil {
= ErrClosedPipe
}
.rerr.Store()
.once.Do(func() { close(.done) })
return nil
}
func ( *pipe) ( []byte) ( int, error) {
select {
case <-.done:
return 0, .writeCloseError()
default:
.wrMu.Lock()
defer .wrMu.Unlock()
}
for := true; || len() > 0; = false {
select {
case .wrCh <- :
:= <-.rdCh
= [:]
+=
case <-.done:
return , .writeCloseError()
}
}
return , nil
}
func ( *pipe) () error {
:= .werr.Load()
if := .rerr.Load(); == nil && != nil {
return
}
return ErrClosedPipe
}
func ( *pipe) ( error) error {
if == nil {
= EOF
}
.werr.Store()
.once.Do(func() { close(.done) })
return nil
}
type PipeReader struct {
p *pipe
}
func ( *PipeReader) () error {
return .CloseWithError(nil)
}
func ( *PipeReader) ( error) error {
return .p.CloseRead()
}
type PipeWriter struct {
p *pipe
}
func ( *PipeWriter) () error {
return .CloseWithError(nil)
}
func ( *PipeWriter) ( error) error {
return .p.CloseWrite()
}
func () (*PipeReader, *PipeWriter) {
:= &pipe{
wrCh: make(chan []byte),
rdCh: make(chan int),
done: make(chan struct{}),
}
return &PipeReader{}, &PipeWriter{}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |