Changeset 3017


Ignore:
Timestamp:
Sep 10, 2007, 1:15:21 AM (14 years ago)
Author:
charles
Message:

this one's a biggie: the per-torrent threads are gone now. w00t!

Location:
branches/encryption/libtransmission
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/encryption/libtransmission/inout.c

    r2998 r3017  
    2222#include "completion.h"
    2323#include "crypto.h"
     24#include "fastresume.h"
    2425#include "fdlimit.h"
    2526#include "inout.h"
     
    3031#include "utils.h"
    3132
    32 struct tr_io_s
     33struct tr_io
    3334{
    3435    tr_torrent * tor;
     
    236237}
    237238
     239/**
     240***
     241**/
     242
    238243void
    239 tr_ioCheckFiles( tr_torrent * tor )
    240 {
    241     assert( tor != NULL );
    242     assert( tor->completion != NULL );
    243     assert( tor->info.pieceCount > 0 );
    244 
    245     if( tor->uncheckedPieces != NULL )
    246     {
    247         int i;
    248 
    249         /* remove the unchecked pieces from completion... */
    250         for( i=0; i<tor->info.pieceCount; ++i )
    251             if( tr_bitfieldHas( tor->uncheckedPieces, i ) )
    252                 tr_cpPieceRem( tor->completion, i );
    253 
    254         tr_inf( "Verifying some pieces of \"%s\"", tor->info.name );
    255 
    256         for( i=0; i<tor->info.pieceCount; ++i )
    257         {
    258             if( !tr_bitfieldHas( tor->uncheckedPieces, i ) )
    259                 continue;
    260 
    261             tr_torrentSetHasPiece( tor, i, !checkPiece( tor, i ) );
    262             tr_bitfieldRem( tor->uncheckedPieces, i );
    263         }
    264 
    265         tr_bitfieldFree( tor->uncheckedPieces );
    266         tor->uncheckedPieces = NULL;
    267         tor->fastResumeDirty = TRUE;
    268     }
    269 }
    270 
    271 /****
    272 *****  Life Cycle
    273 ****/
    274 
    275 tr_io_t*
    276 tr_ioNew ( tr_torrent * tor )
    277 {
    278     tr_io_t * io = tr_calloc( 1, sizeof( tr_io_t ) );
    279     io->tor = tor;
    280     return io;
    281 }
    282 
    283 
    284 void
    285 tr_ioSync( tr_io_t * io )
    286 {
    287     if( io != NULL )
    288     {
    289         int i;
    290         const tr_info_t * info = &io->tor->info;
    291 
    292         for( i=0; i<info->fileCount; ++i )
    293             tr_fdFileClose( io->tor->destination, info->files[i].name );
    294     }
    295 }
    296 
    297 void
    298 tr_ioClose( tr_io_t * io )
    299 {
    300     if( io != NULL )
    301     {
    302         tr_ioSync( io );
    303         tr_free( io );
    304     }
     244tr_ioClose( const tr_torrent * tor )
     245{
     246    int i;
     247    const tr_info_t * info = &tor->info;
     248
     249    for( i=0; i<info->fileCount; ++i )
     250        tr_fdFileClose( tor->destination, info->files[i].name );
    305251}
    306252
    307253int
    308 tr_ioHash( tr_io_t * io, int pieceIndex )
     254tr_ioHash( tr_io * io, int pieceIndex )
    309255{
    310256    int ret;
     
    404350        }
    405351        stopCurrent = FALSE;
    406         tor->fastResumeDirty = TRUE;
     352        tr_fastResumeSave( tor );
    407353        fireCheckDone( tor, currentNode.recheck_done_cb, currentNode.status_when_done );
    408354    }
  • branches/encryption/libtransmission/inout.h

    r2998 r3017  
    2828struct tr_torrent;
    2929
    30 typedef struct tr_io_s tr_io_t;
    31 
    32 void tr_ioCheckFiles  ( tr_torrent * );
    33 
    34 tr_io_t * tr_ioNew ( tr_torrent * );
     30typedef struct tr_io tr_io;
    3531
    3632/***********************************************************************
     
    5046 * Hashes the specified piece and updates the completion accordingly.
    5147 **********************************************************************/
    52 int tr_ioHash ( tr_io_t *, int piece );
     48int tr_ioHash ( tr_io *, int piece );
    5349
    54 /***********************************************************************
    55  * tr_ioSync
    56  ***********************************************************************
    57  * Flush all data on disc by closing all files, and update the cache
    58  * file.
    59  **********************************************************************/
    60 void tr_ioSync( tr_io_t * );
    61 
    62 void      tr_ioClose       ( tr_io_t * );
     50/* close all the files associated with this torrent*/
     51void tr_ioClose( const tr_torrent * );
    6352
    6453/**
  • branches/encryption/libtransmission/internal.h

    r2998 r3017  
    4545#define FALSE 0
    4646#endif
     47
     48#include "timer.h"
    4749
    4850int tr_trackerInfoInit( struct tr_tracker_info_s  * info,
     
    115117    struct tr_ratecontrol     * swarmspeed;
    116118
     119    tr_timer_tag                saveTag;
     120
    117121    int                        error;
    118122    char                       errorString[128];
     
    145149    char                       runStatusToSaveIsSet;
    146150    cp_status_t                cpStatus;
    147     struct tr_thread_s       * thread;
    148151    struct tr_rwlock_s       * lock;
    149152
    150153    struct tr_tracker        * tracker;
    151154    struct tr_publisher_tag  * trackerSubscription;
    152     struct tr_io_s           * io;
    153155    uint64_t                   startDate;
    154156    uint64_t                   stopDate;
    155157    char                       ioLoaded;
    156     char                       fastResumeDirty;
    157158
    158159    uint64_t                   downloadedCur;
  • branches/encryption/libtransmission/torrent.c

    r2998 r3017  
    149149***/
    150150
    151 static void setRunState( tr_torrent *, run_status_t );
     151static void tr_torrentHasStopped( tr_torrent * );
    152152
    153153static void
     
    180180
    181181        case TR_TRACKER_STOPPED:
    182             if( tor->runStatus == TR_RUN_STOPPING_NET_WAIT )
    183                 setRunState( tor, TR_RUN_STOPPED );
     182            assert( tor->runStatus == TR_RUN_STOPPING_NET_WAIT );
     183            tr_torrentHasStopped( tor );
    184184            break;
    185185    }
     
    261261}
    262262
    263 static void torrentThreadLoop( void * );
    264 static void fastResumeSave( tr_torrent * );
     263static void recheckCpState( tr_torrent * );
     264
     265static void
     266tr_torrentStartImpl( tr_torrent * tor )
     267{
     268    assert( tor != NULL );
     269    assert( tor->runStatus == TR_RUN_RUNNING );
     270
     271    *tor->errorString = '\0';
     272    tr_torrentResetTransferStats( tor );
     273    recheckCpState( tor );
     274    tor->startDate = tr_date();
     275    tr_trackerStart( tor->tracker );
     276}
    265277
    266278static void
    267279recheckDoneCB( tr_torrent * tor )
    268280{
    269     tr_peerMgrUpdateCompletion( tor->handle->peerMgr, tor->info.hash );
    270     fastResumeSave( tor );
     281    tr_peerMgrUpdateCompletion( tor->handle->peerMgr,
     282                                tor->info.hash );
     283
     284    recheckCpState( tor );
    271285
    272286    if( tor->runStatus == TR_RUN_RUNNING )
    273     {
    274         tr_trackerStart( tor->tracker );
    275 
    276         if( tor->thread == NULL )
    277             tor->thread = tr_threadNew( torrentThreadLoop, tor, "torrent thread" );
    278     }
     287        tr_torrentStartImpl( tor );
    279288}
    280289
     
    628637#endif
    629638
     639static int
     640saveFastResumeNow( void * vtor )
     641{
     642    tr_torrent * tor = (tr_torrent *) vtor;
     643
     644    tr_fastResumeSave( tor );
     645    recheckCpState( tor );
     646
     647    tr_timerFree( &tor->saveTag );
     648    return FALSE;
     649}
     650
    630651static void
    631 fastResumeSave( tr_torrent * tor )
    632 {
    633     tr_fastResumeSave( tor );
    634     tor->fastResumeDirty = FALSE;
    635 }
     652saveFastResumeSoon( void * vtor )
     653{
     654    tr_torrent * tor = (tr_torrent *) vtor;
     655
     656    if( tor->saveTag == NULL )
     657        tor->saveTag = tr_timerNew( tor->handle, saveFastResumeNow, tor, NULL, 1000 );
     658}
     659
     660/**
     661***
     662**/
    636663
    637664void
     
    640667    tr_free( tor->destination );
    641668    tor->destination = tr_strdup( path );
    642     fastResumeSave( tor );
     669    saveFastResumeSoon( tor );
    643670}
    644671
     
    9811008}
    9821009
    983 
    984 #if 0
    985 int tr_torrentAttachPeer( tr_torrent * tor, tr_peer_t * peer )
    986 {
    987     int i;
    988     tr_peer_t * otherPeer;
    989 
    990     assert( tor != NULL );
    991     assert( peer != NULL );
    992 
    993     if( tor->peerCount >= TR_MAX_PEER_COUNT )
    994     {
    995         tr_peerDestroy(  peer );
    996         return 0;
    997     }
    998 
    999     /* Don't accept two connections from the same IP */
    1000     for( i = 0; i < tor->peerCount; i++ )
    1001     {
    1002         otherPeer = tor->peers[i];
    1003         if( !memcmp( tr_peerAddress( peer ), tr_peerAddress( otherPeer ), 4 ) )
    1004         {
    1005             tr_peerDestroy(  peer );
    1006             return 0;
    1007         }
    1008     }
    1009 
    1010     tr_peerSetPrivate( peer, tor->info.flags & TR_FLAG_PRIVATE ||
    1011                        tor->pexDisabled );
    1012     tr_peerSetTorrent( peer, tor );
    1013     tor->peers[tor->peerCount++] = peer;
    1014 
    1015     return 1;
    1016 }
    1017 #endif
    1018 
    10191010/***
    10201011****
    10211012***/
    10221013
    1023 static void setRunState( tr_torrent * tor, run_status_t run )
    1024 {
    1025     tr_torrentWriterLock( tor );
    1026     tor->runStatus = run;
    1027     tr_torrentWriterUnlock( tor );
    1028 }
    1029 
    10301014void
    10311015tr_torrentStart( tr_torrent * tor )
    10321016{
    10331017    tr_ioRecheckAdd( tor, recheckDoneCB, TR_RUN_RUNNING );
    1034 }
    1035 
    1036 void tr_torrentStop( tr_torrent * tor )
    1037 {
    1038     if((tor->runStatus != TR_RUN_STOPPING) && (tor->runStatus != TR_RUN_STOPPED) )
    1039         setRunState( tor, TR_RUN_STOPPING );
    1040 }
    1041 
    1042 void tr_torrentClose( tr_torrent * tor )
    1043 {
    1044     tor->runStatusToSave = tor->runStatus;
    1045     tor->runStatusToSaveIsSet = TRUE;
    1046     tr_torrentStop( tor );
    1047     tor->dieFlag = TRUE;
    10481018}
    10491019
     
    10911061
    10921062    tr_sharedUnlock( h->shared );
     1063}
     1064
     1065static void
     1066tr_torrentHasStopped( tr_torrent * tor )
     1067{
     1068    /* close the IO */
     1069    tr_ioClose( tor );
     1070    saveFastResumeSoon( tor );
     1071
     1072    /* free everything */
     1073    tr_torrentFree( tor );
     1074}
     1075
     1076void
     1077tr_torrentStop( tr_torrent * tor )
     1078{
     1079    if( tor->runStatus==TR_RUN_STOPPING || tor->runStatus==TR_RUN_STOPPED )
     1080        return;
     1081
     1082    /* close the peers */
     1083    tr_peerMgrStopTorrent( tor->handle->peerMgr, tor->info.hash );
     1084
     1085    /* tell the tracker we're stopping */
     1086    tor->runStatus = TR_RUN_STOPPING_NET_WAIT;
     1087    tor->stopDate = tr_date();
     1088    tr_trackerStop( tor->tracker );
     1089}
     1090
     1091void tr_torrentClose( tr_torrent * tor )
     1092{
     1093    tor->runStatusToSave = tor->runStatus;
     1094    tor->runStatusToSaveIsSet = TRUE;
     1095    tr_torrentStop( tor );
     1096    tor->dieFlag = TRUE;
    10931097}
    10941098
     
    11081112            tr_trackerCompleted( tor->tracker ); /* tell the tracker */
    11091113        }
    1110         tr_ioSync( tor->io );
    1111         fastResumeSave( tor );
     1114        tr_ioClose( tor );
     1115        saveFastResumeSoon( tor );
    11121116    }
    11131117    tr_torrentWriterUnlock( tor );
    11141118}
    1115 
    1116 static void
    1117 torrentThreadLoop ( void * _tor )
    1118 {
    1119     tr_torrent * tor = _tor;
    1120 
    1121     /* loop until the torrent is being deleted */
    1122     while( ! ( tor->dieFlag && (tor->runStatus == TR_RUN_STOPPED) ) )
    1123     {
    1124         /* sleep a little while */
    1125         tr_wait( tor->runStatus == TR_RUN_STOPPED ? 1600 : 100 );
    1126 
    1127         if( tor->fastResumeDirty )
    1128         {
    1129             fastResumeSave( tor );
    1130             recheckCpState( tor );
    1131         }
    1132 
    1133         /* if we're stopping... */
    1134         if( tor->runStatus == TR_RUN_STOPPING )
    1135         {
    1136             tr_torrentWriterLock( tor );
    1137 
    1138             /* close the IO */
    1139             tr_ioClose( tor->io );
    1140             tor->io = NULL;
    1141             fastResumeSave( tor );
    1142 
    1143             /* close the peers */
    1144             tr_peerMgrStopTorrent( tor->handle->peerMgr, tor->info.hash );
    1145 
    1146             /* resest the transfer rates */
    1147             tr_rcReset( tor->download );
    1148             tr_rcReset( tor->upload );
    1149             tr_rcReset( tor->swarmspeed );
    1150 
    1151             /* tell the tracker we're stopping */
    1152             tr_trackerStop( tor->tracker );
    1153             tor->runStatus = TR_RUN_STOPPING_NET_WAIT;
    1154             tor->stopDate = tr_date();
    1155             tr_torrentWriterUnlock( tor );
    1156         }
    1157 
    1158         /* ping our peers if we're running... */
    1159         if( tor->runStatus == TR_RUN_RUNNING )
    1160         {
    1161             /* starting to run... */
    1162             if( tor->io == NULL )
    1163             {
    1164                 *tor->errorString = '\0';
    1165                 tr_torrentResetTransferStats( tor );
    1166                 tor->io = tr_ioNew( tor );
    1167                 tor->startDate = tr_date();
    1168             }
    1169 
    1170             /* refresh our completion state */
    1171             recheckCpState( tor );
    1172         }
    1173     }
    1174 
    1175     tr_ioClose( tor->io );
    1176     tr_torrentFree( tor );
    1177 }
    1178 
    11791119
    11801120/**
     
    12051145             priority, tor->info.files[fileIndex].name );
    12061146
    1207     tor->fastResumeDirty = TRUE;
     1147    saveFastResumeSoon( tor );
    12081148
    12091149    tr_torrentWriterUnlock( tor );
     
    13191259    tr_cpInvalidateDND ( tor->completion );
    13201260
    1321     tor->fastResumeDirty = TRUE;
     1261    saveFastResumeSoon( tor );
    13221262
    13231263    tr_torrentWriterUnlock( tor );
Note: See TracChangeset for help on using the changeset viewer.