ClickHouse/src/Common/ConcurrentBoundedQueue.h

156 lines
3.5 KiB
C++
Raw Normal View History

2013-05-03 02:25:50 +00:00
#pragma once
#include <queue>
#include <type_traits>
2021-08-26 15:00:27 +00:00
#include <atomic>
2013-05-03 02:25:50 +00:00
#include <Poco/Mutex.h>
#include <Poco/Semaphore.h>
2021-10-02 07:13:14 +00:00
#include <base/MoveOrCopyIfThrow.h>
2021-08-26 15:00:27 +00:00
#include <Common/Exception.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
}
}
2013-05-03 02:25:50 +00:00
/** A very simple thread-safe queue of limited size.
2017-05-07 20:25:26 +00:00
* If you try to pop an item from an empty queue, the thread is blocked until the queue becomes nonempty.
* If you try to push an element into an overflowed queue, the thread is blocked until space appears in the queue.
2013-05-03 02:25:50 +00:00
*/
template <typename T>
class ConcurrentBoundedQueue
{
private:
std::queue<T> queue;
2021-08-26 15:00:27 +00:00
mutable Poco::FastMutex mutex;
Poco::Semaphore fill_count;
Poco::Semaphore empty_count;
2021-08-26 15:00:27 +00:00
std::atomic_bool closed = false;
template <typename... Args>
bool tryEmplaceImpl(Args &&... args)
{
bool emplaced = true;
{
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
if (closed)
emplaced = false;
else
queue.emplace(std::forward<Args>(args)...);
}
if (emplaced)
fill_count.set();
else
empty_count.set();
return emplaced;
}
void popImpl(T & x)
{
{
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
detail::moveOrCopyIfThrow(std::move(queue.front()), x);
queue.pop();
}
empty_count.set();
}
2013-05-03 02:25:50 +00:00
public:
2021-02-28 21:04:05 +00:00
explicit ConcurrentBoundedQueue(size_t max_fill)
2021-03-03 19:34:25 +00:00
: fill_count(0, max_fill)
, empty_count(max_fill, max_fill)
{}
void push(const T & x)
{
empty_count.wait();
2021-08-26 15:00:27 +00:00
if (!tryEmplaceImpl(x))
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "tryPush/tryEmplace must be used with close()");
}
2017-06-01 13:41:58 +00:00
template <typename... Args>
void emplace(Args &&... args)
{
empty_count.wait();
2021-08-26 15:00:27 +00:00
if (!tryEmplaceImpl(std::forward<Args>(args)...))
throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "tryPush/tryEmplace must be used with close()");
}
void pop(T & x)
{
fill_count.wait();
2021-08-26 15:00:27 +00:00
popImpl(x);
}
2017-06-01 13:41:58 +00:00
bool tryPush(const T & x, UInt64 milliseconds = 0)
{
2021-08-26 15:00:27 +00:00
if (!empty_count.tryWait(milliseconds))
return false;
return tryEmplaceImpl(x);
}
2017-06-01 13:41:58 +00:00
template <typename... Args>
bool tryEmplace(UInt64 milliseconds, Args &&... args)
{
2021-08-26 15:00:27 +00:00
if (!empty_count.tryWait(milliseconds))
return false;
return tryEmplaceImpl(std::forward<Args>(args)...);
}
2017-06-01 13:41:58 +00:00
bool tryPop(T & x, UInt64 milliseconds = 0)
{
2021-08-26 15:00:27 +00:00
if (!fill_count.tryWait(milliseconds))
return false;
popImpl(x);
return true;
}
2021-08-26 15:00:27 +00:00
size_t size() const
{
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
return queue.size();
}
2021-08-26 15:00:27 +00:00
size_t empty() const
2020-03-09 02:55:28 +00:00
{
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
return queue.empty();
}
2021-08-26 15:00:27 +00:00
/// Forbids to push new elements to queue.
/// Returns false if queue was not closed before call, returns true if queue was already closed.
bool close()
{
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
return closed.exchange(true);
}
bool isClosed() const
{
return closed.load();
}
void clear()
{
while (fill_count.tryWait(0))
{
{
Poco::ScopedLock<Poco::FastMutex> lock(mutex);
queue.pop();
}
empty_count.set();
}
}
2013-05-03 02:25:50 +00:00
};