mirror of
https://github.com/matrix-construct/construct
synced 2024-06-26 05:48:20 +02:00
ircd::ctx: Add queue: amalgam of a standard queue container with a dock.
This commit is contained in:
parent
b0bc025ced
commit
f54c4215b4
|
@ -328,6 +328,7 @@ RB_CHK_SYSHEADER([map], [MAP])
|
||||||
RB_CHK_SYSHEADER([set], [SET])
|
RB_CHK_SYSHEADER([set], [SET])
|
||||||
RB_CHK_SYSHEADER([list], [LIST])
|
RB_CHK_SYSHEADER([list], [LIST])
|
||||||
RB_CHK_SYSHEADER([stack], [STACK])
|
RB_CHK_SYSHEADER([stack], [STACK])
|
||||||
|
RB_CHK_SYSHEADER([queue], [QUEUE])
|
||||||
RB_CHK_SYSHEADER([deque], [DEQUE])
|
RB_CHK_SYSHEADER([deque], [DEQUE])
|
||||||
RB_CHK_SYSHEADER([array], [ARRAY])
|
RB_CHK_SYSHEADER([array], [ARRAY])
|
||||||
RB_CHK_SYSHEADER([vector], [VECTOR])
|
RB_CHK_SYSHEADER([vector], [VECTOR])
|
||||||
|
|
|
@ -105,6 +105,7 @@ using ctx::timeout;
|
||||||
#include "ctx/context.h"
|
#include "ctx/context.h"
|
||||||
#include "ctx/prof.h"
|
#include "ctx/prof.h"
|
||||||
#include "ctx/dock.h"
|
#include "ctx/dock.h"
|
||||||
|
#include "ctx/queue.h"
|
||||||
#include "ctx/mutex.h"
|
#include "ctx/mutex.h"
|
||||||
#include "ctx/shared_state.h"
|
#include "ctx/shared_state.h"
|
||||||
#include "ctx/promise.h"
|
#include "ctx/promise.h"
|
||||||
|
|
148
include/ircd/ctx/queue.h
Normal file
148
include/ircd/ctx/queue.h
Normal file
|
@ -0,0 +1,148 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Charybdis Development Team
|
||||||
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
||||||
|
*
|
||||||
|
* 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 {
|
||||||
|
namespace ctx {
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class queue
|
||||||
|
{
|
||||||
|
struct dock dock;
|
||||||
|
std::queue<T> 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<class time_point> T pop_until(time_point&&);
|
||||||
|
template<class duration> T pop_for(const duration &);
|
||||||
|
T pop();
|
||||||
|
|
||||||
|
// Producer interface; emplace item on the queue and notify consumer
|
||||||
|
template<class... args> void emplace(args&&...);
|
||||||
|
void push(const T &);
|
||||||
|
void push(T &&) noexcept;
|
||||||
|
|
||||||
|
queue() = default;
|
||||||
|
~queue() noexcept;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
queue<T>::~queue()
|
||||||
|
noexcept
|
||||||
|
{
|
||||||
|
assert(q.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void
|
||||||
|
queue<T>::push(T &&t)
|
||||||
|
noexcept
|
||||||
|
{
|
||||||
|
q.push(std::move(t));
|
||||||
|
dock.notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
void
|
||||||
|
queue<T>::push(const T &t)
|
||||||
|
{
|
||||||
|
q.push(t);
|
||||||
|
dock.notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
template<class... args>
|
||||||
|
void
|
||||||
|
queue<T>::emplace(args&&... a)
|
||||||
|
{
|
||||||
|
q.emplace(std::forward<args>(a)...);
|
||||||
|
dock.notify();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
T
|
||||||
|
queue<T>::pop()
|
||||||
|
{
|
||||||
|
dock.wait([this]
|
||||||
|
{
|
||||||
|
return !q.empty();
|
||||||
|
});
|
||||||
|
|
||||||
|
const scope pop([this]
|
||||||
|
{
|
||||||
|
q.pop();
|
||||||
|
});
|
||||||
|
|
||||||
|
auto &ret(q.front());
|
||||||
|
return std::move(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
template<class duration>
|
||||||
|
T
|
||||||
|
queue<T>::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<class T>
|
||||||
|
template<class time_point>
|
||||||
|
T
|
||||||
|
queue<T>::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);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace ctx
|
||||||
|
} // namespace ircd
|
|
@ -79,6 +79,7 @@ extern "C" {
|
||||||
#include <RB_INC_FORWARD_LIST
|
#include <RB_INC_FORWARD_LIST
|
||||||
#include <RB_INC_UNORDERED_MAP
|
#include <RB_INC_UNORDERED_MAP
|
||||||
#include <RB_INC_DEQUE
|
#include <RB_INC_DEQUE
|
||||||
|
#include <RB_INC_QUEUE
|
||||||
#include <RB_INC_SSTREAM
|
#include <RB_INC_SSTREAM
|
||||||
#include <RB_INC_FSTREAM
|
#include <RB_INC_FSTREAM
|
||||||
#include <RB_INC_IOSTREAM
|
#include <RB_INC_IOSTREAM
|
||||||
|
|
Loading…
Reference in a new issue