Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
added mutex() property to Condition. also made reader() and writer() …
Browse files Browse the repository at this point in the history
…methods of RWMutex properties.
  • Loading branch information
complexmath committed Apr 26, 2012
1 parent 9a57802 commit 7eb147a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
28 changes: 23 additions & 5 deletions src/core/sync/condition.d
Expand Up @@ -88,8 +88,7 @@ class Condition
}
else version( Posix )
{
m_mutexAddr = m.handleAddr();

m_assocMutex = m;
int rc = pthread_cond_init( &m_hndl, null );
if( rc )
throw new SyncException( "Unable to initialize condition" );
Expand All @@ -115,6 +114,23 @@ class Condition
}


////////////////////////////////////////////////////////////////////////////
// General Properties
////////////////////////////////////////////////////////////////////////////


/**
* Gets the mutex associated with this condition.
*
* Returns:
* The mutex associated with this condition.
*/
@property Mutex mutex()
{
return m_assocMutex;
}


////////////////////////////////////////////////////////////////////////////
// General Actions
////////////////////////////////////////////////////////////////////////////
Expand All @@ -134,7 +150,7 @@ class Condition
}
else version( Posix )
{
int rc = pthread_cond_wait( &m_hndl, m_mutexAddr );
int rc = pthread_cond_wait( &m_hndl, m_assocMutex.handleAddr() );
if( rc )
throw new SyncException( "Unable to wait for condition" );
}
Expand Down Expand Up @@ -182,7 +198,9 @@ class Condition
timespec t = void;
mktspec( t, val );

int rc = pthread_cond_timedwait( &m_hndl, m_mutexAddr, &t );
int rc = pthread_cond_timedwait( &m_hndl,
m_assocMutex.handleAddr(),
&t );
if( !rc )
return true;
if( rc == ETIMEDOUT )
Expand Down Expand Up @@ -432,8 +450,8 @@ private:
}
else version( Posix )
{
Mutex m_assocMutex;
pthread_cond_t m_hndl;
pthread_mutex_t* m_mutexAddr;
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/core/sync/rwmutex.d
Expand Up @@ -118,12 +118,12 @@ class ReadWriteMutex


/**
* Gets the policy for the associated mutex.
* Gets the policy used by this mutex.
*
* Returns:
* The policy used by this mutex.
*/
Policy policy()
@property Policy policy()
{
return m_policy;
}
Expand All @@ -140,7 +140,7 @@ class ReadWriteMutex
* Returns:
* A reader sub-mutex.
*/
Reader reader()
@property Reader reader()
{
return m_reader;
}
Expand All @@ -152,7 +152,7 @@ class ReadWriteMutex
* Returns:
* A writer sub-mutex.
*/
Writer writer()
@property Writer writer()
{
return m_writer;
}
Expand Down Expand Up @@ -190,7 +190,7 @@ class ReadWriteMutex
++m_numQueuedReaders;
scope(exit) --m_numQueuedReaders;

while( shouldQueueReader() )
while( shouldQueueReader )
m_readerQueue.wait();
++m_numActiveReaders;
}
Expand Down Expand Up @@ -225,7 +225,7 @@ class ReadWriteMutex
{
synchronized( m_commonMutex )
{
if( shouldQueueReader() )
if( shouldQueueReader )
return false;
++m_numActiveReaders;
return true;
Expand All @@ -234,7 +234,7 @@ class ReadWriteMutex


private:
bool shouldQueueReader()
@property bool shouldQueueReader()
{
if( m_numActiveWriters > 0 )
return true;
Expand Down Expand Up @@ -293,7 +293,7 @@ class ReadWriteMutex
++m_numQueuedWriters;
scope(exit) --m_numQueuedWriters;

while( shouldQueueWriter() )
while( shouldQueueWriter )
m_writerQueue.wait();
++m_numActiveWriters;
}
Expand Down Expand Up @@ -341,7 +341,7 @@ class ReadWriteMutex
{
synchronized( m_commonMutex )
{
if( shouldQueueWriter() )
if( shouldQueueWriter )
return false;
++m_numActiveWriters;
return true;
Expand All @@ -350,7 +350,7 @@ class ReadWriteMutex


private:
bool shouldQueueWriter()
@property bool shouldQueueWriter()
{
if( m_numActiveWriters > 0 ||
m_numActiveReaders > 0 )
Expand Down

0 comments on commit 7eb147a

Please sign in to comment.