diff --git a/client.go b/client.go index b96cbd5f..9b3c870b 100644 --- a/client.go +++ b/client.go @@ -1825,7 +1825,6 @@ func newTorrent(ih InfoHash) (t *torrent) { HalfOpen: make(map[string]struct{}), pieceStateChanges: pubsub.NewPubSub(), } - t.wantPeers.L = &t.stateMu return } @@ -2024,6 +2023,7 @@ func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (T Torrent, new bool, err er // TODO: Tidy this up? t = newTorrent(spec.InfoHash) t.cl = cl + t.wantPeers.L = &cl.mu if spec.ChunkSize != 0 { t.chunkSize = pp.Integer(spec.ChunkSize) } @@ -2087,8 +2087,6 @@ func (me *Client) dropTorrent(infoHash InfoHash) (err error) { func (cl *Client) waitWantPeers(t *torrent) bool { cl.mu.Lock() defer cl.mu.Unlock() - t.stateMu.Lock() - defer t.stateMu.Unlock() for { select { case <-t.ceasingNetworking: @@ -2102,11 +2100,7 @@ func (cl *Client) waitWantPeers(t *torrent) bool { return true } wait: - cl.mu.Unlock() t.wantPeers.Wait() - t.stateMu.Unlock() - cl.mu.Lock() - t.stateMu.Lock() } } diff --git a/t.go b/t.go index ecbcdbf6..59bb285f 100644 --- a/t.go +++ b/t.go @@ -48,14 +48,14 @@ func (t Torrent) NewReader() (ret *Reader) { // same state. The sum of the state run lengths is the number of pieces // in the torrent. func (t Torrent) PieceStateRuns() []PieceStateRun { - t.torrent.stateMu.Lock() - defer t.torrent.stateMu.Unlock() + t.cl.mu.Lock() + defer t.cl.mu.Unlock() return t.torrent.pieceStateRuns() } func (t Torrent) PieceState(piece int) PieceState { - t.torrent.stateMu.Lock() - defer t.torrent.stateMu.Unlock() + t.cl.mu.Lock() + defer t.cl.mu.Unlock() return t.torrent.pieceState(piece) } diff --git a/torrent.go b/torrent.go index 7d9c59b6..0fffe67f 100644 --- a/torrent.go +++ b/torrent.go @@ -51,11 +51,10 @@ type peersKey struct { Port int } -// Is not aware of Client. Maintains state of torrent for with-in a Client. +// Maintains state of torrent within a Client. type torrent struct { cl *Client - stateMu sync.Mutex closing chan struct{} // Closed when no more network activity is desired. This includes @@ -166,8 +165,6 @@ func (t *torrent) worstConns(cl *Client) (wcs *worstConns) { } func (t *torrent) ceaseNetworking() { - t.stateMu.Lock() - defer t.stateMu.Unlock() select { case <-t.ceasingNetworking: return