Ignore:
Timestamp:
Sep 20, 2007, 4:32:01 PM (13 years ago)
Author:
livings124
Message:

merge encryption branch to trunk (xcode project is still out of date)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/platform.c

    r2975 r3105  
    5757***/
    5858
    59 struct tr_thread_s
     59struct tr_thread
    6060{
    6161    void          (* func ) ( void * );
    6262    void           * arg;
    63     char           * name;
     63    const char     * name;
    6464
    6565#ifdef __BEOS__
     
    8383ThreadFunc( void * _t )
    8484{
    85     tr_thread_t * t = _t;
    86     char* name = tr_strdup( t->name );
     85    tr_thread * t = _t;
     86    const char * name = t->name;
    8787
    8888#ifdef __BEOS__
     
    9696    tr_dbg( "Thread '%s' exited", name );
    9797
    98     tr_free( name );
    99 
    10098#ifdef WIN32
    10199    _endthreadex( 0 );
     
    104102}
    105103
    106 tr_thread_t *
     104tr_thread *
    107105tr_threadNew( void (*func)(void *),
    108106              void * arg,
    109107              const char * name )
    110108{
    111     tr_thread_t * t = tr_new0( tr_thread_t, 1 );
     109    tr_thread * t = tr_new0( tr_thread, 1 );
    112110    t->func = func;
    113111    t->arg  = arg;
    114     t->name = tr_strdup( name );
     112    t->name = name;
    115113
    116114#ifdef __BEOS__
     
    127125
    128126int
    129 tr_amInThread ( const tr_thread_t * t )
    130 {
    131     int ret;
    132 #ifdef __BEOS__
    133     ret = find_thread(NULL) == t->thread;
    134 #elif defined(WIN32)
    135     ret = GetCurrentThreadId() == t->thread_id;
    136 #else
    137     ret = pthread_equal( t->thread, pthread_self( ) );
    138 #endif
    139     return ret;
     127tr_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#else
     134    return pthread_equal( t->thread, pthread_self( ) );
     135#endif
    140136}
    141137   
    142138void
    143 tr_threadJoin( tr_thread_t * t )
     139tr_threadJoin( tr_thread * t )
    144140{
    145141    if( t != NULL )
     
    156152
    157153        tr_dbg( "Thread '%s' joined", t->name );
    158         tr_free( t->name );
    159154        t->name = NULL;
    160155        t->func = NULL;
     
    167162***/
    168163
    169 struct tr_lock_s
     164struct tr_lock
    170165{
    171166#ifdef __BEOS__
     
    178173};
    179174
    180 tr_lock_t*
     175tr_lock*
    181176tr_lockNew( void )
    182177{
    183     tr_lock_t * l = tr_new0( tr_lock_t, 1 );
     178    tr_lock * l = tr_new0( tr_lock, 1 );
    184179
    185180#ifdef __BEOS__
     
    195190
    196191void
    197 tr_lockFree( tr_lock_t * l )
     192tr_lockFree( tr_lock * l )
    198193{
    199194#ifdef __BEOS__
     
    208203
    209204int
    210 tr_lockTryLock( tr_lock_t * l ) /* success on zero! */
     205tr_lockTryLock( tr_lock * l ) /* success on zero! */
    211206{
    212207#ifdef __BEOS__
     
    220215
    221216void
    222 tr_lockLock( tr_lock_t * l )
     217tr_lockLock( tr_lock * l )
    223218{
    224219#ifdef __BEOS__
     
    232227
    233228void
    234 tr_lockUnlock( tr_lock_t * l )
     229tr_lockUnlock( tr_lock * l )
    235230{
    236231#ifdef __BEOS__
     
    241236    pthread_mutex_unlock( &l->lock );
    242237#endif
    243 }
    244 
    245 /***
    246 ****  RW LOCK
    247 ***/
    248 
    249 struct tr_rwlock_s
    250 {
    251     tr_lock_t * lock;
    252     tr_cond_t * readCond;
    253     tr_cond_t * writeCond;
    254     size_t readCount;
    255     size_t wantToRead;
    256     size_t wantToWrite;
    257     int haveWriter;
    258 };
    259 
    260 static void
    261 tr_rwSignal( tr_rwlock_t * rw )
    262 {
    263   if ( rw->wantToWrite )
    264     tr_condSignal( rw->writeCond );
    265   else if ( rw->wantToRead )
    266     tr_condBroadcast( rw->readCond );
    267 }
    268 
    269 tr_rwlock_t*
    270 tr_rwNew ( void )
    271 {
    272     tr_rwlock_t * rw = tr_new0( tr_rwlock_t, 1 );
    273     rw->lock = tr_lockNew( );
    274     rw->readCond = tr_condNew( );
    275     rw->writeCond = tr_condNew( );
    276     return rw;
    277 }
    278 
    279 void
    280 tr_rwReaderLock( tr_rwlock_t * rw )
    281 {
    282     tr_lockLock( rw->lock );
    283     rw->wantToRead++;
    284     while( rw->haveWriter || rw->wantToWrite )
    285         tr_condWait( rw->readCond, rw->lock );
    286     rw->wantToRead--;
    287     rw->readCount++;
    288     tr_lockUnlock( rw->lock );
    289 }
    290 
    291 int
    292 tr_rwReaderTrylock( tr_rwlock_t * rw )
    293 {
    294     int ret = FALSE;
    295     tr_lockLock( rw->lock );
    296     if ( !rw->haveWriter && !rw->wantToWrite ) {
    297         rw->readCount++;
    298         ret = TRUE;
    299     }
    300     tr_lockUnlock( rw->lock );
    301     return ret;
    302 
    303 }
    304 
    305 void
    306 tr_rwReaderUnlock( tr_rwlock_t * rw )
    307 {
    308     tr_lockLock( rw->lock );
    309     --rw->readCount;
    310     if( !rw->readCount )
    311         tr_rwSignal( rw );
    312     tr_lockUnlock( rw->lock );
    313 }
    314 
    315 void
    316 tr_rwWriterLock( tr_rwlock_t * rw )
    317 {
    318     tr_lockLock( rw->lock );
    319     rw->wantToWrite++;
    320     while( rw->haveWriter || rw->readCount )
    321         tr_condWait( rw->writeCond, rw->lock );
    322     rw->wantToWrite--;
    323     rw->haveWriter = TRUE;
    324     tr_lockUnlock( rw->lock );
    325 }
    326 
    327 int
    328 tr_rwWriterTrylock( tr_rwlock_t * rw )
    329 {
    330     int ret = FALSE;
    331     tr_lockLock( rw->lock );
    332     if( !rw->haveWriter && !rw->readCount )
    333         ret = rw->haveWriter = TRUE;
    334     tr_lockUnlock( rw->lock );
    335     return ret;
    336 }
    337 void
    338 tr_rwWriterUnlock( tr_rwlock_t * rw )
    339 {
    340     tr_lockLock( rw->lock );
    341     rw->haveWriter = FALSE;
    342     tr_rwSignal( rw );
    343     tr_lockUnlock( rw->lock );
    344 }
    345 
    346 void
    347 tr_rwFree( tr_rwlock_t * rw )
    348 {
    349     tr_condFree( rw->writeCond );
    350     tr_condFree( rw->readCond );
    351     tr_lockFree( rw->lock );
    352     tr_free( rw );
    353238}
    354239
     
    357242***/
    358243
    359 struct tr_cond_s
     244struct tr_cond
    360245{
    361246#ifdef __BEOS__
     
    365250#elif defined(WIN32)
    366251    tr_list * events;
    367     tr_lock_t * lock;
     252    tr_lock * lock;
    368253#else
    369254    pthread_cond_t cond;
     
    384269#endif
    385270
    386 tr_cond_t*
     271tr_cond*
    387272tr_condNew( void )
    388273{
    389     tr_cond_t * c = tr_new0( tr_cond_t, 1 );
     274    tr_cond * c = tr_new0( tr_cond, 1 );
    390275#ifdef __BEOS__
    391276    c->sem = create_sem( 1, "" );
     
    402287
    403288void
    404 tr_condWait( tr_cond_t * c, tr_lock_t * l )
     289tr_condWait( tr_cond * c, tr_lock * l )
    405290{
    406291#ifdef __BEOS__
     
    450335
    451336#ifdef __BEOS__
    452 static int condTrySignal( tr_cond_t * c )
     337static int condTrySignal( tr_cond * c )
    453338{
    454339    if( c->start == c->end )
     
    474359#endif
    475360void
    476 tr_condSignal( tr_cond_t * c )
     361tr_condSignal( tr_cond * c )
    477362{
    478363#ifdef __BEOS__
     
    497382
    498383void
    499 tr_condBroadcast( tr_cond_t * c )
     384tr_condBroadcast( tr_cond * c )
    500385{
    501386#ifdef __BEOS__
     
    521406
    522407void
    523 tr_condFree( tr_cond_t * c )
     408tr_condFree( tr_cond * c )
    524409{
    525410#ifdef __BEOS__
Note: See TracChangeset for help on using the changeset viewer.