remove lock on buffered reader and writer

pull/255/merge
Darien Raymond 8 years ago
parent f7379bc1c3
commit f195f15536
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -2,7 +2,6 @@ package bufio
import ( import (
"io" "io"
"sync"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
@ -10,7 +9,6 @@ import (
// BufferedReader is a reader with internal cache. // BufferedReader is a reader with internal cache.
type BufferedReader struct { type BufferedReader struct {
sync.Mutex
reader io.Reader reader io.Reader
buffer *buf.Buffer buffer *buf.Buffer
cached bool cached bool
@ -27,15 +25,11 @@ func NewReader(rawReader io.Reader) *BufferedReader {
// Release implements Releasable.Release(). // Release implements Releasable.Release().
func (v *BufferedReader) Release() { func (v *BufferedReader) Release() {
v.Lock()
defer v.Unlock()
v.buffer.Release() v.buffer.Release()
v.buffer = nil v.buffer = nil
if releasable, ok := v.reader.(common.Releasable); ok { if releasable, ok := v.reader.(common.Releasable); ok {
releasable.Release() releasable.Release()
} }
v.reader = nil
} }
// Cached returns true if the internal cache is effective. // Cached returns true if the internal cache is effective.
@ -51,14 +45,7 @@ func (v *BufferedReader) SetCached(cached bool) {
// Read implements io.Reader.Read(). // Read implements io.Reader.Read().
func (v *BufferedReader) Read(b []byte) (int, error) { func (v *BufferedReader) Read(b []byte) (int, error) {
v.Lock() if !v.cached || v.buffer == nil {
defer v.Unlock()
if v.reader == nil {
return 0, io.EOF
}
if !v.cached {
if !v.buffer.IsEmpty() { if !v.buffer.IsEmpty() {
return v.buffer.Read(b) return v.buffer.Read(b)
} }

@ -2,7 +2,6 @@ package bufio
import ( import (
"io" "io"
"sync"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
@ -10,7 +9,6 @@ import (
) )
type BufferedWriter struct { type BufferedWriter struct {
sync.Mutex
writer io.Writer writer io.Writer
buffer *buf.Buffer buffer *buf.Buffer
cached bool cached bool
@ -25,13 +23,6 @@ func NewWriter(rawWriter io.Writer) *BufferedWriter {
} }
func (v *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) { 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) totalBytes := int64(0)
for { for {
oriSize := v.buffer.Len() oriSize := v.buffer.Len()
@ -43,19 +34,14 @@ func (v *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) {
} }
return totalBytes, err return totalBytes, err
} }
v.FlushWithoutLock() if err := v.Flush(); err != nil {
return totalBytes, err
}
} }
} }
func (v *BufferedWriter) Write(b []byte) (int, error) { func (v *BufferedWriter) Write(b []byte) (int, error) {
v.Lock() if !v.cached || v.buffer == nil {
defer v.Unlock()
if v.writer == nil {
return 0, io.ErrClosedPipe
}
if !v.cached {
return v.writer.Write(b) return v.writer.Write(b)
} }
nBytes, err := v.buffer.Write(b) nBytes, err := v.buffer.Write(b)
@ -63,7 +49,7 @@ func (v *BufferedWriter) Write(b []byte) (int, error) {
return 0, err return 0, err
} }
if v.buffer.IsFull() { if v.buffer.IsFull() {
err := v.FlushWithoutLock() err := v.Flush()
if err != nil { if err != nil {
return 0, err return 0, err
} }
@ -77,17 +63,6 @@ func (v *BufferedWriter) Write(b []byte) (int, error) {
} }
func (v *BufferedWriter) Flush() 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() defer v.buffer.Clear()
for !v.buffer.IsEmpty() { for !v.buffer.IsEmpty() {
nBytes, err := v.writer.Write(v.buffer.Bytes()) nBytes, err := v.writer.Write(v.buffer.Bytes())
@ -113,14 +88,10 @@ func (v *BufferedWriter) SetCached(cached bool) {
func (v *BufferedWriter) Release() { func (v *BufferedWriter) Release() {
v.Flush() v.Flush()
v.Lock()
defer v.Unlock()
v.buffer.Release() v.buffer.Release()
v.buffer = nil v.buffer = nil
if releasable, ok := v.writer.(common.Releasable); ok { if releasable, ok := v.writer.(common.Releasable); ok {
releasable.Release() releasable.Release()
} }
v.writer = nil
} }

Loading…
Cancel
Save