spdlog/include/c11log/details/blocking_queue.h

116 lines
3.6 KiB
C
Raw Normal View History

2014-01-25 04:09:04 -05:00
#pragma once
2014-01-25 05:50:26 -05: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 04:09:04 -05:00
#include <cstddef>
#include <chrono>
#include <memory>
#include <queue>
#include <mutex>
#include <condition_variable>
namespace c11log {
2014-01-25 05:50:26 -05:00
namespace details
{
2014-01-28 21:00:05 -05:00
2014-01-25 04:09:04 -05:00
template<typename T>
class blocking_queue {
public:
2014-01-25 05:50:26 -05:00
using queue_t = std::queue<T>;
using size_type = typename queue_t::size_type;
using clock = std::chrono::system_clock;
2014-01-25 08:52:10 -05:00
explicit blocking_queue(size_type max_size) :max_size_(max_size), q_()
2014-01-25 04:09:04 -05:00
{}
blocking_queue(const blocking_queue&) = delete;
2014-01-25 08:52:10 -05:00
blocking_queue& operator=(const blocking_queue&) = delete;
2014-01-25 04:09:04 -05:00
~blocking_queue() = default;
2014-01-25 05:50:26 -05:00
size_type size()
2014-01-25 04:09:04 -05:00
{
2014-01-25 08:52:10 -05:00
std::lock_guard<std::mutex> lock(mutex_);
return q_.size();
2014-01-25 04:09:04 -05:00
}
2014-01-25 05:50:26 -05:00
// Push copy of item into the back of the queue.
2014-01-25 05:59:43 -05:00
// If the queue is full, block the calling thread util there is room or timeout have passed.
2014-01-25 05:50:26 -05:00
// Return: false on timeout, true on successful push.
2014-01-31 15:52:07 -05:00
template<typename Duration_Rep, typename Duration_Period, typename TT>
bool push(TT&& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout)
2014-01-25 04:09:04 -05:00
{
2014-01-25 08:52:10 -05:00
std::unique_lock<std::mutex> ul(mutex_);
if (q_.size() >= max_size_)
2014-01-25 05:50:26 -05:00
{
2014-01-25 08:52:10 -05:00
if (!item_popped_cond_.wait_until(ul, clock::now() + timeout, [this]() { return this->q_.size() < this->max_size_; }))
2014-01-25 04:09:04 -05:00
return false;
}
2014-01-31 15:52:07 -05:00
q_.push(std::forward<TT>(item));
2014-01-25 08:52:10 -05:00
if (q_.size() <= 1)
2014-01-25 05:50:26 -05:00
{
2014-01-25 05:59:43 -05:00
ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly..
2014-01-25 08:52:10 -05:00
item_pushed_cond_.notify_one();
2014-01-25 05:50:26 -05:00
}
2014-01-25 04:09:04 -05:00
return true;
}
2014-01-25 05:50:26 -05:00
// Push copy of item into the back of the queue.
2014-01-25 05:59:43 -05:00
// If the queue is full, block the calling thread until there is room.
2014-01-31 15:52:07 -05:00
template<typename TT>
void push(TT&& item)
2014-01-25 05:50:26 -05:00
{
2014-01-31 15:52:07 -05:00
while (!push(std::forward<TT>(item), one_hour));
2014-01-25 05:50:26 -05:00
}
2014-01-25 05:59:43 -05: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/
2014-01-25 05:50:26 -05:00
template<class Duration_Rep, class Duration_Period>
bool pop(T& item, const std::chrono::duration<Duration_Rep, Duration_Period>& timeout)
2014-01-25 04:09:04 -05:00
{
2014-01-25 08:52:10 -05:00
std::unique_lock<std::mutex> ul(mutex_);
if (q_.empty())
2014-01-25 05:50:26 -05:00
{
2014-01-25 08:52:10 -05:00
if (!item_pushed_cond_.wait_until(ul, clock::now() + timeout, [this]() { return !this->q_.empty(); }))
2014-01-25 04:09:04 -05:00
return false;
}
2014-01-31 18:47:37 -05:00
item = std::move(q_.front());
2014-01-25 08:52:10 -05:00
q_.pop();
if (q_.size() >= max_size_ - 1)
2014-01-25 05:50:26 -05:00
{
2014-01-25 05:59:43 -05:00
ul.unlock(); //So the notified thread will have better chance to accuire the lock immediatly..
2014-01-25 08:52:10 -05:00
item_popped_cond_.notify_one();
2014-01-25 05:50:26 -05:00
}
2014-01-25 04:09:04 -05:00
return true;
}
2014-01-25 05:50:26 -05:00
2014-01-25 05:59:43 -05: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.
2014-01-25 05:50:26 -05:00
void pop(T& item)
{
2014-01-28 21:00:05 -05:00
while (!pop(item, one_hour));
2014-01-25 05:50:26 -05:00
}
2014-01-25 08:52:10 -05:00
// Clear the queue
void clear()
{
2014-01-31 19:27:21 -05:00
{
std::unique_lock<std::mutex> ul(mutex_);
2014-02-01 17:00:29 -05:00
queue_t().swap(q_);
2014-01-31 19:27:21 -05:00
}
item_popped_cond_.notify_all();
2014-01-25 08:52:10 -05:00
}
2014-01-25 04:09:04 -05:00
private:
2014-01-25 08:52:10 -05:00
size_type max_size_;
std::queue<T> q_;
std::mutex mutex_;
std::condition_variable item_pushed_cond_;
2014-01-28 21:00:05 -05:00
std::condition_variable item_popped_cond_;
2014-01-31 15:52:07 -05:00
const std::chrono::hours one_hour{1};
2014-01-25 04:09:04 -05:00
};
2014-01-28 21:00:05 -05:00
}
2014-01-25 04:09:04 -05:00
}