2014-01-25 17:09:04 +08:00
|
|
|
#pragma once
|
|
|
|
|
2014-01-25 18:50:26 +08:00
|
|
|
// blocking_queue:
|
|
|
|
// A blocking multi-consumer/multi-producer thread safe queue.
|
|
|
|
// Has max capacity and supports timeout on push or pop operations.
|
|
|
|
|
2014-01-25 17:09:04 +08:00
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
|
|
|
#include <queue>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
|
2014-02-22 04:51:54 +08:00
|
|
|
namespace c11log {
|
|
|
|
namespace details {
|
2014-02-09 07:56:21 +08:00
|
|
|
|
2014-01-25 17:09:04 +08:00
|
|
|
template<typename T>
|
2014-02-22 04:51:54 +08:00
|
|
|
class blocking_queue {
|
2014-01-25 17:09:04 +08:00
|
|
|
public:
|
2014-02-22 04:51:54 +08:00
|
|
|
using queue_t = std::queue<T>;
|
|
|
|
using size_type = typename queue_t::size_type;
|
|
|
|
using clock = std::chrono::system_clock;
|
2014-01-25 18:50:26 +08:00
|
|
|
|
2014-02-22 04:51:54 +08:00
|
|
|
explicit blocking_queue(size_type max_size) :
|
2014-02-22 16:34:42 +08:00
|
|
|
_max_size(max_size),
|
|
|
|
_q(),
|
|
|
|
_mutex() {
|
2014-02-22 04:51:54 +08:00
|
|
|
}
|
|
|
|
blocking_queue(const blocking_queue&) = delete;
|
|
|
|
blocking_queue& operator=(const blocking_queue&) = delete;
|
|
|
|
~blocking_queue() = default;
|
2014-01-25 17:09:04 +08:00
|
|
|
|
2014-02-22 04:51:54 +08:00
|
|
|
size_type size() {
|
2014-02-22 16:34:42 +08:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
return _q.size();
|
2014-02-22 04:51:54 +08:00
|
|
|
}
|
2014-01-25 18:50:26 +08:00
|
|
|
|
2014-02-22 04:51:54 +08:00
|
|
|
// Push copy of item into the back of the queue.
|
|
|
|
// If the queue is full, block the calling thread util there is room or timeout have passed.
|
|
|
|
// Return: false on timeout, true on successful push.
|
|
|
|
template<typename Duration_Rep, typename Duration_Period, typename TT>
|
|
|
|
bool push(TT&& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout) {
|
2014-02-22 16:34:42 +08:00
|
|
|
std::unique_lock<std::mutex> ul(_mutex);
|
|
|
|
if (_q.size() >= _max_size) {
|
|
|
|
if (!_item_popped_cond.wait_until(ul, clock::now() + timeout, [this]() {
|
|
|
|
return this->_q.size() < this->_max_size;
|
2014-02-22 04:51:54 +08:00
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-22 16:34:42 +08:00
|
|
|
_q.push(std::forward<TT>(item));
|
|
|
|
if (_q.size() <= 1) {
|
2014-02-22 04:51:54 +08:00
|
|
|
ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly..
|
2014-02-22 16:34:42 +08:00
|
|
|
_item_pushed_cond.notify_one();
|
2014-02-22 04:51:54 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-01-25 18:50:26 +08:00
|
|
|
|
2014-02-22 04:51:54 +08:00
|
|
|
// Push copy of item into the back of the queue.
|
|
|
|
// If the queue is full, block the calling thread until there is room.
|
|
|
|
template<typename TT>
|
|
|
|
void push(TT&& item) {
|
2014-03-01 20:06:58 +08:00
|
|
|
while (!push(std::forward<TT>(item), _one_hour));
|
2014-02-22 04:51:54 +08:00
|
|
|
}
|
2014-01-25 18:50:26 +08:00
|
|
|
|
2014-02-22 04:51:54 +08:00
|
|
|
// Pop a copy of the front item in the queue into the given item ref.
|
|
|
|
// If the queue is empty, block the calling thread util there is item to pop or timeout have passed.
|
|
|
|
// Return: false on timeout , true on successful pop/
|
|
|
|
template<class Duration_Rep, class Duration_Period>
|
|
|
|
bool pop(T& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout) {
|
2014-02-22 16:34:42 +08:00
|
|
|
std::unique_lock<std::mutex> ul(_mutex);
|
|
|
|
if (_q.empty()) {
|
|
|
|
if (!_item_pushed_cond.wait_until(ul, clock::now() + timeout, [this]() {
|
|
|
|
return !this->_q.empty();
|
2014-02-22 04:51:54 +08:00
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-22 16:34:42 +08:00
|
|
|
item = std::move(_q.front());
|
|
|
|
_q.pop();
|
|
|
|
if (_q.size() >= _max_size - 1) {
|
2014-02-22 04:51:54 +08:00
|
|
|
ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly..
|
2014-02-22 16:34:42 +08:00
|
|
|
_item_popped_cond.notify_one();
|
2014-02-22 04:51:54 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-01-25 18:50:26 +08:00
|
|
|
|
2014-02-22 04:51:54 +08:00
|
|
|
// Pop a copy of the front item in the queue into the given item ref.
|
|
|
|
// If the queue is empty, block the calling thread util there is item to pop.
|
|
|
|
void pop(T& item) {
|
2014-03-01 20:06:58 +08:00
|
|
|
while (!pop(item, _one_hour));
|
2014-02-22 04:51:54 +08:00
|
|
|
}
|
2014-01-25 18:50:26 +08:00
|
|
|
|
2014-02-22 04:51:54 +08:00
|
|
|
// Clear the queue
|
|
|
|
void clear() {
|
|
|
|
{
|
2014-02-22 16:34:42 +08:00
|
|
|
std::unique_lock<std::mutex> ul(_mutex);
|
|
|
|
queue_t().swap(_q);
|
2014-02-22 04:51:54 +08:00
|
|
|
}
|
2014-02-22 16:34:42 +08:00
|
|
|
_item_popped_cond.notify_all();
|
2014-02-22 04:51:54 +08:00
|
|
|
}
|
2014-01-25 21:52:10 +08:00
|
|
|
|
2014-01-25 17:09:04 +08:00
|
|
|
private:
|
2014-02-22 16:34:42 +08:00
|
|
|
size_type _max_size;
|
|
|
|
std::queue<T> _q;
|
|
|
|
std::mutex _mutex;
|
|
|
|
std::condition_variable _item_pushed_cond;
|
|
|
|
std::condition_variable _item_popped_cond;
|
2014-03-01 20:06:58 +08:00
|
|
|
static constexpr auto _one_hour = std::chrono::hours(1);
|
2014-01-25 17:09:04 +08:00
|
|
|
};
|
2014-01-29 10:00:05 +08:00
|
|
|
|
|
|
|
}
|
2014-01-25 17:09:04 +08:00
|
|
|
}
|