mirror of
https://github.com/transmission/transmission
synced 2025-03-12 07:03:44 +00:00
add Win32 implementations of tr_thread_t, tr_mutex_t, and tr_cond_t
This commit is contained in:
parent
ba3dc008fc
commit
497856e6e4
1 changed files with 45 additions and 3 deletions
|
@ -58,13 +58,21 @@ struct tr_thread_s
|
||||||
|
|
||||||
#ifdef SYS_BEOS
|
#ifdef SYS_BEOS
|
||||||
thread_id thread;
|
thread_id thread;
|
||||||
|
#elif defined(WIN32)
|
||||||
|
HANDLE thread;
|
||||||
#else
|
#else
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
#ifdef WIN32
|
||||||
|
#define ThreadFuncReturnType unsigned WINAPI
|
||||||
|
#else
|
||||||
|
#define ThreadFuncReturnType void
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static ThreadFuncReturnType
|
||||||
ThreadFunc( void * _t )
|
ThreadFunc( void * _t )
|
||||||
{
|
{
|
||||||
tr_thread_t * t = _t;
|
tr_thread_t * t = _t;
|
||||||
|
@ -79,7 +87,13 @@ ThreadFunc( void * _t )
|
||||||
tr_dbg( "Thread '%s' started", name );
|
tr_dbg( "Thread '%s' started", name );
|
||||||
t->func( t->arg );
|
t->func( t->arg );
|
||||||
tr_dbg( "Thread '%s' exited", name );
|
tr_dbg( "Thread '%s' exited", name );
|
||||||
|
|
||||||
tr_free( name );
|
tr_free( name );
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
_endthreadex( 0 );
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
tr_thread_t *
|
tr_thread_t *
|
||||||
|
@ -95,6 +109,8 @@ tr_threadNew( void (*func)(void *),
|
||||||
#ifdef SYS_BEOS
|
#ifdef SYS_BEOS
|
||||||
t->thread = spawn_thread( (void*)ThreadFunc, name, B_NORMAL_PRIORITY, t );
|
t->thread = spawn_thread( (void*)ThreadFunc, name, B_NORMAL_PRIORITY, t );
|
||||||
resume_thread( t->thread );
|
resume_thread( t->thread );
|
||||||
|
#elif defined(WIN32)
|
||||||
|
t->thread = (HANDLE) _beginthreadex( NULL, 0, &ThreadFunc, NULL, 0, NULL );
|
||||||
#else
|
#else
|
||||||
pthread_create( &t->thread, NULL, (void * (*) (void *)) ThreadFunc, t );
|
pthread_create( &t->thread, NULL, (void * (*) (void *)) ThreadFunc, t );
|
||||||
#endif
|
#endif
|
||||||
|
@ -110,6 +126,9 @@ tr_threadJoin( tr_thread_t * t )
|
||||||
#ifdef SYS_BEOS
|
#ifdef SYS_BEOS
|
||||||
long exit;
|
long exit;
|
||||||
wait_for_thread( t->thread, &exit );
|
wait_for_thread( t->thread, &exit );
|
||||||
|
#elif defined(WIN32)
|
||||||
|
WaitForSingleObject(thread->handle, INFINITE);
|
||||||
|
CloseHandle( t->thread );
|
||||||
#else
|
#else
|
||||||
pthread_join( t->thread, NULL );
|
pthread_join( t->thread, NULL );
|
||||||
#endif
|
#endif
|
||||||
|
@ -130,6 +149,8 @@ struct tr_lock_s
|
||||||
{
|
{
|
||||||
#ifdef SYS_BEOS
|
#ifdef SYS_BEOS
|
||||||
sem_id lock;
|
sem_id lock;
|
||||||
|
#elif defined(WIN32)
|
||||||
|
CRITICAL_SECTION lock;
|
||||||
#else
|
#else
|
||||||
pthread_mutex_t lock;
|
pthread_mutex_t lock;
|
||||||
#endif
|
#endif
|
||||||
|
@ -142,6 +163,8 @@ tr_lockNew( void )
|
||||||
|
|
||||||
#ifdef SYS_BEOS
|
#ifdef SYS_BEOS
|
||||||
l->lock = create_sem( 1, "" );
|
l->lock = create_sem( 1, "" );
|
||||||
|
#elif defined(WIN32)
|
||||||
|
InitializeCriticalSection( &l->lock );
|
||||||
#else
|
#else
|
||||||
pthread_mutex_init( &l->lock, NULL );
|
pthread_mutex_init( &l->lock, NULL );
|
||||||
#endif
|
#endif
|
||||||
|
@ -154,6 +177,8 @@ tr_lockFree( tr_lock_t * l )
|
||||||
{
|
{
|
||||||
#ifdef SYS_BEOS
|
#ifdef SYS_BEOS
|
||||||
delete_sem( l->lock );
|
delete_sem( l->lock );
|
||||||
|
#elif defined(WIN32)
|
||||||
|
DeleteCriticalSection( &l->lock );
|
||||||
#else
|
#else
|
||||||
pthread_mutex_destroy( &l->lock );
|
pthread_mutex_destroy( &l->lock );
|
||||||
#endif
|
#endif
|
||||||
|
@ -161,12 +186,13 @@ tr_lockFree( tr_lock_t * l )
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
tr_lockTryLock( tr_lock_t * l )
|
tr_lockTryLock( tr_lock_t * l ) /* success on zero! */
|
||||||
{
|
{
|
||||||
#ifdef SYS_BEOS
|
#ifdef SYS_BEOS
|
||||||
return acquire_sem_etc( l->lock, 1, B_RELATIVE_TIMEOUT, 0 );
|
return acquire_sem_etc( l->lock, 1, B_RELATIVE_TIMEOUT, 0 );
|
||||||
|
#elif defined(WIN32)
|
||||||
|
return !TryEnterCriticalSection( &l->lock );
|
||||||
#else
|
#else
|
||||||
/* success on zero! */
|
|
||||||
return pthread_mutex_trylock( &l->lock );
|
return pthread_mutex_trylock( &l->lock );
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -176,6 +202,8 @@ tr_lockLock( tr_lock_t * l )
|
||||||
{
|
{
|
||||||
#ifdef SYS_BEOS
|
#ifdef SYS_BEOS
|
||||||
acquire_sem( l->lock );
|
acquire_sem( l->lock );
|
||||||
|
#elif defined(WIN32)
|
||||||
|
EnterCriticalSection( &l->lock );
|
||||||
#else
|
#else
|
||||||
pthread_mutex_lock( &l->lock );
|
pthread_mutex_lock( &l->lock );
|
||||||
#endif
|
#endif
|
||||||
|
@ -186,6 +214,8 @@ tr_lockUnlock( tr_lock_t * l )
|
||||||
{
|
{
|
||||||
#ifdef SYS_BEOS
|
#ifdef SYS_BEOS
|
||||||
release_sem( l->lock );
|
release_sem( l->lock );
|
||||||
|
#elif defined(WIN32)
|
||||||
|
DeleteCriticalSection( &l->lock );
|
||||||
#else
|
#else
|
||||||
pthread_mutex_unlock( &l->lock );
|
pthread_mutex_unlock( &l->lock );
|
||||||
#endif
|
#endif
|
||||||
|
@ -311,6 +341,8 @@ struct tr_cond_s
|
||||||
sem_id sem;
|
sem_id sem;
|
||||||
thread_id theads[BEOS_MAX_THREADS];
|
thread_id theads[BEOS_MAX_THREADS];
|
||||||
int start, end;
|
int start, end;
|
||||||
|
#elif defined(WIN32)
|
||||||
|
CONDITION_VARIABLE cond;
|
||||||
#else
|
#else
|
||||||
pthread_cond_t cond;
|
pthread_cond_t cond;
|
||||||
#endif
|
#endif
|
||||||
|
@ -324,6 +356,8 @@ tr_condNew( void )
|
||||||
c->sem = create_sem( 1, "" );
|
c->sem = create_sem( 1, "" );
|
||||||
c->start = 0;
|
c->start = 0;
|
||||||
c->end = 0;
|
c->end = 0;
|
||||||
|
#elif defined(WIN32)
|
||||||
|
InitializeConditionVariable( &c->cond );
|
||||||
#else
|
#else
|
||||||
pthread_cond_init( &c->cond, NULL );
|
pthread_cond_init( &c->cond, NULL );
|
||||||
#endif
|
#endif
|
||||||
|
@ -343,6 +377,8 @@ void tr_condWait( tr_cond_t * c, tr_lock_t * l )
|
||||||
release_sem( *l );
|
release_sem( *l );
|
||||||
suspend_thread( find_thread( NULL ) ); /* Wait for signal */
|
suspend_thread( find_thread( NULL ) ); /* Wait for signal */
|
||||||
acquire_sem( *l );
|
acquire_sem( *l );
|
||||||
|
#elif defined(WIN32)
|
||||||
|
SleepConditionVariableCS( &c->cond, &l->lock, INFINITE );
|
||||||
#else
|
#else
|
||||||
pthread_cond_wait( &c->cond, &l->lock );
|
pthread_cond_wait( &c->cond, &l->lock );
|
||||||
#endif
|
#endif
|
||||||
|
@ -378,6 +414,8 @@ void tr_condSignal( tr_cond_t * c )
|
||||||
acquire_sem( c->sem );
|
acquire_sem( c->sem );
|
||||||
condTrySignal( c );
|
condTrySignal( c );
|
||||||
release_sem( c->sem );
|
release_sem( c->sem );
|
||||||
|
#elif defined(WIN32)
|
||||||
|
WakeConditionVariable( &c->cond );
|
||||||
#else
|
#else
|
||||||
pthread_cond_signal( &c->cond );
|
pthread_cond_signal( &c->cond );
|
||||||
#endif
|
#endif
|
||||||
|
@ -388,6 +426,8 @@ void tr_condBroadcast( tr_cond_t * c )
|
||||||
acquire_sem( c->sem );
|
acquire_sem( c->sem );
|
||||||
while( !condTrySignal( c ) );
|
while( !condTrySignal( c ) );
|
||||||
release_sem( c->sem );
|
release_sem( c->sem );
|
||||||
|
#elif defined(WIN32)
|
||||||
|
WakeAllConditionVariable( &c->cond );
|
||||||
#else
|
#else
|
||||||
pthread_cond_broadcast( &c->cond );
|
pthread_cond_broadcast( &c->cond );
|
||||||
#endif
|
#endif
|
||||||
|
@ -398,6 +438,8 @@ tr_condFree( tr_cond_t * c )
|
||||||
{
|
{
|
||||||
#ifdef SYS_BEOS
|
#ifdef SYS_BEOS
|
||||||
delete_sem( c->sem );
|
delete_sem( c->sem );
|
||||||
|
#elif defined(WIN32)
|
||||||
|
/* a no-op, apparently */
|
||||||
#else
|
#else
|
||||||
pthread_cond_destroy( &c->cond );
|
pthread_cond_destroy( &c->cond );
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue