/* * Copyright (C) 2016 Charybdis Development Team * Copyright (C) 2016 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #pragma once #define HAVE_IRCD_CTX_QUEUE_H namespace ircd::ctx { template class queue; } template class ircd::ctx::queue { struct dock dock; std::queue q; public: auto empty() const { return q.empty(); } auto size() const { return q.size(); } // 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() = default; ~queue() noexcept; }; template ircd::ctx::queue::~queue() noexcept { assert(q.empty()); } template void ircd::ctx::queue::push(T &&t) noexcept { q.push(std::move(t)); dock.notify(); } template void ircd::ctx::queue::push(const T &t) { q.push(t); dock.notify(); } template template void ircd::ctx::queue::emplace(args&&... a) { q.emplace(std::forward(a)...); dock.notify(); } template T ircd::ctx::queue::pop() { dock.wait([this] { return !q.empty(); }); const scope pop([this] { q.pop(); }); auto &ret(q.front()); return std::move(ret); } template template T ircd::ctx::queue::pop_for(const duration &dur) { const auto status(dock.wait_for(dur, [this] { return !q.empty(); })); if(status == cv_status::timeout) throw timeout(); const scope pop([this] { q.pop(); }); auto &ret(q.front()); return std::move(ret); } template template T ircd::ctx::queue::pop_until(time_point&& tp) { const auto status(dock.wait_until(tp, [this] { return !q.empty(); })); if(status == cv_status::timeout) throw timeout(); const scope pop([this] { q.pop(); }); auto &ret(q.front()); return std::move(ret); }