Ditch the torrent stateMu for the client mutex

This commit is contained in:
Matt Joiner 2016-02-21 03:31:50 +11:00
parent f6472fc1fd
commit dcdf85a474
3 changed files with 6 additions and 15 deletions

View File

@ -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()
}
}

8
t.go
View File

@ -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)
}

View 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