Skip to content

Commit

Permalink
Improvements to queue API.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnbrq committed Oct 18, 2023
1 parent b670e9e commit 4329a95
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions include/cxxdes/sync/queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,44 @@ struct queue {
template <typename ...Args>
[[nodiscard("expected usage: co_await queue.put(args...)")]]
subroutine<> put(Args && ...args) {
while (true) {
if (max_size_ == 0 || q_.size() < max_size_)
break ;
while (!can_put())
co_await event_.wait();
}
q_.emplace(std::forward<Args>(args)...);
co_await event_.wake();
}


[[nodiscard("expected usage: co_await queue.pop()")]]
subroutine<T> pop() {
while (true) {
if (q_.size() > 0)
break ;
while (!can_pop())
co_await event_.wait();
}
auto v = std::move(q_.front());
q_.pop();
co_await event_.wake();
co_return v;
}

std::size_t size() const {
std::size_t size() const noexcept {
return q_.size();
}

const std::queue<T> &underlying_queue() const {
std::size_t max_size() const noexcept {
return max_size_;
}

bool bounded() const noexcept {
return max_size() > 0;
}

bool can_put() const noexcept {
return !bounded() || (max_size() > size());
}

bool can_pop() const noexcept {
return size() > 0;
}

const std::queue<T> &underlying_queue() const noexcept {
return q_;
}
private:
Expand Down

0 comments on commit 4329a95

Please sign in to comment.