]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Remove unnecessary locking in Torrent.Piece
authorMatt Joiner <anacrolix@gmail.com>
Wed, 18 Dec 2019 05:49:15 +0000 (16:49 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 18 Dec 2019 05:49:15 +0000 (16:49 +1100)
t.go
torrent.go

diff --git a/t.go b/t.go
index 92a265f15bcee20b4a4d7d23db3bdd50cfa78c2d..95b69fe7528b90b72598e3c170d7f3eb2e21e999 100644 (file)
--- a/t.go
+++ b/t.go
@@ -15,8 +15,7 @@ func (t *Torrent) InfoHash() metainfo.Hash {
        return t.infoHash
 }
 
-// Returns a channel that is closed when the info (.Info()) for the torrent
-// has become available.
+// Returns a channel that is closed when the info (.Info()) for the torrent has become available.
 func (t *Torrent) GotInfo() <-chan struct{} {
        t.cl.lock()
        defer t.cl.unlock()
@@ -243,7 +242,5 @@ func (t *Torrent) AddTrackers(announceList [][]string) {
 }
 
 func (t *Torrent) Piece(i pieceIndex) *Piece {
-       t.cl.lock()
-       defer t.cl.unlock()
-       return &t.pieces[i]
+       return t.piece(i)
 }
index 8a7a72f1a10191869738c13b00626f805f72a57e..2937b94ca3b96e194e344851a33a9a6e05f212d3 100644 (file)
@@ -36,7 +36,8 @@ func (t *Torrent) chunkIndexSpec(chunkIndex pp.Integer, piece pieceIndex) chunkS
        return chunkIndexSpec(chunkIndex, t.pieceLength(piece), t.chunkSize)
 }
 
-// Maintains state of torrent within a Client.
+// Maintains state of torrent within a Client. Many methods should not be called before the info is
+// available, see .Info and .GotInfo.
 type Torrent struct {
        // Torrent-level aggregate statistics. First in struct to ensure 64-bit
        // alignment. See #262.
@@ -1698,7 +1699,8 @@ func (t *Torrent) queuePieceCheck(pieceIndex pieceIndex) {
        t.tryCreateMorePieceHashers()
 }
 
-// Forces all the pieces to be re-hashed. See also Piece.VerifyData.
+// Forces all the pieces to be re-hashed. See also Piece.VerifyData. This should not be called
+// before the Info is available.
 func (t *Torrent) VerifyData() {
        for i := pieceIndex(0); i < t.NumPieces(); i++ {
                t.Piece(i).VerifyData()