Skip to content
Snippets Groups Projects
Commit a2671c17 authored by Rye Mutt's avatar Rye Mutt :bread:
Browse files

Add lock and unlock functions to LLMutexLock and LLMutexTryLock

parent 60c410b4
No related branches found
No related tags found
No related merge requests found
......@@ -82,24 +82,37 @@ class LLMutexLock
{
public:
LLMutexLock(LLMutex* mutex)
: mMutex(mutex)
, mLocked(false)
{
mMutex = mutex;
if(mMutex)
mMutex->lock();
lock();
}
~LLMutexLock()
{
unlock();
}
void lock()
{
if (mMutex && !mLocked)
{
mMutex->lock();
mLocked = true;
}
}
void unlock()
{
if (mMutex)
if (mMutex && mLocked)
{
mMutex->unlock();
mMutex = nullptr;
mLocked = false;
}
}
private:
LLMutex* mMutex;
bool mLocked;
};
//============================================================================
......@@ -127,27 +140,48 @@ class LLMutexTrylock
: mMutex(mutex),
mLocked(false)
{
if (!mMutex)
return;
lock(aTries, delay_ms);
}
for (U32 i = 0; i < aTries; ++i)
~LLMutexTrylock()
{
unlock();
}
bool isLocked() const
{
return mLocked;
}
void lock()
{
if (mMutex && !mLocked)
{
mLocked = mMutex->trylock();
if (mLocked)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
}
}
~LLMutexTrylock()
void lock(U32 aTries, U32 delay_ms)
{
if (mMutex && mLocked)
mMutex->unlock();
if (mMutex && !mLocked)
{
for (U32 i = 0; i < aTries; ++i)
{
mLocked = mMutex->trylock();
if (mLocked)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
}
}
}
bool isLocked() const
void unlock()
{
return mLocked;
if (mMutex && mLocked)
{
mMutex->unlock();
mLocked = false;
}
}
private:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment