i2pd/Queue.h

170 lines
3.1 KiB
C
Raw Normal View History

2013-12-10 17:00:13 +04:00
#ifndef QUEUE_H__
#define QUEUE_H__
#include <queue>
2015-01-23 06:00:41 +03:00
#include <vector>
2013-12-10 17:00:13 +04:00
#include <mutex>
#include <thread>
#include <condition_variable>
2014-04-23 20:49:02 +04:00
#include <functional>
2013-12-10 17:00:13 +04:00
namespace i2p
{
namespace util
{
template<typename Element>
class Queue
{
public:
void Put (Element * e)
{
std::unique_lock<std::mutex> l(m_QueueMutex);
m_Queue.push (e);
m_NonEmpty.notify_one ();
}
2015-01-23 06:00:41 +03:00
void Put (const std::vector<Element *>& vec)
{
if (!vec.empty ())
{
std::unique_lock<std::mutex> l(m_QueueMutex);
for (auto it: vec)
m_Queue.push (it);
m_NonEmpty.notify_one ();
}
}
2013-12-10 17:00:13 +04:00
Element * GetNext ()
{
std::unique_lock<std::mutex> l(m_QueueMutex);
Element * el = GetNonThreadSafe ();
if (!el)
{
m_NonEmpty.wait (l);
el = GetNonThreadSafe ();
}
return el;
}
Element * GetNextWithTimeout (int usec)
{
std::unique_lock<std::mutex> l(m_QueueMutex);
Element * el = GetNonThreadSafe ();
if (!el)
{
m_NonEmpty.wait_for (l, std::chrono::milliseconds (usec));
el = GetNonThreadSafe ();
}
return el;
}
2014-01-11 05:21:38 +04:00
2014-04-23 20:17:14 +04:00
void Wait ()
{
std::unique_lock<std::mutex> l(m_QueueMutex);
m_NonEmpty.wait (l);
}
2014-01-11 05:21:38 +04:00
bool Wait (int sec, int usec)
{
std::unique_lock<std::mutex> l(m_QueueMutex);
return m_NonEmpty.wait_for (l, std::chrono::seconds (sec) + std::chrono::milliseconds (usec)) != std::cv_status::timeout;
}
bool IsEmpty ()
{
std::unique_lock<std::mutex> l(m_QueueMutex);
return m_Queue.empty ();
}
2015-02-04 00:45:19 +03:00
int GetSize ()
{
std::unique_lock<std::mutex> l(m_QueueMutex);
return m_Queue.size ();
}
2014-07-02 21:48:45 +04:00
void WakeUp () { m_NonEmpty.notify_all (); };
2013-12-10 17:00:13 +04:00
Element * Get ()
{
std::unique_lock<std::mutex> l(m_QueueMutex);
return GetNonThreadSafe ();
}
2014-01-11 05:21:38 +04:00
Element * Peek ()
{
std::unique_lock<std::mutex> l(m_QueueMutex);
return GetNonThreadSafe (true);
}
2013-12-10 17:00:13 +04:00
private:
2014-01-11 05:21:38 +04:00
Element * GetNonThreadSafe (bool peek = false)
2013-12-10 17:00:13 +04:00
{
if (!m_Queue.empty ())
{
Element * el = m_Queue.front ();
2014-01-11 05:21:38 +04:00
if (!peek)
m_Queue.pop ();
2013-12-10 17:00:13 +04:00
return el;
}
return nullptr;
}
private:
std::queue<Element *> m_Queue;
std::mutex m_QueueMutex;
std::condition_variable m_NonEmpty;
};
template<class Msg>
class MsgQueue: public Queue<Msg>
{
public:
2014-04-23 20:49:02 +04:00
typedef std::function<void()> OnEmpty;
2014-04-23 20:17:14 +04:00
MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue<Msg>::Run, this)) {};
2014-07-02 21:48:45 +04:00
~MsgQueue () { Stop (); };
2014-02-05 03:15:42 +04:00
void Stop()
{
2014-07-02 21:48:45 +04:00
if (m_IsRunning)
{
m_IsRunning = false;
Queue<Msg>::WakeUp ();
m_Thread.join();
}
2014-02-05 03:15:42 +04:00
}
2013-12-10 17:00:13 +04:00
2014-04-23 20:49:02 +04:00
void SetOnEmpty (OnEmpty const & e) { m_OnEmpty = e; };
2013-12-10 17:00:13 +04:00
private:
2014-04-23 20:17:14 +04:00
2013-12-10 17:00:13 +04:00
void Run ()
{
2014-04-23 20:17:14 +04:00
while (m_IsRunning)
2013-12-10 17:00:13 +04:00
{
2014-04-23 20:17:14 +04:00
while (Msg * msg = Queue<Msg>::Get ())
{
msg->Process ();
delete msg;
}
2014-04-23 20:49:02 +04:00
if (m_OnEmpty != nullptr)
m_OnEmpty ();
2014-09-22 22:44:10 +04:00
if (m_IsRunning)
Queue<Msg>::Wait ();
2013-12-10 17:00:13 +04:00
}
}
private:
2014-04-23 20:17:14 +04:00
2014-09-22 22:44:10 +04:00
volatile bool m_IsRunning;
2014-04-23 20:49:02 +04:00
OnEmpty m_OnEmpty;
2014-12-07 20:54:35 +03:00
std::thread m_Thread;
2013-12-10 17:00:13 +04:00
};
}
}
#endif