diff --git a/libtransmission/peer-io.c b/libtransmission/peer-io.c index 157c7c1ec..8224ccef2 100644 --- a/libtransmission/peer-io.c +++ b/libtransmission/peer-io.c @@ -33,7 +33,7 @@ #include "net.h" #include "peer-io.h" #include "platform.h" /* MAX_STACK_ARRAY_SIZE */ -#include "trevent.h" +#include "trevent.h" /* tr_runInEventThread() */ #include "utils.h" #define MAGIC_NUMBER 206745 diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index 1330a2f31..3cbc82d5a 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -36,7 +36,6 @@ #include "session.h" #include "stats.h" /* tr_statsAddUploaded, tr_statsAddDownloaded */ #include "torrent.h" -#include "trevent.h" #include "utils.h" #include "webseed.h" @@ -48,9 +47,6 @@ enum /* how frequently to change which peers are choked */ RECHOKE_PERIOD_MSEC = ( 10 * 1000 ), - /* minimum interval for refilling peers' request lists */ - REFILL_PERIOD_MSEC = 400, - /* how frequently to reallocate bandwidth */ BANDWIDTH_PERIOD_MSEC = 500, @@ -188,13 +184,13 @@ Torrent; struct tr_peerMgr { - tr_session * session; - tr_ptrArray incomingHandshakes; /* tr_handshake */ - tr_timer * bandwidthTimer; - tr_timer * rechokeTimer; - tr_timer * reconnectTimer; - tr_timer * refillUpkeepTimer; - tr_timer * atomTimer; + tr_session * session; + tr_ptrArray incomingHandshakes; /* tr_handshake */ + struct event * bandwidthTimer; + struct event * rechokeTimer; + struct event * reconnectTimer; + struct event * refillUpkeepTimer; + struct event * atomTimer; }; #define tordbg( t, ... ) \ @@ -467,23 +463,25 @@ tr_peerMgrNew( tr_session * session ) return m; } +static void +deleteTimer( struct event ** t ) +{ + if( *t != NULL ) + { + evtimer_del( *t ); + tr_free( *t ); + *t = NULL; + } +} + static void deleteTimers( struct tr_peerMgr * m ) { - if( m->atomTimer ) - tr_timerFree( &m->atomTimer ); - - if( m->bandwidthTimer ) - tr_timerFree( &m->bandwidthTimer ); - - if( m->rechokeTimer ) - tr_timerFree( &m->rechokeTimer ); - - if( m->reconnectTimer ) - tr_timerFree( &m->reconnectTimer ); - - if( m->refillUpkeepTimer ) - tr_timerFree( &m->refillUpkeepTimer ); + deleteTimer( &m->atomTimer ); + deleteTimer( &m->bandwidthTimer ); + deleteTimer( &m->rechokeTimer ); + deleteTimer( &m->reconnectTimer ); + deleteTimer( &m->refillUpkeepTimer ); } void @@ -1010,9 +1008,17 @@ tr_peerMgrDidPeerRequest( const tr_torrent * tor, return FALSE; } +static void +renewTimer( struct event * timer, int msec ) +{ + const int seconds = msec / 1000; + const int usec = (msec%1000) * 1000; + tr_timerAdd( timer, seconds, usec ); +} + /* cancel requests that are too old */ -static int -refillUpkeep( void * vmgr ) +static void +refillUpkeep( int foo UNUSED, short bar UNUSED, void * vmgr ) { time_t now; time_t too_old; @@ -1063,8 +1069,8 @@ refillUpkeep( void * vmgr ) } } + renewTimer( mgr->refillUpkeepTimer, REFILL_UPKEEP_PERIOD_MSEC ); managerUnlock( mgr ); - return TRUE; } static void @@ -1840,30 +1846,37 @@ tr_peerMgrGetPeers( tr_torrent * tor, return count; } -static int atomPulse ( void * vmgr ); -static int bandwidthPulse ( void * vmgr ); -static int rechokePulse ( void * vmgr ); -static int reconnectPulse ( void * vmgr ); +static void atomPulse ( int, short, void * ); +static void bandwidthPulse ( int, short, void * ); +static void rechokePulse ( int, short, void * ); +static void reconnectPulse ( int, short, void * ); + +static struct event * +createTimer( int msec, void (*callback)(int, short, void *), void * cbdata ) +{ + struct event * timer = tr_new0( struct event, 1 ); + evtimer_set( timer, callback, cbdata ); + renewTimer( timer, msec ); + return timer; +} static void ensureMgrTimersExist( struct tr_peerMgr * m ) { - tr_session * s = m->session; - if( m->atomTimer == NULL ) - m->atomTimer = tr_timerNew( s, atomPulse, m, ATOM_PERIOD_MSEC ); + m->atomTimer = createTimer( ATOM_PERIOD_MSEC, atomPulse, m ); if( m->bandwidthTimer == NULL ) - m->bandwidthTimer = tr_timerNew( s, bandwidthPulse, m, BANDWIDTH_PERIOD_MSEC ); + m->bandwidthTimer = createTimer( BANDWIDTH_PERIOD_MSEC, bandwidthPulse, m ); if( m->rechokeTimer == NULL ) - m->rechokeTimer = tr_timerNew( s, rechokePulse, m, RECHOKE_PERIOD_MSEC ); + m->rechokeTimer = createTimer( RECHOKE_PERIOD_MSEC, rechokePulse, m ); if( m->reconnectTimer == NULL ) - m->reconnectTimer = tr_timerNew( s, reconnectPulse, m, RECONNECT_PERIOD_MSEC ); + m->reconnectTimer = createTimer( RECONNECT_PERIOD_MSEC, reconnectPulse, m ); if( m->refillUpkeepTimer == NULL ) - m->refillUpkeepTimer = tr_timerNew( s, refillUpkeep, m, REFILL_UPKEEP_PERIOD_MSEC ); + m->refillUpkeepTimer = createTimer( REFILL_UPKEEP_PERIOD_MSEC, refillUpkeep, m ); } void @@ -1877,7 +1890,7 @@ tr_peerMgrStartTorrent( tr_torrent * tor ) t->isRunning = TRUE; - rechokePulse( t->manager ); + rechokePulse( 0, 0, t->manager ); managerUnlock( t->manager ); } @@ -2320,8 +2333,8 @@ rechokeTorrent( Torrent * t, const uint64_t now ) tr_free( choke ); } -static int -rechokePulse( void * vmgr ) +static void +rechokePulse( int foo UNUSED, short bar UNUSED, void * vmgr ) { uint64_t now; tr_torrent * tor = NULL; @@ -2333,8 +2346,8 @@ rechokePulse( void * vmgr ) if( tor->isRunning ) rechokeTorrent( tor->torrentPeers, now ); + renewTimer( mgr->rechokeTimer, RECHOKE_PERIOD_MSEC ); managerUnlock( mgr ); - return TRUE; } /*** @@ -2864,8 +2877,8 @@ enforceSessionPeerLimit( tr_session * session, uint64_t now ) } -static int -reconnectPulse( void * vmgr ) +static void +reconnectPulse( int foo UNUSED, short bar UNUSED, void * vmgr ) { tr_torrent * tor; tr_peerMgr * mgr = vmgr; @@ -2888,8 +2901,8 @@ reconnectPulse( void * vmgr ) if( tor->isRunning ) reconnectTorrent( tor->torrentPeers ); + renewTimer( mgr->reconnectTimer, RECONNECT_PERIOD_MSEC ); managerUnlock( mgr ); - return TRUE; } /**** @@ -2916,8 +2929,8 @@ pumpAllPeers( tr_peerMgr * mgr ) } } -static int -bandwidthPulse( void * vmgr ) +static void +bandwidthPulse( int foo UNUSED, short bar UNUSED, void * vmgr ) { tr_torrent * tor = NULL; tr_peerMgr * mgr = vmgr; @@ -2953,8 +2966,8 @@ bandwidthPulse( void * vmgr ) if( tor->isRunning && ( tor->error == TR_STAT_LOCAL_ERROR )) tr_torrentStop( tor ); + renewTimer( mgr->bandwidthTimer, BANDWIDTH_PERIOD_MSEC ); managerUnlock( mgr ); - return TRUE; } /*** @@ -3013,8 +3026,8 @@ getMaxAtomCount( const tr_torrent * tor ) return n * 10; } -static int -atomPulse( void * vmgr ) +static void +atomPulse( int foo UNUSED, short bar UNUSED, void * vmgr ) { tr_torrent * tor = NULL; tr_peerMgr * mgr = vmgr; @@ -3072,6 +3085,6 @@ atomPulse( void * vmgr ) } } + renewTimer( mgr->atomTimer, ATOM_PERIOD_MSEC ); managerUnlock( mgr ); - return TRUE; } diff --git a/libtransmission/port-forwarding.c b/libtransmission/port-forwarding.c index b73784ae3..228972257 100644 --- a/libtransmission/port-forwarding.c +++ b/libtransmission/port-forwarding.c @@ -24,7 +24,6 @@ #include "port-forwarding.h" #include "session.h" #include "torrent.h" -#include "trevent.h" #include "upnp.h" #include "utils.h" diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 65f963388..73d99873c 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -40,7 +40,7 @@ #include "session.h" #include "torrent.h" #include "torrent-magnet.h" -#include "trevent.h" +#include "trevent.h" /* tr_runInEventThread() */ #include "utils.h" #include "verify.h" diff --git a/libtransmission/trevent.c b/libtransmission/trevent.c index 1164002e4..89838ed00 100644 --- a/libtransmission/trevent.c +++ b/libtransmission/trevent.c @@ -132,18 +132,6 @@ typedef struct tr_event_handle } tr_event_handle; -typedef int timer_func ( void* ); - -struct tr_timer -{ - tr_bool inCallback; - timer_func * func; - void * user_data; - struct tr_event_handle * eh; - struct timeval tv; - struct event event; -}; - struct tr_run_data { void ( *func )( void * ); @@ -280,7 +268,7 @@ tr_eventClose( tr_session * session ) **/ tr_bool -tr_amInEventThread( tr_session * session ) +tr_amInEventThread( const tr_session * session ) { assert( tr_isSession( session ) ); assert( session->events != NULL ); @@ -292,67 +280,6 @@ tr_amInEventThread( tr_session * session ) *** **/ -static void -timerCallback( int fd UNUSED, - short event UNUSED, - void * vtimer ) -{ - int more; - struct tr_timer * timer = vtimer; - - timer->inCallback = 1; - more = ( *timer->func )( timer->user_data ); - timer->inCallback = 0; - - if( !more ) - tr_timerFree( &timer ); - else { - assert( tr_isTimeval( &timer->tv ) ); - evtimer_add( &timer->event, &timer->tv ); - } -} - -void -tr_timerFree( tr_timer ** ptimer ) -{ - tr_timer * timer; - - /* zero out the argument passed in */ - assert( ptimer ); - timer = *ptimer; - *ptimer = NULL; - - /* destroy the timer directly or via the command queue */ - if( timer && !timer->inCallback ) - { - assert( tr_amInEventThread( timer->eh->session ) ); - event_del( &timer->event ); - tr_free( timer ); - } -} - -tr_timer* -tr_timerNew( tr_session * session, - timer_func func, - void * user_data, - uint64_t interval_milliseconds ) -{ - tr_timer * timer; - - assert( tr_amInEventThread( session ) ); - - timer = tr_new0( tr_timer, 1 ); - timer->func = func; - timer->user_data = user_data; - timer->eh = session->events; - - tr_timevalMsec( interval_milliseconds, &timer->tv ); - evtimer_set( &timer->event, timerCallback, timer ); - evtimer_add( &timer->event, &timer->tv ); - - return timer; -} - void tr_runInEventThread( tr_session * session, void func( void* ), void * user_data ) diff --git a/libtransmission/trevent.h b/libtransmission/trevent.h index 6ddf46e01..13d097bab 100644 --- a/libtransmission/trevent.h +++ b/libtransmission/trevent.h @@ -27,31 +27,10 @@ void tr_eventInit( tr_session * ); void tr_eventClose( tr_session * ); +tr_bool tr_amInEventThread( const tr_session * ); + +void tr_runInEventThread( tr_session *, void func( void* ), void * user_data ); + struct event_base * tr_eventGetBase( tr_session * ); - -typedef struct tr_timer tr_timer; - -/** - * Calls timer_func(user_data) after the specified interval. - * The timer is freed if timer_func returns zero. - * Otherwise, it's called again after the same interval. - */ -tr_timer* tr_timerNew( tr_session * handle, - int func( void * user_data ), - void * user_data, - uint64_t timeout_milliseconds ); - -/** - * Frees a timer and sets the timer pointer to NULL. - */ -void tr_timerFree( tr_timer ** timer ); - - -tr_bool tr_amInEventThread( tr_session * ); - -void tr_runInEventThread( tr_session * session, - void func( void* ), - void * user_data ); - #endif diff --git a/libtransmission/web.c b/libtransmission/web.c index 27cffce92..3fb1c937d 100644 --- a/libtransmission/web.c +++ b/libtransmission/web.c @@ -22,7 +22,7 @@ #include "list.h" #include "net.h" /* socklen_t */ #include "session.h" -#include "trevent.h" +#include "trevent.h" /* tr_runInEventThread() */ #include "utils.h" #include "version.h" #include "web.h"