blob: 2e5c20ad456a72285b97f21432c07f697288a07f [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package modules
import (
// queueRW implements a ReadWriteCloser backed by an unbounded in-memory
// buffer.
type queueRW struct {
mu sync.Mutex
cond *sync.Cond
buf bytes.Buffer
closed bool
func newRW() io.ReadWriteCloser {
q := &queueRW{}
q.cond = sync.NewCond(&
return q
func (q *queueRW) Close() error {
defer q.cond.Broadcast()
q.closed = true
return nil
func (q *queueRW) Read(p []byte) (n int, err error) {
for q.buf.Len() == 0 && !q.closed {
return q.buf.Read(p)
func (q *queueRW) Write(p []byte) (n int, err error) {
defer q.cond.Broadcast()
return q.buf.Write(p)