]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Move File.State lock to top of function
authorMatt Joiner <anacrolix@gmail.com>
Fri, 26 May 2017 02:59:52 +0000 (12:59 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 26 May 2017 03:00:28 +0000 (13:00 +1000)
file.go

diff --git a/file.go b/file.go
index f57cd7af8a35b8491040fbd51e3e4fba88d611b1..720e4fb510e37396bd715b8afee172cb03616754 100644 (file)
--- a/file.go
+++ b/file.go
@@ -54,6 +54,8 @@ type FilePieceState struct {
 
 // Returns the state of pieces in this file.
 func (f *File) State() (ret []FilePieceState) {
+       f.t.cl.mu.RLock()
+       defer f.t.cl.mu.RUnlock()
        pieceSize := int64(f.t.usualPieceSize())
        off := f.offset % pieceSize
        remaining := f.length
@@ -65,9 +67,7 @@ func (f *File) State() (ret []FilePieceState) {
                if len1 > remaining {
                        len1 = remaining
                }
-               f.t.cl.mu.RLock()
                ps := f.t.pieceState(i)
-               f.t.cl.mu.RUnlock()
                ret = append(ret, FilePieceState{len1, ps})
                off = 0
                remaining -= len1