mirror of
https://github.com/transmission/transmission.git
synced 2026-05-08 09:39:08 +01:00
merge encryption branch to trunk (xcode project is still out of date)
This commit is contained in:
+28
-143
@@ -56,11 +56,11 @@
|
||||
**** THREADS
|
||||
***/
|
||||
|
||||
struct tr_thread_s
|
||||
struct tr_thread
|
||||
{
|
||||
void (* func ) ( void * );
|
||||
void * arg;
|
||||
char * name;
|
||||
const char * name;
|
||||
|
||||
#ifdef __BEOS__
|
||||
thread_id thread;
|
||||
@@ -82,8 +82,8 @@ struct tr_thread_s
|
||||
static ThreadFuncReturnType
|
||||
ThreadFunc( void * _t )
|
||||
{
|
||||
tr_thread_t * t = _t;
|
||||
char* name = tr_strdup( t->name );
|
||||
tr_thread * t = _t;
|
||||
const char * name = t->name;
|
||||
|
||||
#ifdef __BEOS__
|
||||
/* This is required because on BeOS, SIGINT is sent to each thread,
|
||||
@@ -95,23 +95,21 @@ ThreadFunc( void * _t )
|
||||
t->func( t->arg );
|
||||
tr_dbg( "Thread '%s' exited", name );
|
||||
|
||||
tr_free( name );
|
||||
|
||||
#ifdef WIN32
|
||||
_endthreadex( 0 );
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
tr_thread_t *
|
||||
tr_thread *
|
||||
tr_threadNew( void (*func)(void *),
|
||||
void * arg,
|
||||
const char * name )
|
||||
{
|
||||
tr_thread_t * t = tr_new0( tr_thread_t, 1 );
|
||||
tr_thread * t = tr_new0( tr_thread, 1 );
|
||||
t->func = func;
|
||||
t->arg = arg;
|
||||
t->name = tr_strdup( name );
|
||||
t->name = name;
|
||||
|
||||
#ifdef __BEOS__
|
||||
t->thread = spawn_thread( (void*)ThreadFunc, name, B_NORMAL_PRIORITY, t );
|
||||
@@ -126,21 +124,19 @@ tr_threadNew( void (*func)(void *),
|
||||
}
|
||||
|
||||
int
|
||||
tr_amInThread ( const tr_thread_t * t )
|
||||
tr_amInThread ( const tr_thread * t )
|
||||
{
|
||||
int ret;
|
||||
#ifdef __BEOS__
|
||||
ret = find_thread(NULL) == t->thread;
|
||||
return find_thread(NULL) == t->thread;
|
||||
#elif defined(WIN32)
|
||||
ret = GetCurrentThreadId() == t->thread_id;
|
||||
return GetCurrentThreadId() == t->thread_id;
|
||||
#else
|
||||
ret = pthread_equal( t->thread, pthread_self( ) );
|
||||
return pthread_equal( t->thread, pthread_self( ) );
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
tr_threadJoin( tr_thread_t * t )
|
||||
tr_threadJoin( tr_thread * t )
|
||||
{
|
||||
if( t != NULL )
|
||||
{
|
||||
@@ -155,7 +151,6 @@ tr_threadJoin( tr_thread_t * t )
|
||||
#endif
|
||||
|
||||
tr_dbg( "Thread '%s' joined", t->name );
|
||||
tr_free( t->name );
|
||||
t->name = NULL;
|
||||
t->func = NULL;
|
||||
tr_free( t );
|
||||
@@ -166,7 +161,7 @@ tr_threadJoin( tr_thread_t * t )
|
||||
**** LOCKS
|
||||
***/
|
||||
|
||||
struct tr_lock_s
|
||||
struct tr_lock
|
||||
{
|
||||
#ifdef __BEOS__
|
||||
sem_id lock;
|
||||
@@ -177,10 +172,10 @@ struct tr_lock_s
|
||||
#endif
|
||||
};
|
||||
|
||||
tr_lock_t*
|
||||
tr_lock*
|
||||
tr_lockNew( void )
|
||||
{
|
||||
tr_lock_t * l = tr_new0( tr_lock_t, 1 );
|
||||
tr_lock * l = tr_new0( tr_lock, 1 );
|
||||
|
||||
#ifdef __BEOS__
|
||||
l->lock = create_sem( 1, "" );
|
||||
@@ -194,7 +189,7 @@ tr_lockNew( void )
|
||||
}
|
||||
|
||||
void
|
||||
tr_lockFree( tr_lock_t * l )
|
||||
tr_lockFree( tr_lock * l )
|
||||
{
|
||||
#ifdef __BEOS__
|
||||
delete_sem( l->lock );
|
||||
@@ -207,7 +202,7 @@ tr_lockFree( tr_lock_t * l )
|
||||
}
|
||||
|
||||
int
|
||||
tr_lockTryLock( tr_lock_t * l ) /* success on zero! */
|
||||
tr_lockTryLock( tr_lock * l ) /* success on zero! */
|
||||
{
|
||||
#ifdef __BEOS__
|
||||
return acquire_sem_etc( l->lock, 1, B_RELATIVE_TIMEOUT, 0 );
|
||||
@@ -219,7 +214,7 @@ tr_lockTryLock( tr_lock_t * l ) /* success on zero! */
|
||||
}
|
||||
|
||||
void
|
||||
tr_lockLock( tr_lock_t * l )
|
||||
tr_lockLock( tr_lock * l )
|
||||
{
|
||||
#ifdef __BEOS__
|
||||
acquire_sem( l->lock );
|
||||
@@ -231,7 +226,7 @@ tr_lockLock( tr_lock_t * l )
|
||||
}
|
||||
|
||||
void
|
||||
tr_lockUnlock( tr_lock_t * l )
|
||||
tr_lockUnlock( tr_lock * l )
|
||||
{
|
||||
#ifdef __BEOS__
|
||||
release_sem( l->lock );
|
||||
@@ -242,121 +237,11 @@ tr_lockUnlock( tr_lock_t * l )
|
||||
#endif
|
||||
}
|
||||
|
||||
/***
|
||||
**** RW LOCK
|
||||
***/
|
||||
|
||||
struct tr_rwlock_s
|
||||
{
|
||||
tr_lock_t * lock;
|
||||
tr_cond_t * readCond;
|
||||
tr_cond_t * writeCond;
|
||||
size_t readCount;
|
||||
size_t wantToRead;
|
||||
size_t wantToWrite;
|
||||
int haveWriter;
|
||||
};
|
||||
|
||||
static void
|
||||
tr_rwSignal( tr_rwlock_t * rw )
|
||||
{
|
||||
if ( rw->wantToWrite )
|
||||
tr_condSignal( rw->writeCond );
|
||||
else if ( rw->wantToRead )
|
||||
tr_condBroadcast( rw->readCond );
|
||||
}
|
||||
|
||||
tr_rwlock_t*
|
||||
tr_rwNew ( void )
|
||||
{
|
||||
tr_rwlock_t * rw = tr_new0( tr_rwlock_t, 1 );
|
||||
rw->lock = tr_lockNew( );
|
||||
rw->readCond = tr_condNew( );
|
||||
rw->writeCond = tr_condNew( );
|
||||
return rw;
|
||||
}
|
||||
|
||||
void
|
||||
tr_rwReaderLock( tr_rwlock_t * rw )
|
||||
{
|
||||
tr_lockLock( rw->lock );
|
||||
rw->wantToRead++;
|
||||
while( rw->haveWriter || rw->wantToWrite )
|
||||
tr_condWait( rw->readCond, rw->lock );
|
||||
rw->wantToRead--;
|
||||
rw->readCount++;
|
||||
tr_lockUnlock( rw->lock );
|
||||
}
|
||||
|
||||
int
|
||||
tr_rwReaderTrylock( tr_rwlock_t * rw )
|
||||
{
|
||||
int ret = FALSE;
|
||||
tr_lockLock( rw->lock );
|
||||
if ( !rw->haveWriter && !rw->wantToWrite ) {
|
||||
rw->readCount++;
|
||||
ret = TRUE;
|
||||
}
|
||||
tr_lockUnlock( rw->lock );
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
tr_rwReaderUnlock( tr_rwlock_t * rw )
|
||||
{
|
||||
tr_lockLock( rw->lock );
|
||||
--rw->readCount;
|
||||
if( !rw->readCount )
|
||||
tr_rwSignal( rw );
|
||||
tr_lockUnlock( rw->lock );
|
||||
}
|
||||
|
||||
void
|
||||
tr_rwWriterLock( tr_rwlock_t * rw )
|
||||
{
|
||||
tr_lockLock( rw->lock );
|
||||
rw->wantToWrite++;
|
||||
while( rw->haveWriter || rw->readCount )
|
||||
tr_condWait( rw->writeCond, rw->lock );
|
||||
rw->wantToWrite--;
|
||||
rw->haveWriter = TRUE;
|
||||
tr_lockUnlock( rw->lock );
|
||||
}
|
||||
|
||||
int
|
||||
tr_rwWriterTrylock( tr_rwlock_t * rw )
|
||||
{
|
||||
int ret = FALSE;
|
||||
tr_lockLock( rw->lock );
|
||||
if( !rw->haveWriter && !rw->readCount )
|
||||
ret = rw->haveWriter = TRUE;
|
||||
tr_lockUnlock( rw->lock );
|
||||
return ret;
|
||||
}
|
||||
void
|
||||
tr_rwWriterUnlock( tr_rwlock_t * rw )
|
||||
{
|
||||
tr_lockLock( rw->lock );
|
||||
rw->haveWriter = FALSE;
|
||||
tr_rwSignal( rw );
|
||||
tr_lockUnlock( rw->lock );
|
||||
}
|
||||
|
||||
void
|
||||
tr_rwFree( tr_rwlock_t * rw )
|
||||
{
|
||||
tr_condFree( rw->writeCond );
|
||||
tr_condFree( rw->readCond );
|
||||
tr_lockFree( rw->lock );
|
||||
tr_free( rw );
|
||||
}
|
||||
|
||||
/***
|
||||
**** COND
|
||||
***/
|
||||
|
||||
struct tr_cond_s
|
||||
struct tr_cond
|
||||
{
|
||||
#ifdef __BEOS__
|
||||
sem_id sem;
|
||||
@@ -364,7 +249,7 @@ struct tr_cond_s
|
||||
int start, end;
|
||||
#elif defined(WIN32)
|
||||
tr_list * events;
|
||||
tr_lock_t * lock;
|
||||
tr_lock * lock;
|
||||
#else
|
||||
pthread_cond_t cond;
|
||||
#endif
|
||||
@@ -383,10 +268,10 @@ static DWORD getContEventTLS( void )
|
||||
}
|
||||
#endif
|
||||
|
||||
tr_cond_t*
|
||||
tr_cond*
|
||||
tr_condNew( void )
|
||||
{
|
||||
tr_cond_t * c = tr_new0( tr_cond_t, 1 );
|
||||
tr_cond * c = tr_new0( tr_cond, 1 );
|
||||
#ifdef __BEOS__
|
||||
c->sem = create_sem( 1, "" );
|
||||
c->start = 0;
|
||||
@@ -401,7 +286,7 @@ tr_condNew( void )
|
||||
}
|
||||
|
||||
void
|
||||
tr_condWait( tr_cond_t * c, tr_lock_t * l )
|
||||
tr_condWait( tr_cond * c, tr_lock * l )
|
||||
{
|
||||
#ifdef __BEOS__
|
||||
|
||||
@@ -449,7 +334,7 @@ tr_condWait( tr_cond_t * c, tr_lock_t * l )
|
||||
}
|
||||
|
||||
#ifdef __BEOS__
|
||||
static int condTrySignal( tr_cond_t * c )
|
||||
static int condTrySignal( tr_cond * c )
|
||||
{
|
||||
if( c->start == c->end )
|
||||
return 1;
|
||||
@@ -473,7 +358,7 @@ static int condTrySignal( tr_cond_t * c )
|
||||
}
|
||||
#endif
|
||||
void
|
||||
tr_condSignal( tr_cond_t * c )
|
||||
tr_condSignal( tr_cond * c )
|
||||
{
|
||||
#ifdef __BEOS__
|
||||
|
||||
@@ -496,7 +381,7 @@ tr_condSignal( tr_cond_t * c )
|
||||
}
|
||||
|
||||
void
|
||||
tr_condBroadcast( tr_cond_t * c )
|
||||
tr_condBroadcast( tr_cond * c )
|
||||
{
|
||||
#ifdef __BEOS__
|
||||
|
||||
@@ -520,7 +405,7 @@ tr_condBroadcast( tr_cond_t * c )
|
||||
}
|
||||
|
||||
void
|
||||
tr_condFree( tr_cond_t * c )
|
||||
tr_condFree( tr_cond * c )
|
||||
{
|
||||
#ifdef __BEOS__
|
||||
delete_sem( c->sem );
|
||||
|
||||
Reference in New Issue
Block a user