mirror of
https://github.com/transmission/transmission
synced 2024-12-24 16:52:39 +00:00
building up our threading tools: implement RW locks
This commit is contained in:
parent
18cd986789
commit
6011450518
2 changed files with 153 additions and 9 deletions
|
@ -212,12 +212,13 @@ static void ThreadFunc( void * _t )
|
|||
tr_dbg( "Thread '%s' exited", t->name );
|
||||
}
|
||||
|
||||
void tr_threadCreate( tr_thread_t * t, void (*func)(void *), void * arg,
|
||||
char * name )
|
||||
void tr_threadCreate( tr_thread_t * t,
|
||||
void (*func)(void *), void * arg,
|
||||
const char * name )
|
||||
{
|
||||
t->func = func;
|
||||
t->arg = arg;
|
||||
t->name = strdup( name );
|
||||
t->name = tr_strdup( name );
|
||||
#ifdef SYS_BEOS
|
||||
t->thread = spawn_thread( (void *) ThreadFunc, name,
|
||||
B_NORMAL_PRIORITY, t );
|
||||
|
@ -231,7 +232,7 @@ const tr_thread_t THREAD_EMPTY = { NULL, NULL, NULL, 0 };
|
|||
|
||||
void tr_threadJoin( tr_thread_t * t )
|
||||
{
|
||||
if (t->func != NULL)
|
||||
if( t->func != NULL )
|
||||
{
|
||||
#ifdef SYS_BEOS
|
||||
long exit;
|
||||
|
@ -264,6 +265,15 @@ void tr_lockClose( tr_lock_t * l )
|
|||
#endif
|
||||
}
|
||||
|
||||
int tr_lockTryLock( tr_lock_t * l )
|
||||
{
|
||||
#ifdef SYS_BEOS
|
||||
#error how is this done in beos
|
||||
#else
|
||||
return pthread_mutex_trylock( l );
|
||||
#endif
|
||||
}
|
||||
|
||||
void tr_lockLock( tr_lock_t * l )
|
||||
{
|
||||
#ifdef SYS_BEOS
|
||||
|
@ -323,6 +333,14 @@ void tr_condSignal( tr_cond_t * c )
|
|||
pthread_cond_signal( c );
|
||||
#endif
|
||||
}
|
||||
void tr_condBroadcast( tr_cond_t * c )
|
||||
{
|
||||
#ifdef SYS_BEOS
|
||||
#error how is this done in beos
|
||||
#else
|
||||
pthread_cond_broadcast( c );
|
||||
#endif
|
||||
}
|
||||
|
||||
void tr_condClose( tr_cond_t * c )
|
||||
{
|
||||
|
@ -702,3 +720,100 @@ tr_getDefaultRoute( struct in_addr * addr UNUSED )
|
|||
}
|
||||
|
||||
#endif
|
||||
|
||||
/***
|
||||
****
|
||||
***/
|
||||
|
||||
static void
|
||||
tr_rwSignal( tr_rwlock_t * rw )
|
||||
{
|
||||
if ( rw->wantToWrite )
|
||||
tr_condSignal( &rw->writeCond );
|
||||
else if ( rw->wantToRead )
|
||||
tr_condBroadcast( &rw->readCond );
|
||||
}
|
||||
|
||||
void
|
||||
tr_rwInit ( tr_rwlock_t * rw )
|
||||
{
|
||||
memset( rw, 0, sizeof(tr_rwlock_t) );
|
||||
tr_lockInit( &rw->lock );
|
||||
tr_condInit( &rw->readCond );
|
||||
tr_condInit( &rw->writeCond );
|
||||
}
|
||||
|
||||
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_rwClose( tr_rwlock_t * rw )
|
||||
{
|
||||
tr_condClose( &rw->writeCond );
|
||||
tr_condClose( &rw->readCond );
|
||||
tr_lockClose( &rw->lock );
|
||||
}
|
||||
|
|
|
@ -55,17 +55,46 @@ const char * tr_getTorrentsDirectory( void );
|
|||
const tr_thread_t THREAD_EMPTY;
|
||||
|
||||
void tr_threadCreate ( tr_thread_t *, void (*func)(void *),
|
||||
void * arg, char * name );
|
||||
void * arg, const char * name );
|
||||
void tr_threadJoin ( tr_thread_t * );
|
||||
void tr_lockInit ( tr_lock_t * );
|
||||
void tr_lockClose ( tr_lock_t * );
|
||||
int tr_lockTryLock ( tr_lock_t * );
|
||||
void tr_lockLock ( tr_lock_t * );
|
||||
void tr_lockUnlock ( tr_lock_t * );
|
||||
|
||||
void tr_condInit ( tr_cond_t * );
|
||||
void tr_condWait ( tr_cond_t *, tr_lock_t * );
|
||||
void tr_condSignal ( tr_cond_t * );
|
||||
void tr_condClose ( tr_cond_t * );
|
||||
void tr_condInit ( tr_cond_t * );
|
||||
void tr_condSignal ( tr_cond_t * );
|
||||
void tr_condBroadcast ( tr_cond_t * );
|
||||
void tr_condClose ( tr_cond_t * );
|
||||
void tr_condWait ( tr_cond_t *, tr_lock_t * );
|
||||
|
||||
/***
|
||||
**** RW lock:
|
||||
**** The lock can be had by one writer or any number of readers.
|
||||
***/
|
||||
|
||||
typedef 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;
|
||||
}
|
||||
tr_rwlock_t;
|
||||
|
||||
void tr_rwInit ( tr_rwlock_t * );
|
||||
void tr_rwClose ( tr_rwlock_t * );
|
||||
void tr_rwReaderLock ( tr_rwlock_t * );
|
||||
int tr_rwReaderTrylock ( tr_rwlock_t * );
|
||||
void tr_rwReaderUnlock ( tr_rwlock_t * );
|
||||
void tr_rwWriterLock ( tr_rwlock_t * );
|
||||
int tr_rwWriterTrylock ( tr_rwlock_t * );
|
||||
void tr_rwWriterUnlock ( tr_rwlock_t * );
|
||||
|
||||
|
||||
struct in_addr; /* forward declaration to calm gcc down */
|
||||
int
|
||||
|
|
Loading…
Reference in a new issue