From 8aa0f36a37ed5a52778f51b0dc5f57dfbf8a42a0 Mon Sep 17 00:00:00 2001 From: Jordan Lee Date: Tue, 18 Jan 2011 23:44:36 +0000 Subject: [PATCH] cleanup of who calls the libtransmission thread locking functions. In some cases we were calling them from deep inside libtransmission, when they should have been called directly from the public-visible API functions: tr_torrentWebSpeeds_KBps(), tr_torrentPeers(), tr_torrentTrackers(). --- libtransmission/announcer.c | 1 + libtransmission/peer-mgr.c | 123 +++++++++++++----------------------- libtransmission/torrent.c | 42 ++++++++---- libtransmission/torrent.h | 5 ++ 4 files changed, 82 insertions(+), 89 deletions(-) diff --git a/libtransmission/announcer.c b/libtransmission/announcer.c index 5e599b00f..f7dff8f3b 100644 --- a/libtransmission/announcer.c +++ b/libtransmission/announcer.c @@ -1956,6 +1956,7 @@ tr_announcerStats( const tr_torrent * torrent, const time_t now = tr_time( ); assert( tr_isTorrent( torrent ) ); + assert( tr_torrentIsLocked( torrent ) ); /* count the trackers... */ for( i=n=0, tierCount=tr_ptrArraySize( &torrent->tiers->tiers ); itorrentPeers; - assert( t != NULL ); - managerLock( t->manager ); + assert( tr_isTorrent( tor ) ); + assert( tr_torrentIsLocked( tor ) ); + ensureMgrTimersExist( t->manager ); t->isRunning = TRUE; t->maxPeers = t->tor->maxConnectedPeers; rechokePulse( 0, 0, t->manager ); - managerUnlock( t->manager ); } static void @@ -2081,8 +2081,6 @@ stopTorrent( Torrent * t ) { int i, n; - assert( torrentIsLocked( t ) ); - t->isRunning = FALSE; /* disconnect the peers. */ @@ -2099,38 +2097,30 @@ stopTorrent( Torrent * t ) void tr_peerMgrStopTorrent( tr_torrent * tor ) { - Torrent * t = tor->torrentPeers; - - managerLock( t->manager ); - - stopTorrent( t ); + assert( tr_isTorrent( tor ) ); + assert( tr_torrentIsLocked( tor ) ); - managerUnlock( t->manager ); + stopTorrent( tor->torrentPeers ); } void -tr_peerMgrAddTorrent( tr_peerMgr * manager, - tr_torrent * tor ) +tr_peerMgrAddTorrent( tr_peerMgr * manager, tr_torrent * tor ) { - managerLock( manager ); - - assert( tor ); + assert( tr_isTorrent( tor ) ); + assert( tr_torrentIsLocked( tor ) ); assert( tor->torrentPeers == NULL ); tor->torrentPeers = torrentConstructor( manager, tor ); - - managerUnlock( manager ); } void tr_peerMgrRemoveTorrent( tr_torrent * tor ) { - tr_torrentLock( tor ); + assert( tr_isTorrent( tor ) ); + assert( tr_torrentIsLocked( tor ) ); stopTorrent( tor->torrentPeers ); torrentDestructor( tor->torrentPeers ); - - tr_torrentUnlock( tor ); } void @@ -2172,38 +2162,35 @@ tr_bitfield* tr_peerMgrGetAvailable( const tr_torrent * tor ) { int i; - int peerCount; Torrent * t = tor->torrentPeers; - const tr_peer ** peers; - tr_bitfield * pieces; - managerLock( t->manager ); + const int peerCount = tr_ptrArraySize( &t->peers ); + const tr_peer ** peers = (const tr_peer**) tr_ptrArrayBase( &t->peers ); + tr_bitfield * pieces = tr_bitfieldNew( t->tor->info.pieceCount ); + + assert( tr_torrentIsLocked( tor ) ); - pieces = tr_bitfieldNew( t->tor->info.pieceCount ); - peerCount = tr_ptrArraySize( &t->peers ); - peers = (const tr_peer**) tr_ptrArrayBase( &t->peers ); for( i=0; ihave ); - managerUnlock( t->manager ); return pieces; } void -tr_peerMgrTorrentStats( tr_torrent * tor, - int * setmePeersKnown, - int * setmePeersConnected, - int * setmeSeedsConnected, - int * setmeWebseedsSendingToUs, - int * setmePeersSendingToUs, - int * setmePeersGettingFromUs, - int * setmePeersFrom ) +tr_peerMgrTorrentStats( tr_torrent * tor, + int * setmePeersKnown, + int * setmePeersConnected, + int * setmeSeedsConnected, + int * setmeWebseedsSendingToUs, + int * setmePeersSendingToUs, + int * setmePeersGettingFromUs, + int * setmePeersFrom ) { int i, size; const Torrent * t = tor->torrentPeers; const tr_peer ** peers; const tr_webseed ** webseeds; - managerLock( t->manager ); + assert( tr_torrentIsLocked( tor ) ); peers = (const tr_peer **) tr_ptrArrayBase( &t->peers ); size = tr_ptrArraySize( &t->peers ); @@ -2245,8 +2232,6 @@ tr_peerMgrTorrentStats( tr_torrent * tor, for( i=0; imanager ); } int @@ -2271,21 +2256,17 @@ tr_peerMgrGetWebseedSpeed_Bps( const tr_torrent * tor, uint64_t now ) double* tr_peerMgrWebSpeeds_KBps( const tr_torrent * tor ) { - const Torrent * t = tor->torrentPeers; - const tr_webseed ** webseeds; int i; - int webseedCount; - double * ret; - uint64_t now; - - assert( t->manager ); - managerLock( t->manager ); + const Torrent * t = tor->torrentPeers; + const int webseedCount = tr_ptrArraySize( &t->webseeds ); + const tr_webseed ** webseeds = (const tr_webseed**) tr_ptrArrayBase( &t->webseeds ); + const uint64_t now = tr_time_msec( ); + double * ret = tr_new0( double, webseedCount ); - webseeds = (const tr_webseed**) tr_ptrArrayBase( &t->webseeds ); - webseedCount = tr_ptrArraySize( &t->webseeds ); + assert( tr_isTorrent( tor ) ); + assert( tr_torrentIsLocked( tor ) ); + assert( t->manager != NULL ); assert( webseedCount == tor->info.webseedCount ); - ret = tr_new0( double, webseedCount ); - now = tr_time_msec( ); for( i=0; imanager ); return ret; } @@ -2307,24 +2287,19 @@ tr_peerGetPieceSpeed_Bps( const tr_peer * peer, uint64_t now, tr_direction direc struct tr_peer_stat * -tr_peerMgrPeerStats( const tr_torrent * tor, - int * setmeCount ) +tr_peerMgrPeerStats( const tr_torrent * tor, int * setmeCount ) { - int i, size; + int i; const Torrent * t = tor->torrentPeers; - const tr_peer ** peers; - tr_peer_stat * ret; - uint64_t now_msec; - time_t now; + const int size = tr_ptrArraySize( &t->peers ); + const tr_peer ** peers = (const tr_peer**) tr_ptrArrayBase( &t->peers ); + const uint64_t now_msec = tr_time_msec( ); + const time_t now = tr_time(); + tr_peer_stat * ret = tr_new0( tr_peer_stat, size ); + assert( tr_isTorrent( tor ) ); + assert( tr_torrentIsLocked( tor ) ); assert( t->manager ); - managerLock( t->manager ); - - size = tr_ptrArraySize( &t->peers ); - peers = (const tr_peer**) tr_ptrArrayBase( &t->peers ); - ret = tr_new0( tr_peer_stat, size ); - now_msec = tr_time_msec( ); - now = tr_time(); for( i=0; imanager ); return ret; } @@ -2388,24 +2362,17 @@ void tr_peerMgrClearInterest( tr_torrent * tor ) { int i; - Torrent * t; - int peerCount; + Torrent * t = tor->torrentPeers; + const int peerCount = tr_ptrArraySize( &t->peers ); assert( tr_isTorrent( tor ) ); - - t = tor->torrentPeers; - - torrentLock( t ); - - peerCount = tr_ptrArraySize( &t->peers ); + assert( tr_torrentIsLocked( tor ) ); for( i=0; ipeers, i ); tr_peerMsgsSetInterested( peer->msgs, FALSE ); } - - torrentUnlock( t ); } /* do we still want this piece and does the peer have it? */ diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 15e415a5c..841f00c77 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -1398,40 +1398,56 @@ tr_torrentFilesFree( tr_file_stat * files, double* tr_torrentWebSpeeds_KBps( const tr_torrent * tor ) { - return tr_isTorrent( tor ) ? tr_peerMgrWebSpeeds_KBps( tor ) : NULL; + double * ret = NULL; + + if( tr_isTorrent( tor ) ) + { + tr_torrentLock( tor ); + ret = tr_peerMgrWebSpeeds_KBps( tor ); + tr_torrentUnlock( tor ); + } + + return ret; } tr_peer_stat * -tr_torrentPeers( const tr_torrent * tor, - int * peerCount ) +tr_torrentPeers( const tr_torrent * tor, int * peerCount ) { tr_peer_stat * ret = NULL; if( tr_isTorrent( tor ) ) + { + tr_torrentLock( tor ); ret = tr_peerMgrPeerStats( tor, peerCount ); + tr_torrentUnlock( tor ); + } return ret; } void -tr_torrentPeersFree( tr_peer_stat * peers, - int peerCount UNUSED ) +tr_torrentPeersFree( tr_peer_stat * peers, int peerCount UNUSED ) { tr_free( peers ); } tr_tracker_stat * -tr_torrentTrackers( const tr_torrent * torrent, - int * setmeTrackerCount ) +tr_torrentTrackers( const tr_torrent * torrent, int * setmeTrackerCount ) { - assert( tr_isTorrent( torrent ) ); + tr_tracker_stat * ret = NULL; - return tr_announcerStats( torrent, setmeTrackerCount ); + if( tr_isTorrent( torrent ) ) + { + tr_torrentLock( torrent ); + ret = tr_announcerStats( torrent, setmeTrackerCount ); + tr_torrentUnlock( torrent ); + } + + return ret; } void -tr_torrentTrackersFree( tr_tracker_stat * trackers, - int trackerCount ) +tr_torrentTrackersFree( tr_tracker_stat * trackers, int trackerCount ) { tr_announcerStatsFree( trackers, trackerCount ); } @@ -1722,6 +1738,8 @@ stopTorrent( void * vtor ) assert( tr_isTorrent( tor ) ); + tr_torrentLock( tor ); + tr_verifyRemove( tor ); tr_peerMgrStopTorrent( tor ); tr_announcerTorrentStopped( tor ); @@ -1731,6 +1749,8 @@ stopTorrent( void * vtor ) if( !tor->isDeleting ) tr_torrentSave( tor ); + + tr_torrentUnlock( tor ); } void diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 838055edb..4ab74b067 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -297,6 +297,11 @@ static inline void tr_torrentLock( const tr_torrent * tor ) tr_sessionLock( tor->session ); } +static inline tr_bool tr_torrentIsLocked( const tr_torrent * tor ) +{ + return tr_sessionIsLocked( tor->session ); +} + static inline void tr_torrentUnlock( const tr_torrent * tor ) { tr_sessionUnlock( tor->session ); -- 2.40.0