2014-06-20 19:56:19 +00:00
|
|
|
#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()
|
2014-06-20 19:56:19 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
|
|
|
|
if (m_count >= m_max)
|
|
|
|
{
|
2014-06-21 14:26:37 +00:00
|
|
|
return;
|
2014-06-20 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-06-21 14:26:37 +00:00
|
|
|
m_count++;
|
|
|
|
m_cond.notify_one();
|
2014-06-20 19:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SSemaphore::post_and_wait()
|
|
|
|
{
|
2014-06-21 14:26:37 +00:00
|
|
|
// TODO: ???
|
2014-06-20 19:56:19 +00:00
|
|
|
if (try_wait()) return false;
|
|
|
|
|
|
|
|
post();
|
|
|
|
wait();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|