Make combination of rworker_local values easier

The `values` helper method provides a convenient way to access all of the
internal values as a vector.
This commit is contained in:
Markus Mäkelä 2018-09-02 01:39:10 +03:00
parent 58c0b4f5f4
commit 1e7d088aed
No known key found for this signature in database
GPG Key ID: 72D48FCE664F7B19

View File

@ -508,6 +508,30 @@ public:
mxs_rworker_broadcast(update_value, this);
}
/**
* Get all local values
*
* Note: this method must be called from the main worker thread.
*
* @return A vector containing the individual values for each worker
*/
std::vector<T> values() const
{
mxb_assert_message(RoutingWorker::get_current() == RoutingWorker::get(RoutingWorker::MAIN),
"this method must be called from the main worker thread");
std::vector<T> rval;
std::mutex lock;
mxb::Semaphore sem;
auto n = RoutingWorker::broadcast([&](){
std::lock_guard<std::mutex> guard(lock);
rval.push_back(*get_local_value());
}, &sem, RoutingWorker::EXECUTE_AUTO);
sem.wait_n(n);
return std::move(rval);
}
private:
uint64_t m_handle; // The handle to the worker local data