]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Ditch the torrent stateMu for the client mutex
authorMatt Joiner <anacrolix@gmail.com>
Sat, 20 Feb 2016 16:31:50 +0000 (03:31 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 20 Feb 2016 16:31:50 +0000 (03:31 +1100)
client.go
t.go
torrent.go

index b96cbd5f9b0ec04ec8d4cb11dcd22ed3a34ea559..9b3c870bca6636d2ff6c7f15aa7ce29eacae2d7a 100644 (file)
--- 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 ecbcdbf6a4a44e1c2b67527f60371501405fc437..59bb285f0713b9387ae7423cdedbc4b478c32721 100644 (file)
--- 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)
 }
 
index 7d9c59b67d581d2c59dceef1697dde10e725ed89..0fffe67ff2e555d5df091389f87bd909a343dfa6 100644 (file)
@@ -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