2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 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. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-11-01 11:56:31 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_CTX_QUEUE_H
|
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::ctx
|
|
|
|
{
|
|
|
|
template<class T> class queue;
|
|
|
|
}
|
2016-11-01 11:56:31 +01:00
|
|
|
|
|
|
|
template<class T>
|
2017-08-28 23:51:22 +02:00
|
|
|
class ircd::ctx::queue
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
|
|
|
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>
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::queue<T>::~queue()
|
2016-11-01 11:56:31 +01:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
assert(q.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::queue<T>::push(T &&t)
|
2016-11-01 11:56:31 +01:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
q.push(std::move(t));
|
|
|
|
dock.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::queue<T>::push(const T &t)
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
|
|
|
q.push(t);
|
|
|
|
dock.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
template<class... args>
|
|
|
|
void
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::queue<T>::emplace(args&&... a)
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
|
|
|
q.emplace(std::forward<args>(a)...);
|
|
|
|
dock.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
T
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::queue<T>::pop()
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
|
|
|
dock.wait([this]
|
|
|
|
{
|
|
|
|
return !q.empty();
|
|
|
|
});
|
|
|
|
|
2017-09-22 22:57:43 +02:00
|
|
|
const unwind pop([this]
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
|
|
|
q.pop();
|
|
|
|
});
|
|
|
|
|
|
|
|
auto &ret(q.front());
|
|
|
|
return std::move(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
template<class duration>
|
|
|
|
T
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::queue<T>::pop_for(const duration &dur)
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
|
|
|
const auto status(dock.wait_for(dur, [this]
|
|
|
|
{
|
|
|
|
return !q.empty();
|
|
|
|
}));
|
|
|
|
|
|
|
|
if(status == cv_status::timeout)
|
|
|
|
throw timeout();
|
|
|
|
|
2017-09-22 22:57:43 +02:00
|
|
|
const unwind pop([this]
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
|
|
|
q.pop();
|
|
|
|
});
|
|
|
|
|
|
|
|
auto &ret(q.front());
|
|
|
|
return std::move(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
template<class time_point>
|
|
|
|
T
|
2017-08-28 23:51:22 +02:00
|
|
|
ircd::ctx::queue<T>::pop_until(time_point&& tp)
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
|
|
|
const auto status(dock.wait_until(tp, [this]
|
|
|
|
{
|
|
|
|
return !q.empty();
|
|
|
|
}));
|
|
|
|
|
|
|
|
if(status == cv_status::timeout)
|
|
|
|
throw timeout();
|
|
|
|
|
2017-09-22 22:57:43 +02:00
|
|
|
const unwind pop([this]
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
|
|
|
q.pop();
|
|
|
|
});
|
|
|
|
|
|
|
|
auto &ret(q.front());
|
|
|
|
return std::move(ret);
|
|
|
|
}
|