1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
package nio
import (
"io"
"sync"
)
// PipeReader is the read half of the pipe.
type PipeReader struct {
*bufpipe
}
// CloseWithError closes the reader; subsequent writes to the write half of the pipe will return the error err.
func (r *PipeReader) CloseWithError(err error) error {
if err == nil {
err = io.ErrClosedPipe
}
r.bufpipe.l.Lock()
defer r.bufpipe.l.Unlock()
if r.bufpipe.rerr == nil {
r.bufpipe.rerr = err
r.bufpipe.rwait.Signal()
r.bufpipe.wwait.Signal()
}
return nil
}
// Close closes the reader; subsequent writes to the write half of the pipe will return the error io.ErrClosedPipe.
func (r *PipeReader) Close() error {
return r.CloseWithError(nil)
}
// A PipeWriter is the write half of a pipe.
type PipeWriter struct {
*bufpipe
}
// CloseWithError closes the writer; once the buffer is empty subsequent reads from the read half of the pipe will return
// no bytes and the error err, or io.EOF if err is nil. CloseWithError always returns nil.
func (w *PipeWriter) CloseWithError(err error) error {
if err == nil {
err = io.EOF
}
w.bufpipe.l.Lock()
defer w.bufpipe.l.Unlock()
if w.bufpipe.werr == nil {
w.bufpipe.werr = err
w.bufpipe.rwait.Signal()
w.bufpipe.wwait.Signal()
}
return nil
}
// Close closes the writer; once the buffer is empty subsequent reads from the read half of the pipe will return
// no bytes and io.EOF after all the buffer has been read. CloseWithError always returns nil.
func (w *PipeWriter) Close() error {
return w.CloseWithError(nil)
}
type bufpipe struct {
rl sync.Mutex
wl sync.Mutex
l sync.Mutex
rwait sync.Cond
wwait sync.Cond
b Buffer
rerr error // if reader closed, error to give writes
werr error // if writer closed, error to give reads
}
func newBufferedPipe(buf Buffer) *bufpipe {
s := &bufpipe{
b: buf,
}
s.rwait.L = &s.l
s.wwait.L = &s.l
return s
}
func empty(buf Buffer) bool {
return buf.Len() == 0
}
func gap(buf Buffer) int64 {
return buf.Cap() - buf.Len()
}
func (r *PipeReader) Read(p []byte) (n int, err error) {
r.rl.Lock()
defer r.rl.Unlock()
r.l.Lock()
defer r.wwait.Signal()
defer r.l.Unlock()
for empty(r.b) {
if r.rerr != nil {
return 0, io.ErrClosedPipe
}
if r.werr != nil {
return 0, r.werr
}
r.wwait.Signal()
r.rwait.Wait()
}
n, err = r.b.Read(p)
if err == io.EOF {
err = nil
}
return n, err
}
func (w *PipeWriter) Write(p []byte) (int, error) {
var m int
var n, space int64
var err error
sliceLen := int64(len(p))
w.wl.Lock()
defer w.wl.Unlock()
w.l.Lock()
defer w.rwait.Signal()
defer w.l.Unlock()
if w.werr != nil {
return 0, io.ErrClosedPipe
}
// while there is data to write
for writeLen := sliceLen; writeLen > 0 && err == nil; writeLen = sliceLen - n {
// wait for some buffer space to become available (while no errs)
for space = gap(w.b); space == 0 && w.rerr == nil && w.werr == nil; space = gap(w.b) {
w.rwait.Signal()
w.wwait.Wait()
}
if w.rerr != nil {
err = w.rerr
break
}
if w.werr != nil {
err = io.ErrClosedPipe
break
}
// space > 0, and locked
var nn int64
if space < writeLen {
// => writeLen - space > 0
// => (sliceLen - n) - space > 0
// => sliceLen > n + space
// nn is safe to use for p[:nn]
nn = n + space
} else {
nn = sliceLen
}
m, err = w.b.Write(p[n:nn])
n += int64(m)
// one of the following cases has occurred:
// 1. done writing -> writeLen == 0
// 2. ran out of buffer space -> gap(w.b) == 0
// 3. an error occurred err != nil
// all of these cases are handled at the top of this loop
}
return int(n), err
}
|