rpcs3/Utilities/SSemaphore.cpp

68 lines
877 B
C++
Raw Normal View History

#include "stdafx.h"
#include "Utilities/SSemaphore.h"
bool SSemaphore::wait(u64 timeout)
{
std::unique_lock<std::mutex> lock(m_cv_mutex);
u64 counter = 0;
while (true)
{
if (Emu.IsStopped())
{
return false;
}
if (timeout && counter >= timeout)
{
return false;
}
m_cond.wait_for(lock, std::chrono::milliseconds(1));
counter++;
std::lock_guard<std::mutex> lock(m_mutex);
if (m_count)
{
m_count--;
return true;
}
}
}
bool SSemaphore::try_wait()
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_count)
{
m_count--;
return true;
}
else
{
return false;
}
}
2014-06-21 14:26:37 +00:00
void SSemaphore::post()
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_count >= m_max)
{
2014-06-21 14:26:37 +00:00
return;
}
2014-06-21 14:26:37 +00:00
m_count++;
m_cond.notify_one();
}
bool SSemaphore::post_and_wait()
{
2014-06-21 14:26:37 +00:00
// TODO: ???
if (try_wait()) return false;
post();
wait();
return true;
}