|
|
|
@ -17,14 +17,14 @@ namespace util
@@ -17,14 +17,14 @@ namespace util
|
|
|
|
|
{ |
|
|
|
|
public: |
|
|
|
|
|
|
|
|
|
void Put (Element * e) |
|
|
|
|
void Put (Element e) |
|
|
|
|
{ |
|
|
|
|
std::unique_lock<std::mutex> l(m_QueueMutex); |
|
|
|
|
m_Queue.push (e); |
|
|
|
|
m_NonEmpty.notify_one (); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Put (const std::vector<Element *>& vec) |
|
|
|
|
void Put (const std::vector<Element>& vec) |
|
|
|
|
{ |
|
|
|
|
if (!vec.empty ()) |
|
|
|
|
{ |
|
|
|
@ -35,10 +35,10 @@ namespace util
@@ -35,10 +35,10 @@ namespace util
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Element * GetNext () |
|
|
|
|
Element GetNext () |
|
|
|
|
{ |
|
|
|
|
std::unique_lock<std::mutex> l(m_QueueMutex); |
|
|
|
|
Element * el = GetNonThreadSafe (); |
|
|
|
|
auto el = GetNonThreadSafe (); |
|
|
|
|
if (!el) |
|
|
|
|
{ |
|
|
|
|
m_NonEmpty.wait (l); |
|
|
|
@ -47,10 +47,10 @@ namespace util
@@ -47,10 +47,10 @@ namespace util
|
|
|
|
|
return el; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Element * GetNextWithTimeout (int usec) |
|
|
|
|
Element GetNextWithTimeout (int usec) |
|
|
|
|
{ |
|
|
|
|
std::unique_lock<std::mutex> l(m_QueueMutex); |
|
|
|
|
Element * el = GetNonThreadSafe (); |
|
|
|
|
auto el = GetNonThreadSafe (); |
|
|
|
|
if (!el) |
|
|
|
|
{ |
|
|
|
|
m_NonEmpty.wait_for (l, std::chrono::milliseconds (usec)); |
|
|
|
@ -85,13 +85,13 @@ namespace util
@@ -85,13 +85,13 @@ namespace util
|
|
|
|
|
|
|
|
|
|
void WakeUp () { m_NonEmpty.notify_all (); }; |
|
|
|
|
|
|
|
|
|
Element * Get () |
|
|
|
|
Element Get () |
|
|
|
|
{ |
|
|
|
|
std::unique_lock<std::mutex> l(m_QueueMutex); |
|
|
|
|
return GetNonThreadSafe (); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Element * Peek () |
|
|
|
|
Element Peek () |
|
|
|
|
{ |
|
|
|
|
std::unique_lock<std::mutex> l(m_QueueMutex); |
|
|
|
|
return GetNonThreadSafe (true); |
|
|
|
@ -99,11 +99,11 @@ namespace util
@@ -99,11 +99,11 @@ namespace util
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
|
|
Element * GetNonThreadSafe (bool peek = false) |
|
|
|
|
Element GetNonThreadSafe (bool peek = false) |
|
|
|
|
{ |
|
|
|
|
if (!m_Queue.empty ()) |
|
|
|
|
{ |
|
|
|
|
Element * el = m_Queue.front (); |
|
|
|
|
auto el = m_Queue.front (); |
|
|
|
|
if (!peek) |
|
|
|
|
m_Queue.pop (); |
|
|
|
|
return el; |
|
|
|
@ -113,13 +113,13 @@ namespace util
@@ -113,13 +113,13 @@ namespace util
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
|
|
std::queue<Element *> m_Queue; |
|
|
|
|
std::queue<Element> m_Queue; |
|
|
|
|
std::mutex m_QueueMutex; |
|
|
|
|
std::condition_variable m_NonEmpty; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
template<class Msg> |
|
|
|
|
class MsgQueue: public Queue<Msg> |
|
|
|
|
class MsgQueue: public Queue<Msg *> |
|
|
|
|
{ |
|
|
|
|
public: |
|
|
|
|
|
|
|
|
@ -132,7 +132,7 @@ namespace util
@@ -132,7 +132,7 @@ namespace util
|
|
|
|
|
if (m_IsRunning) |
|
|
|
|
{ |
|
|
|
|
m_IsRunning = false; |
|
|
|
|
Queue<Msg>::WakeUp (); |
|
|
|
|
Queue<Msg *>::WakeUp (); |
|
|
|
|
m_Thread.join(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -145,7 +145,7 @@ namespace util
@@ -145,7 +145,7 @@ namespace util
|
|
|
|
|
{ |
|
|
|
|
while (m_IsRunning) |
|
|
|
|
{ |
|
|
|
|
while (Msg * msg = Queue<Msg>::Get ()) |
|
|
|
|
while (auto msg = Queue<Msg *>::Get ()) |
|
|
|
|
{ |
|
|
|
|
msg->Process (); |
|
|
|
|
delete msg; |
|
|
|
@ -153,7 +153,7 @@ namespace util
@@ -153,7 +153,7 @@ namespace util
|
|
|
|
|
if (m_OnEmpty != nullptr) |
|
|
|
|
m_OnEmpty (); |
|
|
|
|
if (m_IsRunning) |
|
|
|
|
Queue<Msg>::Wait (); |
|
|
|
|
Queue<Msg *>::Wait (); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|