Changeset 9704


Ignore:
Timestamp:
Dec 10, 2009, 5:52:46 AM (13 years ago)
Author:
charles
Message:

(trunk libT) finally get rid of the last remnants of tr_timer

Location:
trunk/libtransmission
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/peer-io.c

    r9671 r9704  
    3434#include "peer-io.h"
    3535#include "platform.h" /* MAX_STACK_ARRAY_SIZE */
    36 #include "trevent.h"
     36#include "trevent.h" /* tr_runInEventThread() */
    3737#include "utils.h"
    3838
  • trunk/libtransmission/peer-mgr.c

    r9671 r9704  
    3737#include "stats.h" /* tr_statsAddUploaded, tr_statsAddDownloaded */
    3838#include "torrent.h"
    39 #include "trevent.h"
    4039#include "utils.h"
    4140#include "webseed.h"
     
    4847    /* how frequently to change which peers are choked */
    4948    RECHOKE_PERIOD_MSEC = ( 10 * 1000 ),
    50 
    51     /* minimum interval for refilling peers' request lists */
    52     REFILL_PERIOD_MSEC = 400,
    5349
    5450    /* how frequently to reallocate bandwidth */
     
    189185struct tr_peerMgr
    190186{
    191     tr_session      * session;
    192     tr_ptrArray       incomingHandshakes; /* tr_handshake */
    193     tr_timer        * bandwidthTimer;
    194     tr_timer        * rechokeTimer;
    195     tr_timer        * reconnectTimer;
    196     tr_timer        * refillUpkeepTimer;
    197     tr_timer        * atomTimer;
     187    tr_session    * session;
     188    tr_ptrArray     incomingHandshakes; /* tr_handshake */
     189    struct event  * bandwidthTimer;
     190    struct event  * rechokeTimer;
     191    struct event  * reconnectTimer;
     192    struct event  * refillUpkeepTimer;
     193    struct event  * atomTimer;
    198194};
    199195
     
    469465
    470466static void
     467deleteTimer( struct event ** t )
     468{
     469    if( *t != NULL )
     470    {
     471        evtimer_del( *t );
     472        tr_free( *t );
     473        *t = NULL;
     474    }
     475}
     476
     477static void
    471478deleteTimers( struct tr_peerMgr * m )
    472479{
    473     if( m->atomTimer )
    474         tr_timerFree( &m->atomTimer );
    475 
    476     if( m->bandwidthTimer )
    477         tr_timerFree( &m->bandwidthTimer );
    478 
    479     if( m->rechokeTimer )
    480         tr_timerFree( &m->rechokeTimer );
    481 
    482     if( m->reconnectTimer )
    483         tr_timerFree( &m->reconnectTimer );
    484 
    485     if( m->refillUpkeepTimer )
    486         tr_timerFree( &m->refillUpkeepTimer );
     480    deleteTimer( &m->atomTimer );
     481    deleteTimer( &m->bandwidthTimer );
     482    deleteTimer( &m->rechokeTimer );
     483    deleteTimer( &m->reconnectTimer );
     484    deleteTimer( &m->refillUpkeepTimer );
    487485}
    488486
     
    10111009}
    10121010
     1011static void
     1012renewTimer( struct event * timer, int msec )
     1013{
     1014    const int seconds =  msec / 1000;
     1015    const int usec = (msec%1000) * 1000;
     1016    tr_timerAdd( timer, seconds, usec );
     1017}
     1018
    10131019/* cancel requests that are too old */
    1014 static int
    1015 refillUpkeep( void * vmgr )
     1020static void
     1021refillUpkeep( int foo UNUSED, short bar UNUSED, void * vmgr )
    10161022{
    10171023    time_t now;
     
    10641070    }
    10651071
     1072    renewTimer( mgr->refillUpkeepTimer, REFILL_UPKEEP_PERIOD_MSEC );
    10661073    managerUnlock( mgr );
    1067     return TRUE;
    10681074}
    10691075
     
    18411847}
    18421848
    1843 static int atomPulse      ( void * vmgr );
    1844 static int bandwidthPulse ( void * vmgr );
    1845 static int rechokePulse   ( void * vmgr );
    1846 static int reconnectPulse ( void * vmgr );
     1849static void atomPulse      ( int, short, void * );
     1850static void bandwidthPulse ( int, short, void * );
     1851static void rechokePulse   ( int, short, void * );
     1852static void reconnectPulse ( int, short, void * );
     1853
     1854static struct event *
     1855createTimer( int msec, void (*callback)(int, short, void *), void * cbdata )
     1856{
     1857    struct event * timer = tr_new0( struct event, 1 );
     1858    evtimer_set( timer, callback, cbdata );
     1859    renewTimer( timer, msec );
     1860    return timer;
     1861}
    18471862
    18481863static void
    18491864ensureMgrTimersExist( struct tr_peerMgr * m )
    18501865{
    1851     tr_session * s = m->session;
    1852 
    18531866    if( m->atomTimer == NULL )
    1854         m->atomTimer = tr_timerNew( s, atomPulse, m, ATOM_PERIOD_MSEC );
     1867        m->atomTimer = createTimer( ATOM_PERIOD_MSEC, atomPulse, m );
    18551868
    18561869    if( m->bandwidthTimer == NULL )
    1857         m->bandwidthTimer = tr_timerNew( s, bandwidthPulse, m, BANDWIDTH_PERIOD_MSEC );
     1870        m->bandwidthTimer = createTimer( BANDWIDTH_PERIOD_MSEC, bandwidthPulse, m );
    18581871
    18591872    if( m->rechokeTimer == NULL )
    1860         m->rechokeTimer = tr_timerNew( s, rechokePulse, m, RECHOKE_PERIOD_MSEC );
     1873        m->rechokeTimer = createTimer( RECHOKE_PERIOD_MSEC, rechokePulse, m );
    18611874
    18621875    if( m->reconnectTimer == NULL )
    1863         m->reconnectTimer = tr_timerNew( s, reconnectPulse, m, RECONNECT_PERIOD_MSEC );
     1876        m->reconnectTimer = createTimer( RECONNECT_PERIOD_MSEC, reconnectPulse, m );
    18641877
    18651878    if( m->refillUpkeepTimer == NULL )
    1866         m->refillUpkeepTimer = tr_timerNew( s, refillUpkeep, m, REFILL_UPKEEP_PERIOD_MSEC );
     1879        m->refillUpkeepTimer = createTimer( REFILL_UPKEEP_PERIOD_MSEC, refillUpkeep, m );
    18671880}
    18681881
     
    18781891    t->isRunning = TRUE;
    18791892
    1880     rechokePulse( t->manager );
     1893    rechokePulse( 0, 0, t->manager );
    18811894    managerUnlock( t->manager );
    18821895}
     
    23212334}
    23222335
    2323 static int
    2324 rechokePulse( void * vmgr )
     2336static void
     2337rechokePulse( int foo UNUSED, short bar UNUSED, void * vmgr )
    23252338{
    23262339    uint64_t now;
     
    23342347            rechokeTorrent( tor->torrentPeers, now );
    23352348
     2349    renewTimer( mgr->rechokeTimer, RECHOKE_PERIOD_MSEC );
    23362350    managerUnlock( mgr );
    2337     return TRUE;
    23382351}
    23392352
     
    28652878
    28662879
    2867 static int
    2868 reconnectPulse( void * vmgr )
     2880static void
     2881reconnectPulse( int foo UNUSED, short bar UNUSED, void * vmgr )
    28692882{
    28702883    tr_torrent * tor;
     
    28892902            reconnectTorrent( tor->torrentPeers );
    28902903
     2904    renewTimer( mgr->reconnectTimer, RECONNECT_PERIOD_MSEC );
    28912905    managerUnlock( mgr );
    2892     return TRUE;
    28932906}
    28942907
     
    29172930}
    29182931
    2919 static int
    2920 bandwidthPulse( void * vmgr )
     2932static void
     2933bandwidthPulse( int foo UNUSED, short bar UNUSED, void * vmgr )
    29212934{
    29222935    tr_torrent * tor = NULL;
     
    29542967            tr_torrentStop( tor );
    29552968
     2969    renewTimer( mgr->bandwidthTimer, BANDWIDTH_PERIOD_MSEC );
    29562970    managerUnlock( mgr );
    2957     return TRUE;
    29582971}
    29592972
     
    30143027}
    30153028
    3016 static int
    3017 atomPulse( void * vmgr )
     3029static void
     3030atomPulse( int foo UNUSED, short bar UNUSED, void * vmgr )
    30183031{
    30193032    tr_torrent * tor = NULL;
     
    30733086    }
    30743087
     3088    renewTimer( mgr->atomTimer, ATOM_PERIOD_MSEC );
    30753089    managerUnlock( mgr );
    3076     return TRUE;
    3077 }
     3090}
  • trunk/libtransmission/port-forwarding.c

    r9671 r9704  
    2525#include "session.h"
    2626#include "torrent.h"
    27 #include "trevent.h"
    2827#include "upnp.h"
    2928#include "utils.h"
  • trunk/libtransmission/torrent.c

    r9691 r9704  
    4141#include "torrent.h"
    4242#include "torrent-magnet.h"
    43 #include "trevent.h"
     43#include "trevent.h" /* tr_runInEventThread() */
    4444#include "utils.h"
    4545#include "verify.h"
  • trunk/libtransmission/trevent.c

    r9671 r9704  
    133133tr_event_handle;
    134134
    135 typedef int timer_func ( void* );
    136 
    137 struct tr_timer
    138 {
    139     tr_bool                   inCallback;
    140     timer_func *              func;
    141     void *                    user_data;
    142     struct tr_event_handle *  eh;
    143     struct timeval            tv;
    144     struct event              event;
    145 };
    146 
    147135struct tr_run_data
    148136{
     
    281269
    282270tr_bool
    283 tr_amInEventThread( tr_session * session )
     271tr_amInEventThread( const tr_session * session )
    284272{
    285273    assert( tr_isSession( session ) );
     
    292280***
    293281**/
    294 
    295 static void
    296 timerCallback( int    fd UNUSED,
    297                short  event UNUSED,
    298                void * vtimer )
    299 {
    300     int               more;
    301     struct tr_timer * timer = vtimer;
    302 
    303     timer->inCallback = 1;
    304     more = ( *timer->func )( timer->user_data );
    305     timer->inCallback = 0;
    306 
    307     if( !more )
    308         tr_timerFree( &timer );
    309     else {
    310         assert( tr_isTimeval( &timer->tv ) );
    311         evtimer_add( &timer->event, &timer->tv );
    312     }
    313 }
    314 
    315 void
    316 tr_timerFree( tr_timer ** ptimer )
    317 {
    318     tr_timer * timer;
    319 
    320     /* zero out the argument passed in */
    321     assert( ptimer );
    322     timer = *ptimer;
    323     *ptimer = NULL;
    324 
    325     /* destroy the timer directly or via the command queue */
    326     if( timer && !timer->inCallback )
    327     {
    328         assert( tr_amInEventThread( timer->eh->session ) );
    329         event_del( &timer->event );
    330         tr_free( timer );
    331     }
    332 }
    333 
    334 tr_timer*
    335 tr_timerNew( tr_session  * session,
    336              timer_func    func,
    337              void        * user_data,
    338              uint64_t      interval_milliseconds )
    339 {
    340     tr_timer * timer;
    341 
    342     assert( tr_amInEventThread( session ) );
    343 
    344     timer = tr_new0( tr_timer, 1 );
    345     timer->func = func;
    346     timer->user_data = user_data;
    347     timer->eh = session->events;
    348 
    349     tr_timevalMsec( interval_milliseconds, &timer->tv );
    350     evtimer_set( &timer->event, timerCallback, timer );
    351     evtimer_add( &timer->event,  &timer->tv );
    352 
    353     return timer;
    354 }
    355282
    356283void
  • trunk/libtransmission/trevent.h

    r9671 r9704  
    2828void      tr_eventClose( tr_session * );
    2929
     30tr_bool   tr_amInEventThread( const tr_session * );
     31
     32void      tr_runInEventThread( tr_session *, void func( void* ), void * user_data );
     33
    3034struct event_base * tr_eventGetBase( tr_session * );
    3135
    32 
    33 typedef struct tr_timer  tr_timer;
    34 
    35 /**
    36  * Calls timer_func(user_data) after the specified interval.
    37  * The timer is freed if timer_func returns zero.
    38  * Otherwise, it's called again after the same interval.
    39  */
    40 tr_timer* tr_timerNew( tr_session * handle,
    41                        int func( void * user_data ),
    42                        void * user_data,
    43                        uint64_t timeout_milliseconds );
    44 
    45 /**
    46  * Frees a timer and sets the timer pointer to NULL.
    47  */
    48 void      tr_timerFree( tr_timer ** timer );
    49 
    50 
    51 tr_bool   tr_amInEventThread( tr_session * );
    52 
    53 void      tr_runInEventThread( tr_session * session,
    54                                void         func( void* ),
    55                                void       * user_data );
    56 
    5736#endif
  • trunk/libtransmission/web.c

    r9671 r9704  
    2323#include "net.h" /* socklen_t */
    2424#include "session.h"
    25 #include "trevent.h"
     25#include "trevent.h" /* tr_runInEventThread() */
    2626#include "utils.h"
    2727#include "version.h"
Note: See TracChangeset for help on using the changeset viewer.