2013-05-03 02:25:50 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
|
|
#include <Poco/Mutex.h>
|
|
|
|
|
#include <Poco/Semaphore.h>
|
|
|
|
|
|
2013-05-03 10:20:53 +00:00
|
|
|
|
#include <DB/Core/Types.h>
|
|
|
|
|
|
2013-05-03 02:25:50 +00:00
|
|
|
|
|
|
|
|
|
/** Очень простая thread-safe очередь ограниченной длины.
|
|
|
|
|
* Если пытаться вынуть элемент из пустой очереди, то поток блокируется, пока очередь не станет непустой.
|
|
|
|
|
* Если пытаться вставить элемент в переполненную очередь, то поток блокируется, пока в очереди не появится элемент.
|
|
|
|
|
*/
|
|
|
|
|
template <typename T>
|
|
|
|
|
class ConcurrentBoundedQueue
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
size_t max_fill;
|
|
|
|
|
std::queue<T> queue;
|
2016-05-28 10:15:36 +00:00
|
|
|
|
Poco::FastMutex mutex;
|
2013-05-03 02:25:50 +00:00
|
|
|
|
Poco::Semaphore fill_count;
|
|
|
|
|
Poco::Semaphore empty_count;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ConcurrentBoundedQueue(size_t max_fill)
|
|
|
|
|
: fill_count(0, max_fill), empty_count(max_fill, max_fill) {}
|
|
|
|
|
|
|
|
|
|
void push(const T & x)
|
|
|
|
|
{
|
|
|
|
|
empty_count.wait();
|
|
|
|
|
{
|
2016-05-28 10:15:36 +00:00
|
|
|
|
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
|
2013-05-03 02:25:50 +00:00
|
|
|
|
queue.push(x);
|
|
|
|
|
}
|
|
|
|
|
fill_count.set();
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-22 19:57:40 +00:00
|
|
|
|
template <class ... Args>
|
|
|
|
|
void emplace(Args && ... args)
|
|
|
|
|
{
|
|
|
|
|
empty_count.wait();
|
|
|
|
|
{
|
|
|
|
|
Poco::ScopedLock<Poco::Mutex> lock(mutex);
|
|
|
|
|
queue.emplace(std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
fill_count.set();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 02:25:50 +00:00
|
|
|
|
void pop(T & x)
|
|
|
|
|
{
|
|
|
|
|
fill_count.wait();
|
|
|
|
|
{
|
2016-05-28 10:15:36 +00:00
|
|
|
|
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
|
2016-04-25 15:17:23 +00:00
|
|
|
|
x = std::move(queue.front());
|
2013-05-03 02:25:50 +00:00
|
|
|
|
queue.pop();
|
|
|
|
|
}
|
|
|
|
|
empty_count.set();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 10:20:53 +00:00
|
|
|
|
bool tryPush(const T & x, DB::UInt64 milliseconds = 0)
|
2013-05-03 02:25:50 +00:00
|
|
|
|
{
|
2013-05-03 10:20:53 +00:00
|
|
|
|
if (empty_count.tryWait(milliseconds))
|
2013-05-03 02:25:50 +00:00
|
|
|
|
{
|
|
|
|
|
{
|
2016-05-28 10:15:36 +00:00
|
|
|
|
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
|
2013-05-03 02:25:50 +00:00
|
|
|
|
queue.push(x);
|
|
|
|
|
}
|
|
|
|
|
fill_count.set();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 15:17:23 +00:00
|
|
|
|
template <class ... Args>
|
2016-05-17 13:56:06 +00:00
|
|
|
|
bool tryEmplace(DB::UInt64 milliseconds, Args && ... args)
|
2016-04-25 15:17:23 +00:00
|
|
|
|
{
|
|
|
|
|
if (empty_count.tryWait(milliseconds))
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
Poco::ScopedLock<Poco::Mutex> lock(mutex);
|
|
|
|
|
queue.emplace(std::forward<Args>(args)...);
|
|
|
|
|
}
|
|
|
|
|
fill_count.set();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 10:20:53 +00:00
|
|
|
|
bool tryPop(T & x, DB::UInt64 milliseconds = 0)
|
2013-05-03 02:25:50 +00:00
|
|
|
|
{
|
2013-05-03 10:20:53 +00:00
|
|
|
|
if (fill_count.tryWait(milliseconds))
|
2013-05-03 02:25:50 +00:00
|
|
|
|
{
|
|
|
|
|
{
|
2016-05-28 10:15:36 +00:00
|
|
|
|
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
|
2016-04-25 15:17:23 +00:00
|
|
|
|
x = std::move(queue.front());
|
2013-05-03 02:25:50 +00:00
|
|
|
|
queue.pop();
|
|
|
|
|
}
|
|
|
|
|
empty_count.set();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-25 10:46:25 +00:00
|
|
|
|
size_t size()
|
|
|
|
|
{
|
2016-05-28 10:15:36 +00:00
|
|
|
|
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
|
2013-11-25 10:46:25 +00:00
|
|
|
|
return queue.size();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-03 02:25:50 +00:00
|
|
|
|
void clear()
|
|
|
|
|
{
|
|
|
|
|
while (fill_count.tryWait(0))
|
|
|
|
|
{
|
|
|
|
|
{
|
2016-05-28 10:15:36 +00:00
|
|
|
|
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
|
2013-05-03 02:25:50 +00:00
|
|
|
|
queue.pop();
|
|
|
|
|
}
|
|
|
|
|
empty_count.set();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|