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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void pop(T & x)
|
|
|
|
|
{
|
|
|
|
|
fill_count.wait();
|
|
|
|
|
{
|
2016-05-28 10:15:36 +00:00
|
|
|
|
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
|
2013-05-03 02:25:50 +00:00
|
|
|
|
x = queue.front();
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
2013-05-03 02:25:50 +00:00
|
|
|
|
x = queue.front();
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|