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
|
|
|
|
{
|
2019-02-28 19:59:10 +01:00
|
|
|
template<class T,
|
|
|
|
class A = std::allocator<T>>
|
2023-02-20 21:16:52 +01:00
|
|
|
struct queue;
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2016-11-01 11:56:31 +01:00
|
|
|
|
2019-02-28 19:59:10 +01:00
|
|
|
template<class T,
|
|
|
|
class A>
|
2023-02-20 21:16:52 +01:00
|
|
|
struct ircd::ctx::queue
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
2023-02-20 21:16:52 +01:00
|
|
|
using opts = dock::opts;
|
|
|
|
|
|
|
|
private:
|
2018-12-28 21:42:08 +01:00
|
|
|
dock d;
|
2019-02-28 19:59:10 +01:00
|
|
|
std::deque<T, A> q;
|
2018-10-21 09:44:07 +02:00
|
|
|
size_t w {0};
|
2016-11-01 11:56:31 +01:00
|
|
|
|
|
|
|
public:
|
2018-10-21 09:44:07 +02:00
|
|
|
size_t empty() const;
|
|
|
|
size_t size() const;
|
|
|
|
size_t waiting() const;
|
2016-11-01 11:56:31 +01:00
|
|
|
|
|
|
|
// Consumer interface; waits for item and std::move() it off the queue
|
2023-02-20 21:16:52 +01:00
|
|
|
template<class time_point> T pop_until(time_point&&, const opts & = (opts)0);
|
|
|
|
template<class duration> T pop_for(const duration &, const opts & = (opts)0);
|
|
|
|
T pop(const opts & = (opts)0);
|
2016-11-01 11:56:31 +01:00
|
|
|
|
|
|
|
// Producer interface; emplace item on the queue and notify consumer
|
2023-02-20 21:16:52 +01:00
|
|
|
template<class... args> void emplace(const opts &, args&&...);
|
2016-11-01 11:56:31 +01:00
|
|
|
template<class... args> void emplace(args&&...);
|
2023-02-20 21:16:52 +01:00
|
|
|
|
|
|
|
void push(const opts &, const T &);
|
2016-11-01 11:56:31 +01:00
|
|
|
void push(const T &);
|
2023-02-20 21:16:52 +01:00
|
|
|
|
|
|
|
void push(const opts &, T &&) noexcept;
|
2016-11-01 11:56:31 +01:00
|
|
|
void push(T &&) noexcept;
|
|
|
|
|
2019-02-28 19:59:10 +01:00
|
|
|
queue();
|
|
|
|
queue(A&& alloc);
|
2016-11-01 11:56:31 +01:00
|
|
|
~queue() noexcept;
|
|
|
|
};
|
|
|
|
|
2019-02-28 19:59:10 +01:00
|
|
|
template<class T,
|
|
|
|
class A>
|
2022-09-11 21:43:46 +02:00
|
|
|
inline
|
2019-02-28 19:59:10 +01:00
|
|
|
ircd::ctx::queue<T, A>::queue()
|
|
|
|
:q(std::allocator<T>())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T,
|
|
|
|
class A>
|
2022-09-11 21:43:46 +02:00
|
|
|
inline
|
2019-02-28 19:59:10 +01:00
|
|
|
ircd::ctx::queue<T, A>::queue(A&& alloc)
|
|
|
|
:q(std::forward<A>(alloc))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T,
|
|
|
|
class A>
|
2022-09-11 21:43:46 +02:00
|
|
|
inline
|
2022-06-20 08:09:00 +02:00
|
|
|
ircd::ctx::queue<T, A>::~queue()
|
2016-11-01 11:56:31 +01:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
assert(q.empty());
|
|
|
|
}
|
|
|
|
|
2019-02-28 19:59:10 +01:00
|
|
|
template<class T,
|
|
|
|
class A>
|
2022-09-11 21:43:46 +02:00
|
|
|
inline void
|
2019-02-28 19:59:10 +01:00
|
|
|
ircd::ctx::queue<T, A>::push(T&& t)
|
2016-11-01 11:56:31 +01:00
|
|
|
noexcept
|
|
|
|
{
|
2023-02-20 21:16:52 +01:00
|
|
|
static const opts opts {0};
|
|
|
|
push(opts, std::forward<T>(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T,
|
|
|
|
class A>
|
|
|
|
inline void
|
|
|
|
ircd::ctx::queue<T, A>::push(const opts &opts,
|
|
|
|
T&& t)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(opts & opts::LIFO)
|
|
|
|
q.push_front(std::forward<T>(t));
|
|
|
|
else
|
|
|
|
q.push_back(std::forward<T>(t));
|
|
|
|
|
2018-12-28 21:42:08 +01:00
|
|
|
d.notify();
|
2016-11-01 11:56:31 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 19:59:10 +01:00
|
|
|
template<class T,
|
|
|
|
class A>
|
2022-09-11 21:43:46 +02:00
|
|
|
inline void
|
2019-02-28 19:59:10 +01:00
|
|
|
ircd::ctx::queue<T, A>::push(const T &t)
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
2023-02-20 21:16:52 +01:00
|
|
|
static const opts opts {0};
|
|
|
|
push(opts, t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T,
|
|
|
|
class A>
|
|
|
|
inline void
|
|
|
|
ircd::ctx::queue<T, A>::push(const opts &opts,
|
|
|
|
const T &t)
|
|
|
|
{
|
|
|
|
if(opts & opts::LIFO)
|
|
|
|
q.push_front(t);
|
|
|
|
else
|
|
|
|
q.push_back(t);
|
|
|
|
|
2018-12-28 21:42:08 +01:00
|
|
|
d.notify();
|
2016-11-01 11:56:31 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 19:59:10 +01:00
|
|
|
template<class T,
|
|
|
|
class A>
|
2016-11-01 11:56:31 +01:00
|
|
|
template<class... args>
|
2022-09-11 21:43:46 +02:00
|
|
|
inline void
|
2019-02-28 19:59:10 +01:00
|
|
|
ircd::ctx::queue<T, A>::emplace(args&&... a)
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
2023-02-20 21:16:52 +01:00
|
|
|
static const opts opts {0};
|
|
|
|
emplace(opts, std::forward<args>(a)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T,
|
|
|
|
class A>
|
|
|
|
template<class... args>
|
|
|
|
inline void
|
|
|
|
ircd::ctx::queue<T, A>::emplace(const opts &opts,
|
|
|
|
args&&... a)
|
|
|
|
{
|
|
|
|
if(opts & opts::LIFO)
|
|
|
|
q.emplace_front(std::forward<args>(a)...);
|
|
|
|
else
|
|
|
|
q.emplace_back(std::forward<args>(a)...);
|
|
|
|
|
2018-12-28 21:42:08 +01:00
|
|
|
d.notify();
|
2016-11-01 11:56:31 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 19:59:10 +01:00
|
|
|
template<class T,
|
|
|
|
class A>
|
2022-09-11 21:43:46 +02:00
|
|
|
inline T
|
2023-02-20 21:16:52 +01:00
|
|
|
ircd::ctx::queue<T, A>::pop(const opts &opts)
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
2019-02-28 22:33:21 +01:00
|
|
|
const scope_count w
|
2018-10-21 09:44:07 +02:00
|
|
|
{
|
2019-02-28 22:33:21 +01:00
|
|
|
this->w
|
|
|
|
};
|
2018-10-21 09:44:07 +02:00
|
|
|
|
2023-02-20 21:16:52 +01:00
|
|
|
const auto predicate{[this]() noexcept
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
|
|
|
return !q.empty();
|
2023-02-20 21:16:52 +01:00
|
|
|
}};
|
|
|
|
|
|
|
|
d.wait(predicate, opts);
|
2016-11-01 11:56:31 +01:00
|
|
|
|
2018-09-19 08:51:43 +02:00
|
|
|
assert(!q.empty());
|
|
|
|
auto ret(std::move(q.front()));
|
2019-02-28 19:12:30 +01:00
|
|
|
q.pop_front();
|
2018-09-19 08:51:43 +02:00
|
|
|
return ret;
|
2016-11-01 11:56:31 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 19:59:10 +01:00
|
|
|
template<class T,
|
|
|
|
class A>
|
2016-11-01 11:56:31 +01:00
|
|
|
template<class duration>
|
2022-09-11 21:43:46 +02:00
|
|
|
inline T
|
2023-02-20 21:16:52 +01:00
|
|
|
ircd::ctx::queue<T, A>::pop_for(const duration &dur,
|
|
|
|
const opts &opts)
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
2019-02-28 22:33:21 +01:00
|
|
|
const scope_count w
|
2018-10-21 09:44:07 +02:00
|
|
|
{
|
2019-02-28 22:33:21 +01:00
|
|
|
this->w
|
|
|
|
};
|
2018-10-21 09:44:07 +02:00
|
|
|
|
2023-02-20 21:16:52 +01:00
|
|
|
const auto predicate{[this]() noexcept
|
|
|
|
{
|
|
|
|
return !q.empty();
|
|
|
|
}};
|
|
|
|
|
2018-03-27 01:52:14 +02:00
|
|
|
const bool ready
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
2023-02-20 21:16:52 +01:00
|
|
|
d.wait_for(dur, predicate, opts)
|
2018-03-27 01:52:14 +02:00
|
|
|
};
|
2016-11-01 11:56:31 +01:00
|
|
|
|
2023-02-20 21:16:52 +01:00
|
|
|
if(unlikely(!ready))
|
2018-03-27 01:52:14 +02:00
|
|
|
throw timeout{};
|
2016-11-01 11:56:31 +01:00
|
|
|
|
2018-09-19 08:51:43 +02:00
|
|
|
assert(!q.empty());
|
|
|
|
auto ret(std::move(q.front()));
|
|
|
|
q.pop();
|
|
|
|
return ret;
|
2016-11-01 11:56:31 +01:00
|
|
|
}
|
|
|
|
|
2019-02-28 19:59:10 +01:00
|
|
|
template<class T,
|
|
|
|
class A>
|
2016-11-01 11:56:31 +01:00
|
|
|
template<class time_point>
|
2022-09-11 21:43:46 +02:00
|
|
|
inline T
|
2023-02-20 21:16:52 +01:00
|
|
|
ircd::ctx::queue<T, A>::pop_until(time_point&& tp,
|
|
|
|
const opts &opts)
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
2019-02-28 22:33:21 +01:00
|
|
|
const scope_count w
|
2018-10-21 09:44:07 +02:00
|
|
|
{
|
2019-02-28 22:33:21 +01:00
|
|
|
this->w
|
|
|
|
};
|
2018-10-21 09:44:07 +02:00
|
|
|
|
2023-02-20 21:16:52 +01:00
|
|
|
const auto predicate{[this]() noexcept
|
|
|
|
{
|
|
|
|
return !q.empty();
|
|
|
|
}};
|
|
|
|
|
2018-03-27 01:52:14 +02:00
|
|
|
const bool ready
|
2016-11-01 11:56:31 +01:00
|
|
|
{
|
2023-02-20 21:16:52 +01:00
|
|
|
d.wait_until(tp, predicate, opts)
|
2018-03-27 01:52:14 +02:00
|
|
|
};
|
|
|
|
|
2023-02-20 21:16:52 +01:00
|
|
|
if(unlikely(!ready))
|
2018-03-27 01:52:14 +02:00
|
|
|
throw timeout{};
|
2016-11-01 11:56:31 +01:00
|
|
|
|
2018-09-19 08:51:43 +02:00
|
|
|
assert(!q.empty());
|
|
|
|
auto ret(std::move(q.front()));
|
|
|
|
q.pop();
|
|
|
|
return ret;
|
2016-11-01 11:56:31 +01:00
|
|
|
}
|
2018-10-21 09:44:07 +02:00
|
|
|
|
2019-02-28 19:59:10 +01:00
|
|
|
template<class T,
|
|
|
|
class A>
|
2022-09-11 21:43:46 +02:00
|
|
|
inline size_t
|
2019-02-28 19:59:10 +01:00
|
|
|
ircd::ctx::queue<T, A>::waiting()
|
2018-10-21 09:44:07 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2019-02-28 19:59:10 +01:00
|
|
|
template<class T,
|
|
|
|
class A>
|
2022-09-11 21:43:46 +02:00
|
|
|
inline size_t
|
2019-02-28 19:59:10 +01:00
|
|
|
ircd::ctx::queue<T, A>::size()
|
2018-10-21 09:44:07 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return q.size();
|
|
|
|
}
|
|
|
|
|
2019-02-28 19:59:10 +01:00
|
|
|
template<class T,
|
|
|
|
class A>
|
2022-09-11 21:43:46 +02:00
|
|
|
inline size_t
|
2019-02-28 19:59:10 +01:00
|
|
|
ircd::ctx::queue<T, A>::empty()
|
2018-10-21 09:44:07 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return q.empty();
|
|
|
|
}
|