Changeset 3254
- Timestamp:
- Oct 1, 2007, 3:17:15 PM (15 years ago)
- Location:
- trunk/libtransmission
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/internal.h
r3225 r3254 140 140 run_status_t runStatusToSave; 141 141 cp_status_t cpStatus; 142 struct tr_lock * lock;143 142 144 143 struct tr_tracker * tracker; … … 189 188 struct tr_shared * shared; 190 189 190 struct tr_lock * lock; 191 191 192 tr_handle_status stats[2]; 192 193 int statCur; … … 198 199 }; 199 200 200 #endif 201 void tr_globalLock ( struct tr_handle * ); 202 void tr_globalUnlock ( struct tr_handle * ); 203 int tr_globalIsLocked ( const struct tr_handle * ); 204 205 206 #endif -
trunk/libtransmission/makemeta.c
r3111 r3254 416 416 static tr_lock * lock = NULL; 417 417 418 tr_ sharedLock( h->shared);418 tr_globalLock( h ); 419 419 if( !lock ) 420 420 lock = tr_lockNew( ); 421 tr_ sharedUnlock( h->shared);421 tr_globalUnlock( h ); 422 422 423 423 return lock; -
trunk/libtransmission/peer-mgr.c
r3253 r3254 30 30 #include "ptrarray.h" 31 31 #include "ratecontrol.h" 32 #include "shared.h" 32 33 #include "trevent.h" 33 34 #include "utils.h" 34 35 #include "pthread.h"36 35 37 36 enum … … 107 106 int connectionCount; 108 107 tr_ptrArray * handshakes; /* in-process */ 109 tr_lock * lock;110 pthread_t lockThread;111 108 }; 112 109 … … 118 115 managerLock( struct tr_peerMgr * manager ) 119 116 { 120 assert( manager->lockThread != pthread_self() ); 121 tr_lockLock( manager->lock ); 122 manager->lockThread = pthread_self(); 117 tr_globalLock( manager->handle ); 123 118 } 124 119 static void 125 120 managerUnlock( struct tr_peerMgr * manager ) 126 121 { 127 assert( manager->lockThread == pthread_self() ); 128 manager->lockThread = 0; 129 tr_lockUnlock( manager->lock ); 122 tr_globalUnlock( manager->handle ); 130 123 } 131 124 static void … … 142 135 torrentIsLocked( const Torrent * t ) 143 136 { 144 return ( t != NULL ) 145 && ( t->manager != NULL ) 146 && ( t->manager->lockThread != 0 ) 147 && ( t->manager->lockThread == pthread_self( ) ); 137 return ( t != NULL ) 138 && ( tr_globalIsLocked( t->manager->handle ) ); 148 139 } 149 140 … … 436 427 m->torrents = tr_ptrArrayNew( ); 437 428 m->handshakes = tr_ptrArrayNew( ); 438 m->lock = tr_lockNew( );439 429 return m; 440 430 } … … 449 439 450 440 managerUnlock( manager ); 451 tr_lockFree( manager->lock );452 441 tr_free( manager ); 453 442 } … … 483 472 uint32_t peerCount; 484 473 uint32_t fastAllowed; 485 uint 32_t random;474 uint8_t random; 486 475 }; 487 476 … … 505 494 506 495 /* otherwise go with our random seed */ 507 return tr_compareUint 32( a->random, b->random );496 return tr_compareUint8( a->random, b->random ); 508 497 } 509 498 … … 557 546 setme->peerCount = 0; 558 547 setme->fastAllowed = 0; 559 setme->random = tr_rand( UINT 32_MAX );548 setme->random = tr_rand( UINT8_MAX ); 560 549 /* FIXME */ 561 550 // setme->fastAllowed = tr_bitfieldHas( t->tor->allowedList, i); … … 820 809 Torrent * t = (Torrent *) vt; 821 810 const tr_peermsgs_event * e = (const tr_peermsgs_event *) vevent; 822 const int needLock = !torrentIsLocked( t ); 823 824 if( needLock ) 825 torrentLock( t ); 811 812 torrentLock( t ); 826 813 827 814 switch( e->eventType ) … … 862 849 } 863 850 864 if( needLock ) 865 torrentUnlock( t ); 851 torrentUnlock( t ); 866 852 } 867 853 … … 965 951 tr_handshake * handshake; 966 952 967 assert( manager->lockThread!=0 );968 953 assert( io != NULL ); 969 954 … … 1092 1077 { 1093 1078 const Torrent * t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); 1094 const int isLocked = torrentIsLocked( t );1095 1079 int i, peerCount; 1096 1080 const tr_peer ** peers; … … 1098 1082 tr_pex * walk; 1099 1083 1100 if( !isLocked ) 1101 torrentLock( (Torrent*)t ); 1084 torrentLock( (Torrent*)t ); 1102 1085 1103 1086 peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); … … 1121 1104 *setme_pex = pex; 1122 1105 1123 if( !isLocked ) 1124 torrentUnlock( (Torrent*)t ); 1106 torrentUnlock( (Torrent*)t ); 1125 1107 1126 1108 return peerCount; -
trunk/libtransmission/platform.c
r3217 r3254 57 57 ***/ 58 58 59 #ifdef __BEOS__ 60 typedef thread_id tr_thread_id; 61 #elif defined(WIN32) 62 typedef DWORD tr_thread_id; 63 #else 64 typedef pthread_t tr_thread_id; 65 #endif 66 67 static tr_thread_id 68 tr_getCurrentThread( void ) 69 { 70 #ifdef __BEOS__ 71 return find_thread( NULL ); 72 #elif defined(WIN32) 73 return GetCurrentThreadId(); 74 #else 75 return pthread_self( ); 76 #endif 77 } 78 79 static int 80 tr_areThreadsEqual( tr_thread_id a, tr_thread_id b ) 81 { 82 #ifdef __BEOS__ 83 return a == b; 84 #elif defined(WIN32) 85 return a == b; 86 #else 87 return pthread_equal( a, b ); 88 #endif 89 } 90 59 91 struct tr_thread 60 92 { … … 73 105 74 106 }; 107 108 int 109 tr_amInThread ( const tr_thread * t ) 110 { 111 return tr_areThreadsEqual( tr_getCurrentThread(), t->thread ); 112 } 75 113 76 114 #ifdef WIN32 … … 123 161 return t; 124 162 } 125 126 int127 tr_amInThread ( const tr_thread * t )128 {129 #ifdef __BEOS__130 return find_thread(NULL) == t->thread;131 #elif defined(WIN32)132 return GetCurrentThreadId() == t->thread_id;133 #else134 return pthread_equal( t->thread, pthread_self( ) );135 #endif136 }137 163 138 164 void … … 164 190 struct tr_lock 165 191 { 192 uint32_t depth; 166 193 #ifdef __BEOS__ 167 194 sem_id lock; 195 thread_id lockThread; 168 196 #elif defined(WIN32) 169 197 CRITICAL_SECTION lock; 198 DWORD lockThread; 170 199 #else 171 200 pthread_mutex_t lock; 201 pthread_t lockThread; 172 202 #endif 173 203 }; … … 217 247 tr_lockLock( tr_lock * l ) 218 248 { 219 #ifdef __BEOS__ 220 acquire_sem( l->lock ); 221 #elif defined(WIN32) 222 EnterCriticalSection( &l->lock ); 223 #else 224 pthread_mutex_lock( &l->lock ); 225 #endif 249 tr_thread_id currentThread = tr_getCurrentThread( ); 250 if( l->lockThread == currentThread ) 251 { 252 ++l->depth; 253 } 254 else 255 { 256 #ifdef __BEOS__ 257 acquire_sem( l->lock ); 258 #elif defined(WIN32) 259 EnterCriticalSection( &l->lock ); 260 #else 261 pthread_mutex_lock( &l->lock ); 262 #endif 263 l->lockThread = tr_getCurrentThread( ); 264 l->depth = 1; 265 } 266 } 267 268 int 269 tr_lockHave( const tr_lock * l ) 270 { 271 return ( l->depth > 0 ) 272 && ( l->lockThread == tr_getCurrentThread() ); 226 273 } 227 274 … … 229 276 tr_lockUnlock( tr_lock * l ) 230 277 { 231 #ifdef __BEOS__ 232 release_sem( l->lock ); 233 #elif defined(WIN32) 234 LeaveCriticalSection( &l->lock ); 235 #else 236 pthread_mutex_unlock( &l->lock ); 237 #endif 278 assert( tr_lockHave( l ) ); 279 280 if( !--l->depth ) 281 { 282 l->lockThread = 0; 283 #ifdef __BEOS__ 284 release_sem( l->lock ); 285 #elif defined(WIN32) 286 LeaveCriticalSection( &l->lock ); 287 #else 288 pthread_mutex_unlock( &l->lock ); 289 #endif 290 } 238 291 } 239 292 -
trunk/libtransmission/platform.h
r3107 r3254 42 42 void tr_lockLock ( tr_lock * ); 43 43 void tr_lockUnlock ( tr_lock * ); 44 int tr_lockHave ( const tr_lock * ); 44 45 45 46 tr_cond * tr_condNew ( void ); -
trunk/libtransmission/shared.c
r3171 r3254 45 45 { 46 46 tr_handle * h; 47 tr_lock * lock;48 47 tr_timer * pulseTimer; 49 48 … … 76 75 77 76 s->h = h; 78 s->lock = tr_lockNew( );79 77 s->publicPort = -1; 80 78 s->bindPort = -1; … … 97 95 98 96 tr_netClose( s->bindSocket ); 99 tr_lockFree( s->lock );100 97 tr_natpmpClose( s->natpmp ); 101 98 tr_upnpClose( s->upnp ); 102 99 free( s ); 103 }104 105 /**106 ***107 **/108 109 void tr_sharedLock( tr_shared * s )110 {111 tr_lockLock( s->lock );112 }113 void tr_sharedUnlock( tr_shared * s )114 {115 tr_lockUnlock( s->lock );116 100 } 117 101 … … 129 113 #endif 130 114 131 tr_ sharedLock( s);115 tr_globalLock( s->h ); 132 116 133 117 if( port == s->bindPort ) 134 118 { 135 tr_ sharedUnlock( s);119 tr_globalUnlock( s->h ); 136 120 return; 137 121 } … … 166 150 } 167 151 168 tr_ sharedUnlock( s);152 tr_globalUnlock( s->h ); 169 153 } 170 154 … … 238 222 tr_shared * s = vs; 239 223 240 tr_ sharedLock( s);224 tr_globalLock( s->h ); 241 225 242 226 /* NAT-PMP and UPnP pulses */ … … 250 234 AcceptPeers( s ); 251 235 252 tr_ sharedUnlock( s);236 tr_globalUnlock( s->h ); 253 237 254 238 return TRUE; -
trunk/libtransmission/shared.h
r3121 r3254 40 40 41 41 /*********************************************************************** 42 * tr_sharedLock, tr_sharedUnlock43 ***********************************************************************44 * Gets / releases exclusive access to ressources used by the shared45 * thread46 **********************************************************************/47 void tr_sharedLock ( tr_shared * );48 void tr_sharedUnlock ( tr_shared * );49 50 /***********************************************************************51 42 * tr_sharedSetPort 52 43 *********************************************************************** -
trunk/libtransmission/torrent.c
r3233 r3254 91 91 tr_torrentLock( const tr_torrent * tor ) 92 92 { 93 tr_ lockLock ( (tr_lock*)tor->lock);93 tr_globalLock( tor->handle ); 94 94 } 95 95 … … 97 97 tr_torrentUnlock( const tr_torrent * tor ) 98 98 { 99 tr_ lockUnlock ( (tr_lock*)tor->lock);99 tr_globalUnlock( tor->handle ); 100 100 } 101 101 … … 300 300 tor->info.flags |= flags; 301 301 302 tr_ sharedLock( h->shared);302 tr_globalLock( h ); 303 303 304 304 tor->destination = tr_strdup( destination ); … … 356 356 tr_torrentInitFilePieces( tor ); 357 357 358 tor->lock = tr_lockNew( );359 360 358 tor->upload = tr_rcInit(); 361 359 tor->download = tr_rcInit(); … … 365 363 info->hash, SHA_DIGEST_LENGTH, 366 364 NULL ); 367 368 tr_sharedUnlock( h->shared );369 365 370 366 tr_peerMgrAddTorrent( h->peerMgr, tor ); … … 408 404 tor->trackerSubscription = tr_trackerSubscribe( tor->tracker, onTrackerResponse, tor ); 409 405 410 tr_sharedLock( h->shared );411 406 tor->next = h->torrentList; 412 407 h->torrentList = tor; 413 408 h->torrentCount++; 414 tr_sharedUnlock( h->shared ); 409 410 tr_globalUnlock( h ); 415 411 416 412 tr_ioRecheckAdd( tor, recheckDoneCB, tor->runStatus ); … … 1000 996 assert( tor->runStatus == TR_RUN_STOPPED ); 1001 997 1002 tr_ sharedLock( h->shared);998 tr_globalLock( h ); 1003 999 1004 1000 tr_peerMgrRemoveTorrent( h->peerMgr, tor->info.hash ); 1005 1001 1006 tr_lockFree( tor->lock );1007 1002 tr_cpClose( tor->completion ); 1008 1003 … … 1035 1030 tr_free( tor ); 1036 1031 1037 tr_ sharedUnlock( h->shared);1032 tr_globalUnlock( h ); 1038 1033 } 1039 1034 -
trunk/libtransmission/transmission.c
r3217 r3254 122 122 return NULL; 123 123 124 h->lock = tr_lockNew( ); 125 124 126 h->encryptionMode = TR_ENCRYPTION_PREFERRED; 125 127 … … 152 154 153 155 return h; 156 } 157 158 /*** 159 **** 160 ***/ 161 162 void 163 tr_globalLock( struct tr_handle * handle ) 164 { 165 tr_lockLock( handle->lock ); 166 } 167 168 void 169 tr_globalUnlock( struct tr_handle * handle ) 170 { 171 tr_lockUnlock( handle->lock ); 172 } 173 174 int 175 tr_globalIsLocked( const struct tr_handle * handle ) 176 { 177 return tr_lockHave( handle->lock ); 154 178 } 155 179 … … 174 198 void tr_natTraversalEnable( tr_handle * h, int enable ) 175 199 { 176 tr_ sharedLock( h->shared);200 tr_globalLock( h ); 177 201 tr_sharedTraversalEnable( h->shared, enable ); 178 tr_ sharedUnlock( h->shared);202 tr_globalUnlock( h ); 179 203 } 180 204 … … 186 210 s = &h->stats[h->statCur]; 187 211 188 tr_ sharedLock( h->shared);212 tr_globalLock( h ); 189 213 190 214 s->natTraversalStatus = tr_sharedTraversalStatus( h->shared ); 191 215 s->publicPort = tr_sharedGetPublicPort( h->shared ); 192 216 193 tr_ sharedUnlock( h->shared);217 tr_globalUnlock( h ); 194 218 195 219 return s; … … 242 266 *dl = 0.0; 243 267 *ul = 0.0; 244 tr_ sharedLock( h->shared);268 tr_globalLock( h ); 245 269 for( tor = h->torrentList; tor; tor = tor->next ) 246 270 { … … 251 275 tr_torrentUnlock( tor ); 252 276 } 253 tr_ sharedUnlock( h->shared);277 tr_globalUnlock( h ); 254 278 } 255 279 … … 301 325 } 302 326 327 tr_lockFree( h->lock ); 303 328 free( h->tag ); 304 329 free( h );
Note: See TracChangeset
for help on using the changeset viewer.