mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
2.3 KiB
126 lines
2.3 KiB
11 years ago
|
#ifndef QUEUE_H__
|
||
|
#define QUEUE_H__
|
||
|
|
||
|
#include <queue>
|
||
10 years ago
|
#include <vector>
|
||
11 years ago
|
#include <mutex>
|
||
|
#include <thread>
|
||
|
#include <condition_variable>
|
||
11 years ago
|
#include <functional>
|
||
8 years ago
|
#include <utility>
|
||
11 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace util
|
||
|
{
|
||
|
template<typename Element>
|
||
|
class Queue
|
||
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
void Put (Element e)
|
||
11 years ago
|
{
|
||
|
std::unique_lock<std::mutex> l(m_QueueMutex);
|
||
8 years ago
|
m_Queue.push (std::move(e));
|
||
11 years ago
|
m_NonEmpty.notify_one ();
|
||
|
}
|
||
|
|
||
8 years ago
|
template<template<typename, typename...>class Container, typename... R>
|
||
|
void Put (const Container<Element, R...>& vec)
|
||
10 years ago
|
{
|
||
|
if (!vec.empty ())
|
||
|
{
|
||
|
std::unique_lock<std::mutex> l(m_QueueMutex);
|
||
8 years ago
|
for (const auto& it: vec)
|
||
10 years ago
|
m_Queue.push (it);
|
||
|
m_NonEmpty.notify_one ();
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
Element GetNext ()
|
||
11 years ago
|
{
|
||
|
std::unique_lock<std::mutex> l(m_QueueMutex);
|
||
10 years ago
|
auto el = GetNonThreadSafe ();
|
||
11 years ago
|
if (!el)
|
||
|
{
|
||
|
m_NonEmpty.wait (l);
|
||
|
el = GetNonThreadSafe ();
|
||
|
}
|
||
|
return el;
|
||
|
}
|
||
|
|
||
10 years ago
|
Element GetNextWithTimeout (int usec)
|
||
11 years ago
|
{
|
||
|
std::unique_lock<std::mutex> l(m_QueueMutex);
|
||
10 years ago
|
auto el = GetNonThreadSafe ();
|
||
11 years ago
|
if (!el)
|
||
|
{
|
||
|
m_NonEmpty.wait_for (l, std::chrono::milliseconds (usec));
|
||
|
el = GetNonThreadSafe ();
|
||
|
}
|
||
|
return el;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
void Wait ()
|
||
|
{
|
||
|
std::unique_lock<std::mutex> l(m_QueueMutex);
|
||
|
m_NonEmpty.wait (l);
|
||
|
}
|
||
|
|
||
11 years ago
|
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 ();
|
||
|
}
|
||
10 years ago
|
|
||
|
int GetSize ()
|
||
|
{
|
||
|
std::unique_lock<std::mutex> l(m_QueueMutex);
|
||
|
return m_Queue.size ();
|
||
|
}
|
||
|
|
||
11 years ago
|
void WakeUp () { m_NonEmpty.notify_all (); };
|
||
11 years ago
|
|
||
10 years ago
|
Element Get ()
|
||
11 years ago
|
{
|
||
|
std::unique_lock<std::mutex> l(m_QueueMutex);
|
||
|
return GetNonThreadSafe ();
|
||
|
}
|
||
|
|
||
10 years ago
|
Element Peek ()
|
||
11 years ago
|
{
|
||
|
std::unique_lock<std::mutex> l(m_QueueMutex);
|
||
|
return GetNonThreadSafe (true);
|
||
|
}
|
||
|
|
||
11 years ago
|
private:
|
||
|
|
||
10 years ago
|
Element GetNonThreadSafe (bool peek = false)
|
||
11 years ago
|
{
|
||
|
if (!m_Queue.empty ())
|
||
|
{
|
||
10 years ago
|
auto el = m_Queue.front ();
|
||
11 years ago
|
if (!peek)
|
||
|
m_Queue.pop ();
|
||
11 years ago
|
return el;
|
||
|
}
|
||
|
return nullptr;
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
|
||
10 years ago
|
std::queue<Element> m_Queue;
|
||
11 years ago
|
std::mutex m_QueueMutex;
|
||
|
std::condition_variable m_NonEmpty;
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|