Ignore:
Timestamp:
Jul 31, 2007, 7:56:40 PM (14 years ago)
Author:
charles
Message:

add Win32 implementations of tr_thread_t, tr_mutex_t, and tr_cond_t

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/platform.c

    r2576 r2577  
    5959#ifdef SYS_BEOS
    6060    thread_id        thread;
     61#elif defined(WIN32)
     62    HANDLE           thread;
    6163#else
    6264    pthread_t        thread;
     
    6567};
    6668
    67 static void
     69#ifdef WIN32
     70#define ThreadFuncReturnType unsigned WINAPI
     71#else
     72#define ThreadFuncReturnType void
     73#endif
     74
     75static ThreadFuncReturnType
    6876ThreadFunc( void * _t )
    6977{
     
    8088    t->func( t->arg );
    8189    tr_dbg( "Thread '%s' exited", name );
     90
    8291    tr_free( name );
     92
     93#ifdef WIN32
     94    _endthreadex( 0 );
     95    return 0;
     96#endif
    8397}
    8498
     
    96110    t->thread = spawn_thread( (void*)ThreadFunc, name, B_NORMAL_PRIORITY, t );
    97111    resume_thread( t->thread );
     112#elif defined(WIN32)
     113    t->thread = (HANDLE) _beginthreadex( NULL, 0, &ThreadFunc, NULL, 0, NULL );
    98114#else
    99115    pthread_create( &t->thread, NULL, (void * (*) (void *)) ThreadFunc, t );
     
    111127        long exit;
    112128        wait_for_thread( t->thread, &exit );
     129#elif defined(WIN32)
     130        WaitForSingleObject(thread->handle, INFINITE);
     131        CloseHandle( t->thread );
    113132#else
    114133        pthread_join( t->thread, NULL );
     
    131150#ifdef SYS_BEOS
    132151    sem_id lock;
     152#elif defined(WIN32)
     153    CRITICAL_SECTION lock;
    133154#else
    134155    pthread_mutex_t lock;
     
    143164#ifdef SYS_BEOS
    144165    l->lock = create_sem( 1, "" );
     166#elif defined(WIN32)
     167    InitializeCriticalSection( &l->lock );
    145168#else
    146169    pthread_mutex_init( &l->lock, NULL );
     
    155178#ifdef SYS_BEOS
    156179    delete_sem( l->lock );
     180#elif defined(WIN32)
     181    DeleteCriticalSection( &l->lock );
    157182#else
    158183    pthread_mutex_destroy( &l->lock );
     
    162187
    163188int
    164 tr_lockTryLock( tr_lock_t * l )
     189tr_lockTryLock( tr_lock_t * l ) /* success on zero! */
    165190{
    166191#ifdef SYS_BEOS
    167192    return acquire_sem_etc( l->lock, 1, B_RELATIVE_TIMEOUT, 0 );
    168 #else
    169     /* success on zero! */
     193#elif defined(WIN32)
     194    return !TryEnterCriticalSection( &l->lock );
     195#else
    170196    return pthread_mutex_trylock( &l->lock );
    171197#endif
     
    177203#ifdef SYS_BEOS
    178204    acquire_sem( l->lock );
     205#elif defined(WIN32)
     206    EnterCriticalSection( &l->lock );
    179207#else
    180208    pthread_mutex_lock( &l->lock );
     
    187215#ifdef SYS_BEOS
    188216    release_sem( l->lock );
     217#elif defined(WIN32)
     218    DeleteCriticalSection( &l->lock );
    189219#else
    190220    pthread_mutex_unlock( &l->lock );
     
    312342    thread_id theads[BEOS_MAX_THREADS];
    313343    int start, end;
     344#elif defined(WIN32)
     345    CONDITION_VARIABLE cond;
    314346#else
    315347    pthread_cond_t cond;
     
    325357    c->start = 0;
    326358    c->end = 0;
     359#elif defined(WIN32)
     360    InitializeConditionVariable( &c->cond );
    327361#else
    328362    pthread_cond_init( &c->cond, NULL );
     
    344378    suspend_thread( find_thread( NULL ) ); /* Wait for signal */
    345379    acquire_sem( *l );
     380#elif defined(WIN32)
     381    SleepConditionVariableCS( &c->cond, &l->lock, INFINITE );
    346382#else
    347383    pthread_cond_wait( &c->cond, &l->lock );
     
    379415    condTrySignal( c );
    380416    release_sem( c->sem );
     417#elif defined(WIN32)
     418    WakeConditionVariable( &c->cond );
    381419#else
    382420    pthread_cond_signal( &c->cond );
     
    389427    while( !condTrySignal( c ) );
    390428    release_sem( c->sem );
     429#elif defined(WIN32)
     430    WakeAllConditionVariable( &c->cond );
    391431#else
    392432    pthread_cond_broadcast( &c->cond );
     
    399439#ifdef SYS_BEOS
    400440    delete_sem( c->sem );
     441#elif defined(WIN32)
     442    /* a no-op, apparently */
    401443#else
    402444    pthread_cond_destroy( &c->cond );
Note: See TracChangeset for help on using the changeset viewer.