]> Sergey Matveev's repositories - btrtrc.git/blobdiff - torrent.go
Replacing pendingWrites WaitGroup with Mutex/Cond/int
[btrtrc.git] / torrent.go
index 66123a8e95e95820ac90593e12dc54d060bfd4a5..bb8465df591144ba5d77bb2acb3768e59232ae1f 100644 (file)
@@ -225,6 +225,7 @@ func (t *torrent) setMetadata(md *metainfo.Info, infoBytes []byte, eventLocker s
        for _, hash := range infoPieceHashes(md) {
                piece := &piece{}
                piece.Event.L = eventLocker
+               piece.noPendingWrites.L = &piece.pendingWritesMutex
                missinggo.CopyExact(piece.Hash[:], hash)
                t.Pieces = append(t.Pieces, piece)
        }
@@ -638,7 +639,11 @@ func (t *torrent) pieceLength(piece int) (len_ pp.Integer) {
 func (t *torrent) hashPiece(piece pp.Integer) (ps pieceSum) {
        hash := pieceHash.New()
        p := t.Pieces[piece]
-       p.pendingWrites.Wait()
+       p.pendingWritesMutex.Lock()
+       for p.pendingWrites != 0 {
+               p.noPendingWrites.Wait()
+       }
+       p.pendingWritesMutex.Unlock()
        t.data.WriteSectionTo(hash, int64(piece)*t.Info.PieceLength, t.Info.PieceLength)
        missinggo.CopyExact(ps[:], hash.Sum(nil))
        return