Skip to content

Commit

Permalink
Small processing_queue improvements (#4159)
Browse files Browse the repository at this point in the history
* Add batch type alias

* Add `joinable` function

* Use universal reference
  • Loading branch information
pwojcikdev authored Mar 1, 2023
1 parent 81a2ab4 commit 9f682b4
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions nano/lib/processing_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <nano/lib/threading.hpp>
#include <nano/lib/utility.hpp>

#include <algorithm>
#include <condition_variable>
#include <deque>
#include <functional>
Expand All @@ -23,6 +24,7 @@ class processing_queue final
{
public:
using value_t = T;
using batch_t = std::deque<value_t>;

/**
* @param thread_role Spawned processing threads will use this name
Expand Down Expand Up @@ -71,15 +73,23 @@ class processing_queue final
threads.clear ();
}

bool joinable () const
{
return std::any_of (threads.cbegin (), threads.cend (), [] (auto const & thread) {
return thread.joinable ();
});
}

/**
* Queues item for batch processing
*/
void add (T && item)
template <class Item>
void add (Item && item)
{
nano::unique_lock<nano::mutex> lock{ mutex };
if (queue.size () < max_queue_size)
{
queue.emplace_back (std::forward<T> (item));
queue.push_back (std::forward<T> (item));
lock.unlock ();
condition.notify_one ();
stats.inc (stat_type, nano::stat::detail::queue);
Expand Down Expand Up @@ -136,7 +146,7 @@ class processing_queue final
for (int n = 0; n < max_batch_size; ++n)
{
debug_assert (!queue.empty ());
queue_l.emplace_back (std::move (queue.front ()));
queue_l.push_back (std::move (queue.front ()));
queue.pop_front ();
}
return queue_l;
Expand All @@ -160,7 +170,7 @@ class processing_queue final
}

public:
std::function<void (std::deque<value_t> &)> process_batch{ [] (auto &) { debug_assert (false, "processing queue callback empty"); } };
std::function<void (batch_t &)> process_batch{ [] (auto &) { debug_assert (false, "processing queue callback empty"); } };

private:
nano::stats & stats;
Expand Down

0 comments on commit 9f682b4

Please sign in to comment.