0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-10 22:18:54 +02:00

ircd::ctx::queue: Add options for FIFO and LIFO strategies to interface.

This commit is contained in:
Jason Volk 2023-02-20 12:16:52 -08:00
parent e961b23d1e
commit ea92976369

View file

@ -15,13 +15,16 @@ namespace ircd::ctx
{ {
template<class T, template<class T,
class A = std::allocator<T>> class A = std::allocator<T>>
class queue; struct queue;
} }
template<class T, template<class T,
class A> class A>
class ircd::ctx::queue struct ircd::ctx::queue
{ {
using opts = dock::opts;
private:
dock d; dock d;
std::deque<T, A> q; std::deque<T, A> q;
size_t w {0}; size_t w {0};
@ -32,13 +35,18 @@ class ircd::ctx::queue
size_t waiting() const; size_t waiting() const;
// Consumer interface; waits for item and std::move() it off the queue // Consumer interface; waits for item and std::move() it off the queue
template<class time_point> T pop_until(time_point&&); template<class time_point> T pop_until(time_point&&, const opts & = (opts)0);
template<class duration> T pop_for(const duration &); template<class duration> T pop_for(const duration &, const opts & = (opts)0);
T pop(); T pop(const opts & = (opts)0);
// Producer interface; emplace item on the queue and notify consumer // Producer interface; emplace item on the queue and notify consumer
template<class... args> void emplace(const opts &, args&&...);
template<class... args> void emplace(args&&...); template<class... args> void emplace(args&&...);
void push(const opts &, const T &);
void push(const T &); void push(const T &);
void push(const opts &, T &&) noexcept;
void push(T &&) noexcept; void push(T &&) noexcept;
queue(); queue();
@ -77,7 +85,22 @@ inline void
ircd::ctx::queue<T, A>::push(T&& t) ircd::ctx::queue<T, A>::push(T&& t)
noexcept noexcept
{ {
q.push_back(std::forward<T>(t)); 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));
d.notify(); d.notify();
} }
@ -86,7 +109,21 @@ template<class T,
inline void inline void
ircd::ctx::queue<T, A>::push(const T &t) ircd::ctx::queue<T, A>::push(const T &t)
{ {
q.push_back(t); 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);
d.notify(); d.notify();
} }
@ -96,24 +133,41 @@ template<class... args>
inline void inline void
ircd::ctx::queue<T, A>::emplace(args&&... a) ircd::ctx::queue<T, A>::emplace(args&&... a)
{ {
q.emplace(std::forward<args>(a)...); 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)...);
d.notify(); d.notify();
} }
template<class T, template<class T,
class A> class A>
inline T inline T
ircd::ctx::queue<T, A>::pop() ircd::ctx::queue<T, A>::pop(const opts &opts)
{ {
const scope_count w const scope_count w
{ {
this->w this->w
}; };
d.wait([this]() noexcept const auto predicate{[this]() noexcept
{ {
return !q.empty(); return !q.empty();
}); }};
d.wait(predicate, opts);
assert(!q.empty()); assert(!q.empty());
auto ret(std::move(q.front())); auto ret(std::move(q.front()));
@ -125,22 +179,25 @@ template<class T,
class A> class A>
template<class duration> template<class duration>
inline T inline T
ircd::ctx::queue<T, A>::pop_for(const duration &dur) ircd::ctx::queue<T, A>::pop_for(const duration &dur,
const opts &opts)
{ {
const scope_count w const scope_count w
{ {
this->w this->w
}; };
const auto predicate{[this]() noexcept
{
return !q.empty();
}};
const bool ready const bool ready
{ {
d.wait_for(dur, [this]() noexcept d.wait_for(dur, predicate, opts)
{
return !q.empty();
})
}; };
if(!ready) if(unlikely(!ready))
throw timeout{}; throw timeout{};
assert(!q.empty()); assert(!q.empty());
@ -153,22 +210,25 @@ template<class T,
class A> class A>
template<class time_point> template<class time_point>
inline T inline T
ircd::ctx::queue<T, A>::pop_until(time_point&& tp) ircd::ctx::queue<T, A>::pop_until(time_point&& tp,
const opts &opts)
{ {
const scope_count w const scope_count w
{ {
this->w this->w
}; };
const auto predicate{[this]() noexcept
{
return !q.empty();
}};
const bool ready const bool ready
{ {
d.wait_until(tp, [this]() noexcept d.wait_until(tp, predicate, opts)
{
return !q.empty();
})
}; };
if(!ready) if(unlikely(!ready))
throw timeout{}; throw timeout{};
assert(!q.empty()); assert(!q.empty());