2019-02-10 06:05:19 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <deque>
|
2020-03-09 08:50:30 +00:00
|
|
|
#include <../misc.hpp>
|
2019-02-10 06:05:19 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2019-09-26 03:55:39 +00:00
|
|
|
class blocking_queue {
|
2019-02-10 06:05:19 +00:00
|
|
|
std::deque<T> deque{};
|
2019-02-10 08:16:52 +00:00
|
|
|
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
|
|
|
bool cancelled = false;
|
2019-02-10 06:05:19 +00:00
|
|
|
public:
|
2019-09-26 03:55:39 +00:00
|
|
|
~blocking_queue();
|
2019-02-10 06:05:19 +00:00
|
|
|
T take();
|
2019-09-26 03:55:39 +00:00
|
|
|
T &front() const;
|
|
|
|
T &back() const;
|
|
|
|
void push(const T&);
|
|
|
|
void push(T&&);
|
2019-02-10 06:05:19 +00:00
|
|
|
template< class... Args >
|
2019-09-26 03:55:39 +00:00
|
|
|
void emplace(Args &&... args);
|
2019-02-10 06:05:19 +00:00
|
|
|
void clear();
|
2019-02-10 08:16:52 +00:00
|
|
|
void cancel();
|
2019-02-10 06:05:19 +00:00
|
|
|
};
|
|
|
|
|
2019-02-10 08:16:52 +00:00
|
|
|
#define run_and_notify(block) \
|
2019-09-26 03:55:39 +00:00
|
|
|
mutex_guard g(this->lock); \
|
2019-02-10 08:16:52 +00:00
|
|
|
block \
|
2019-09-26 03:55:39 +00:00
|
|
|
pthread_cond_signal(&this->cond);
|
2019-02-10 08:16:52 +00:00
|
|
|
|
2019-02-10 06:05:19 +00:00
|
|
|
template<typename T>
|
2019-09-26 03:55:39 +00:00
|
|
|
blocking_queue<T>::~blocking_queue() {
|
2019-02-10 06:05:19 +00:00
|
|
|
pthread_mutex_destroy(&lock);
|
|
|
|
pthread_cond_destroy(&cond);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-09-26 03:55:39 +00:00
|
|
|
T blocking_queue<T>::take() {
|
|
|
|
mutex_guard g(lock);
|
2019-02-10 08:16:52 +00:00
|
|
|
cancelled = false;
|
|
|
|
while (deque.empty() && !cancelled)
|
2019-02-10 06:05:19 +00:00
|
|
|
pthread_cond_wait(&cond, &lock);
|
2019-02-10 08:16:52 +00:00
|
|
|
if (cancelled)
|
|
|
|
pthread_exit(nullptr);
|
2019-02-10 06:05:19 +00:00
|
|
|
T ret(std::move(deque.front()));
|
|
|
|
deque.pop_front();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-09-26 03:55:39 +00:00
|
|
|
void blocking_queue<T>::push(const T &s) {
|
2019-02-10 08:16:52 +00:00
|
|
|
run_and_notify({ deque.push_back(s); })
|
2019-02-10 06:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-09-26 03:55:39 +00:00
|
|
|
void blocking_queue<T>::push(T &&s) {
|
2019-02-10 08:16:52 +00:00
|
|
|
run_and_notify({ deque.push_back(std::move(s)); })
|
2019-02-10 06:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-09-26 03:55:39 +00:00
|
|
|
T &blocking_queue<T>::front() const {
|
|
|
|
mutex_guard g(lock);
|
|
|
|
return deque.front();
|
2019-02-10 06:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-09-26 03:55:39 +00:00
|
|
|
T &blocking_queue<T>::back() const {
|
|
|
|
mutex_guard g(lock);
|
|
|
|
return deque.back();
|
2019-02-10 06:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
template<class... Args>
|
2019-09-26 03:55:39 +00:00
|
|
|
void blocking_queue<T>::emplace(Args &&... args) {
|
2019-02-10 08:16:52 +00:00
|
|
|
run_and_notify({ deque.emplace_back(std::forward<Args>(args)...); })
|
2019-02-10 06:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-09-26 03:55:39 +00:00
|
|
|
void blocking_queue<T>::clear() {
|
|
|
|
mutex_guard g(lock);
|
2019-02-10 06:05:19 +00:00
|
|
|
std::deque<T> t;
|
|
|
|
deque.swap(t);
|
2019-02-10 08:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2019-09-26 03:55:39 +00:00
|
|
|
void blocking_queue<T>::cancel() {
|
2019-02-10 08:16:52 +00:00
|
|
|
run_and_notify({
|
|
|
|
cancelled = true;
|
|
|
|
std::deque<T> t;
|
|
|
|
deque.swap(t);
|
|
|
|
})
|
2019-02-10 06:05:19 +00:00
|
|
|
}
|
2019-09-26 03:55:39 +00:00
|
|
|
|
|
|
|
#undef run_and_notify
|