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