2014-02-24 00:00:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
2014-10-17 22:20:03 +00:00
|
|
|
static const volatile bool sq_no_wait = true;
|
2014-08-23 14:51:51 +00:00
|
|
|
|
2014-02-24 00:00:42 +00:00
|
|
|
template<typename T, u32 SQSize = 666>
|
|
|
|
class SQueue
|
|
|
|
{
|
2014-06-20 11:00:36 +00:00
|
|
|
std::mutex m_mutex;
|
2014-02-24 00:00:42 +00:00
|
|
|
u32 m_pos;
|
|
|
|
u32 m_count;
|
|
|
|
T m_data[SQSize];
|
|
|
|
|
|
|
|
public:
|
|
|
|
SQueue()
|
|
|
|
: m_pos(0)
|
|
|
|
, m_count(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-31 10:04:34 +00:00
|
|
|
const u32 GetSize() const
|
|
|
|
{
|
|
|
|
return SQSize;
|
|
|
|
}
|
|
|
|
|
2014-10-17 22:20:03 +00:00
|
|
|
bool Push(const T& data, const volatile bool* do_exit)
|
2014-02-24 00:00:42 +00:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (m_count >= SQSize)
|
|
|
|
{
|
2014-10-17 22:20:03 +00:00
|
|
|
if (Emu.IsStopped() || do_exit && *do_exit)
|
2014-03-02 23:02:42 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-20 19:57:28 +00:00
|
|
|
|
2014-10-16 16:29:41 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
2014-02-24 00:00:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-06-20 11:00:36 +00:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2014-02-24 00:00:42 +00:00
|
|
|
|
|
|
|
if (m_count >= SQSize) continue;
|
|
|
|
|
|
|
|
m_data[(m_pos + m_count++) % SQSize] = data;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 22:20:03 +00:00
|
|
|
bool Pop(T& data, const volatile bool* do_exit)
|
2014-02-24 00:00:42 +00:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (!m_count)
|
|
|
|
{
|
2014-10-17 22:20:03 +00:00
|
|
|
if (Emu.IsStopped() || do_exit && *do_exit)
|
2014-03-02 23:02:42 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-20 19:57:28 +00:00
|
|
|
|
2014-10-16 16:29:41 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
2014-02-24 00:00:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-06-20 11:00:36 +00:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2014-02-24 00:00:42 +00:00
|
|
|
|
|
|
|
if (!m_count) continue;
|
|
|
|
|
|
|
|
data = m_data[m_pos];
|
|
|
|
m_pos = (m_pos + 1) % SQSize;
|
|
|
|
m_count--;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-29 03:21:57 +00:00
|
|
|
u32 GetCount()
|
2014-02-24 00:00:42 +00:00
|
|
|
{
|
2014-06-29 03:21:57 +00:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2014-02-24 00:00:42 +00:00
|
|
|
return m_count;
|
|
|
|
}
|
|
|
|
|
2014-06-29 03:21:57 +00:00
|
|
|
u32 GetCountUnsafe()
|
2014-02-24 00:00:42 +00:00
|
|
|
{
|
2014-06-29 03:21:57 +00:00
|
|
|
return m_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2014-02-24 00:00:42 +00:00
|
|
|
return !m_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
2014-06-20 11:00:36 +00:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2014-02-24 00:00:42 +00:00
|
|
|
m_count = 0;
|
|
|
|
}
|
2014-03-02 23:02:42 +00:00
|
|
|
|
2014-10-17 22:20:03 +00:00
|
|
|
T& Peek(const volatile bool* do_exit, u32 pos = 0)
|
2014-03-02 23:02:42 +00:00
|
|
|
{
|
2014-03-22 01:08:25 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2014-06-29 03:21:57 +00:00
|
|
|
if (m_count <= pos)
|
2014-03-22 01:08:25 +00:00
|
|
|
{
|
2014-10-17 22:20:03 +00:00
|
|
|
if (Emu.IsStopped() || do_exit && *do_exit)
|
2014-03-22 01:08:25 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-06-20 19:57:28 +00:00
|
|
|
|
2014-10-16 16:29:41 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
2014-03-22 01:08:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-06-20 11:00:36 +00:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2014-06-29 03:21:57 +00:00
|
|
|
if (m_count > pos)
|
2014-06-20 19:57:28 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 01:08:25 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-02 23:02:42 +00:00
|
|
|
return m_data[(m_pos + pos) % SQSize];
|
|
|
|
}
|
2014-03-06 11:50:45 +00:00
|
|
|
};
|