Remove sync.Cond from piece

It's not trivial to ensure that anyone waiting on the piece Cond will wake on events like the torrent being dropped from the client.
This commit is contained in:
Matt Joiner 2015-11-06 00:40:16 +11:00
parent c7f9bfccc1
commit d01c6221e6
6 changed files with 7 additions and 8 deletions

View File

@ -1923,7 +1923,7 @@ func (cl *Client) setStorage(t *torrent, td Data) (err error) {
type TorrentDataOpener func(*metainfo.Info) Data type TorrentDataOpener func(*metainfo.Info) Data
func (cl *Client) setMetaData(t *torrent, md *metainfo.Info, bytes []byte) (err error) { func (cl *Client) setMetaData(t *torrent, md *metainfo.Info, bytes []byte) (err error) {
err = t.setMetadata(md, bytes, &cl.mu) err = t.setMetadata(md, bytes)
if err != nil { if err != nil {
return return
} }
@ -2590,7 +2590,7 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er
c.peerTouchedPieces[int(req.Index)] = struct{}{} c.peerTouchedPieces[int(req.Index)] = struct{}{}
// log.Println("got chunk", req) // log.Println("got chunk", req)
piece.Event.Broadcast() me.event.Broadcast()
defer t.publishPieceChange(int(req.Index)) defer t.publishPieceChange(int(req.Index))
// Record that we have the chunk. // Record that we have the chunk.
piece.unpendChunkIndex(chunkIndex(req.chunkSpec, t.chunkSize)) piece.unpendChunkIndex(chunkIndex(req.chunkSpec, t.chunkSize))
@ -2662,7 +2662,7 @@ func (me *Client) pieceChanged(t *torrent, piece int) {
correct := t.pieceComplete(piece) correct := t.pieceComplete(piece)
p := &t.Pieces[piece] p := &t.Pieces[piece]
defer t.publishPieceChange(piece) defer t.publishPieceChange(piece)
defer p.Event.Broadcast() defer me.event.Broadcast()
if correct { if correct {
p.Priority = PiecePriorityNone p.Priority = PiecePriorityNone
p.PendingChunkSpecs = nil p.PendingChunkSpecs = nil

View File

@ -99,7 +99,7 @@ func TestTorrentInitialState(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
tor.chunkSize = 2 tor.chunkSize = 2
err = tor.setMetadata(&mi.Info.Info, mi.Info.Bytes, nil) err = tor.setMetadata(&mi.Info.Info, mi.Info.Bytes)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -28,7 +28,6 @@ type piece struct {
Hashing bool Hashing bool
QueuedForHash bool QueuedForHash bool
EverHashed bool EverHashed bool
Event sync.Cond
Priority piecePriority Priority piecePriority
PublicPieceState PieceState PublicPieceState PieceState

View File

@ -75,7 +75,7 @@ func (r *Reader) available(off, max int64) (ret int64) {
} }
func (r *Reader) waitReadable(off int64) { func (r *Reader) waitReadable(off int64) {
r.t.Pieces[off/int64(r.t.usualPieceSize())].Event.Wait() r.t.cl.event.Wait()
} }
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) { func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) {

1
t.go
View File

@ -2,6 +2,7 @@ package torrent
import ( import (
"github.com/anacrolix/missinggo/pubsub" "github.com/anacrolix/missinggo/pubsub"
"github.com/anacrolix/torrent/metainfo" "github.com/anacrolix/torrent/metainfo"
) )

View File

@ -232,7 +232,7 @@ func infoPieceHashes(info *metainfo.Info) (ret []string) {
} }
// Called when metadata for a torrent becomes available. // Called when metadata for a torrent becomes available.
func (t *torrent) setMetadata(md *metainfo.Info, infoBytes []byte, eventLocker sync.Locker) (err error) { func (t *torrent) setMetadata(md *metainfo.Info, infoBytes []byte) (err error) {
err = validateInfo(md) err = validateInfo(md)
if err != nil { if err != nil {
err = fmt.Errorf("bad info: %s", err) err = fmt.Errorf("bad info: %s", err)
@ -249,7 +249,6 @@ func (t *torrent) setMetadata(md *metainfo.Info, infoBytes []byte, eventLocker s
t.Pieces = make([]piece, len(hashes)) t.Pieces = make([]piece, len(hashes))
for i, hash := range hashes { for i, hash := range hashes {
piece := &t.Pieces[i] piece := &t.Pieces[i]
piece.Event.L = eventLocker
piece.noPendingWrites.L = &piece.pendingWritesMutex piece.noPendingWrites.L = &piece.pendingWritesMutex
missinggo.CopyExact(piece.Hash[:], hash) missinggo.CopyExact(piece.Hash[:], hash)
} }