mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
Added ReadWriteLock::tryEnterRead()
This commit is contained in:
parent
3edae5b4cf
commit
7d019ad5a1
2 changed files with 35 additions and 20 deletions
|
|
@ -39,34 +39,37 @@ ReadWriteLock::~ReadWriteLock() noexcept
|
|||
|
||||
//==============================================================================
|
||||
void ReadWriteLock::enterRead() const noexcept
|
||||
{
|
||||
while (! tryEnterRead())
|
||||
waitEvent.wait (100);
|
||||
}
|
||||
|
||||
bool ReadWriteLock::tryEnterRead() const noexcept
|
||||
{
|
||||
const Thread::ThreadID threadId = Thread::getCurrentThreadId();
|
||||
|
||||
const SpinLock::ScopedLockType sl (accessLock);
|
||||
|
||||
for (;;)
|
||||
for (int i = 0; i < readerThreads.size(); ++i)
|
||||
{
|
||||
for (int i = 0; i < readerThreads.size(); ++i)
|
||||
ThreadRecursionCount& trc = readerThreads.getReference(i);
|
||||
|
||||
if (trc.threadID == threadId)
|
||||
{
|
||||
ThreadRecursionCount& trc = readerThreads.getReference(i);
|
||||
|
||||
if (trc.threadID == threadId)
|
||||
{
|
||||
trc.count++;
|
||||
return;
|
||||
}
|
||||
trc.count++;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (numWriters + numWaitingWriters == 0
|
||||
|| (threadId == writerThreadId && numWriters > 0))
|
||||
{
|
||||
ThreadRecursionCount trc = { threadId, 1 };
|
||||
readerThreads.add (trc);
|
||||
return;
|
||||
}
|
||||
|
||||
const SpinLock::ScopedUnlockType ul (accessLock);
|
||||
waitEvent.wait (100);
|
||||
}
|
||||
|
||||
if (numWriters + numWaitingWriters == 0
|
||||
|| (threadId == writerThreadId && numWriters > 0))
|
||||
{
|
||||
ThreadRecursionCount trc = { threadId, 1 };
|
||||
readerThreads.add (trc);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ReadWriteLock::exitRead() const noexcept
|
||||
|
|
|
|||
|
|
@ -79,6 +79,17 @@ public:
|
|||
*/
|
||||
void enterRead() const noexcept;
|
||||
|
||||
/** Tries to lock this object for reading.
|
||||
|
||||
Multiple threads can simulaneously lock the object for reading, but if another
|
||||
thread has it locked for writing, then this will block until it releases the
|
||||
lock.
|
||||
|
||||
@returns true if the lock is successfully gained.
|
||||
@see exitRead, ScopedReadLock
|
||||
*/
|
||||
bool tryEnterRead() const noexcept;
|
||||
|
||||
/** Releases the read-lock.
|
||||
|
||||
If the caller thread hasn't got the lock, this can have unpredictable results.
|
||||
|
|
@ -106,6 +117,7 @@ public:
|
|||
This is like enterWrite(), but doesn't block - it returns true if it manages
|
||||
to obtain the lock.
|
||||
|
||||
@returns true if the lock is successfully gained.
|
||||
@see enterWrite
|
||||
*/
|
||||
bool tryEnterWrite() const noexcept;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue