// Matrix Construct // // Copyright (C) Matrix Construct Developers, Authors & Contributors // Copyright (C) 2016-2018 Jason Volk // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. #pragma once #define HAVE_IRCD_CTX_QUEUE_H namespace ircd::ctx { template> class queue; } template class ircd::ctx::queue { dock d; std::deque q; size_t w {0}; public: size_t empty() const; size_t size() const; size_t waiting() const; // Consumer interface; waits for item and std::move() it off the queue template T pop_until(time_point&&); template T pop_for(const duration &); T pop(); // Producer interface; emplace item on the queue and notify consumer template void emplace(args&&...); void push(const T &); void push(T &&) noexcept; queue(); queue(A&& alloc); ~queue() noexcept; }; template inline ircd::ctx::queue::queue() :q(std::allocator()) { } template inline ircd::ctx::queue::queue(A&& alloc) :q(std::forward(alloc)) { } template inline ircd::ctx::queue::~queue() noexcept { assert(q.empty()); } template inline void ircd::ctx::queue::push(T&& t) noexcept { q.push_back(std::forward(t)); d.notify(); } template inline void ircd::ctx::queue::push(const T &t) { q.push_back(t); d.notify(); } template template inline void ircd::ctx::queue::emplace(args&&... a) { q.emplace(std::forward(a)...); d.notify(); } template inline T ircd::ctx::queue::pop() { const scope_count w { this->w }; d.wait([this]() noexcept { return !q.empty(); }); assert(!q.empty()); auto ret(std::move(q.front())); q.pop_front(); return ret; } template template inline T ircd::ctx::queue::pop_for(const duration &dur) { const scope_count w { this->w }; const bool ready { d.wait_for(dur, [this]() noexcept { return !q.empty(); }) }; if(!ready) throw timeout{}; assert(!q.empty()); auto ret(std::move(q.front())); q.pop(); return ret; } template template inline T ircd::ctx::queue::pop_until(time_point&& tp) { const scope_count w { this->w }; const bool ready { d.wait_until(tp, [this]() noexcept { return !q.empty(); }) }; if(!ready) throw timeout{}; assert(!q.empty()); auto ret(std::move(q.front())); q.pop(); return ret; } template inline size_t ircd::ctx::queue::waiting() const { return w; } template inline size_t ircd::ctx::queue::size() const { return q.size(); } template inline size_t ircd::ctx::queue::empty() const { return q.empty(); }