Changeset 3254 for trunk/libtransmission/platform.c
- Timestamp:
- Oct 1, 2007, 3:17:15 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note: See TracChangeset
for help on using the changeset viewer.