Small fixes
This commit is contained in:
+23
-13
@@ -1,29 +1,38 @@
|
||||
#include "stdafx.h"
|
||||
#include "Utilities/SSemaphore.h"
|
||||
|
||||
bool SSemaphore::wait(u64 timeout)
|
||||
void SSemaphore::wait()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_cv_mutex);
|
||||
u32 order;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
order = m_in_order++;
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> cv_lock(m_cv_mutex);
|
||||
|
||||
u64 counter = 0;
|
||||
while (true)
|
||||
{
|
||||
if (Emu.IsStopped())
|
||||
{
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
if (timeout && counter >= timeout)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
m_cond.wait_for(lock, std::chrono::milliseconds(1));
|
||||
counter++;
|
||||
|
||||
m_cond.wait_for(cv_lock, std::chrono::milliseconds(1));
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
if (m_count)
|
||||
{
|
||||
m_count--;
|
||||
return true;
|
||||
if (m_out_order == order)
|
||||
{
|
||||
m_count--;
|
||||
m_out_order++;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_cond.notify_one();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,7 +41,8 @@ bool SSemaphore::try_wait()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
|
||||
if (m_count)
|
||||
// TODO: ordering?
|
||||
if (m_count && m_in_order == m_out_order)
|
||||
{
|
||||
m_count--;
|
||||
return true;
|
||||
|
||||
@@ -4,6 +4,8 @@ class SSemaphore
|
||||
{
|
||||
const u32 m_max;
|
||||
u32 m_count;
|
||||
u32 m_in_order;
|
||||
u32 m_out_order;
|
||||
std::mutex m_mutex, m_cv_mutex;
|
||||
std::condition_variable m_cond;
|
||||
|
||||
@@ -11,12 +13,16 @@ public:
|
||||
SSemaphore(u32 value, u32 max = 1)
|
||||
: m_max(max > 0 ? max : 0xffffffff)
|
||||
, m_count(value > m_max ? m_max : value)
|
||||
, m_in_order(0)
|
||||
, m_out_order(0)
|
||||
{
|
||||
}
|
||||
|
||||
SSemaphore()
|
||||
: m_max(0xffffffff)
|
||||
, m_count(0)
|
||||
, m_in_order(0)
|
||||
, m_out_order(0)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -24,7 +30,7 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
bool wait(u64 timeout = 0);
|
||||
void wait();
|
||||
|
||||
bool try_wait();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user