From f195f155369f525873f2ef09153d7c3b85bc9a12 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 27 Dec 2016 01:01:47 +0100 Subject: [PATCH] remove lock on buffered reader and writer --- common/bufio/reader.go | 15 +-------------- common/bufio/writer.go | 39 +++++---------------------------------- 2 files changed, 6 insertions(+), 48 deletions(-) diff --git a/common/bufio/reader.go b/common/bufio/reader.go index b00cd207..7957438d 100644 --- a/common/bufio/reader.go +++ b/common/bufio/reader.go @@ -2,7 +2,6 @@ package bufio import ( "io" - "sync" "v2ray.com/core/common" "v2ray.com/core/common/buf" @@ -10,7 +9,6 @@ import ( // BufferedReader is a reader with internal cache. type BufferedReader struct { - sync.Mutex reader io.Reader buffer *buf.Buffer cached bool @@ -27,15 +25,11 @@ func NewReader(rawReader io.Reader) *BufferedReader { // Release implements Releasable.Release(). func (v *BufferedReader) Release() { - v.Lock() - defer v.Unlock() - v.buffer.Release() v.buffer = nil if releasable, ok := v.reader.(common.Releasable); ok { releasable.Release() } - v.reader = nil } // Cached returns true if the internal cache is effective. @@ -51,14 +45,7 @@ func (v *BufferedReader) SetCached(cached bool) { // Read implements io.Reader.Read(). func (v *BufferedReader) Read(b []byte) (int, error) { - v.Lock() - defer v.Unlock() - - if v.reader == nil { - return 0, io.EOF - } - - if !v.cached { + if !v.cached || v.buffer == nil { if !v.buffer.IsEmpty() { return v.buffer.Read(b) } diff --git a/common/bufio/writer.go b/common/bufio/writer.go index 1c8a0cab..c61255ac 100644 --- a/common/bufio/writer.go +++ b/common/bufio/writer.go @@ -2,7 +2,6 @@ package bufio import ( "io" - "sync" "v2ray.com/core/common" "v2ray.com/core/common/buf" @@ -10,7 +9,6 @@ import ( ) type BufferedWriter struct { - sync.Mutex writer io.Writer buffer *buf.Buffer cached bool @@ -25,13 +23,6 @@ func NewWriter(rawWriter io.Writer) *BufferedWriter { } func (v *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) { - v.Lock() - defer v.Unlock() - - if v.writer == nil { - return 0, io.ErrClosedPipe - } - totalBytes := int64(0) for { oriSize := v.buffer.Len() @@ -43,19 +34,14 @@ func (v *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) { } return totalBytes, err } - v.FlushWithoutLock() + if err := v.Flush(); err != nil { + return totalBytes, err + } } } func (v *BufferedWriter) Write(b []byte) (int, error) { - v.Lock() - defer v.Unlock() - - if v.writer == nil { - return 0, io.ErrClosedPipe - } - - if !v.cached { + if !v.cached || v.buffer == nil { return v.writer.Write(b) } nBytes, err := v.buffer.Write(b) @@ -63,7 +49,7 @@ func (v *BufferedWriter) Write(b []byte) (int, error) { return 0, err } if v.buffer.IsFull() { - err := v.FlushWithoutLock() + err := v.Flush() if err != nil { return 0, err } @@ -77,17 +63,6 @@ func (v *BufferedWriter) Write(b []byte) (int, error) { } func (v *BufferedWriter) Flush() error { - v.Lock() - defer v.Unlock() - - if v.writer == nil { - return io.ErrClosedPipe - } - - return v.FlushWithoutLock() -} - -func (v *BufferedWriter) FlushWithoutLock() error { defer v.buffer.Clear() for !v.buffer.IsEmpty() { nBytes, err := v.writer.Write(v.buffer.Bytes()) @@ -113,14 +88,10 @@ func (v *BufferedWriter) SetCached(cached bool) { func (v *BufferedWriter) Release() { v.Flush() - v.Lock() - defer v.Unlock() - v.buffer.Release() v.buffer = nil if releasable, ok := v.writer.(common.Releasable); ok { releasable.Release() } - v.writer = nil }