github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/stream/concurrent_writer.go (about) 1 package stream 2 3 import ( 4 "io" 5 "sync" 6 ) 7 8 // concurrentWriter is an io.Writer that serializes calls to Write. 9 type concurrentWriter struct { 10 // lock serializes operations on the writer. 11 lock sync.Mutex 12 // writer is the underlying writer. 13 writer io.Writer 14 } 15 16 // NewConcurrentWriter creates a new writer that serializes operations on the 17 // underlying writer. 18 func NewConcurrentWriter(writer io.Writer) io.Writer { 19 return &concurrentWriter{writer: writer} 20 } 21 22 // Write implements io.Writer.Write. 23 func (w *concurrentWriter) Write(buffer []byte) (int, error) { 24 // Lock the writer and defer its release. 25 w.lock.Lock() 26 defer w.lock.Unlock() 27 28 // Perform the write. 29 return w.writer.Write(buffer) 30 }