From 963ff0216de46b053d299875ca2b99cd7f77162b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Mon, 24 Apr 2017 16:07:42 +0300 Subject: [PATCH] Allow serial execution of worker tasks The Worker::execute_on_all_wait is intended to be used with dcb_foreach which expects a single-threaded context for its function. --- server/core/maxscale/worker.hh | 15 +++++++++++++++ server/core/worker.cc | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/server/core/maxscale/worker.hh b/server/core/maxscale/worker.hh index 69f3f49bd..a9e0c13c1 100644 --- a/server/core/maxscale/worker.hh +++ b/server/core/maxscale/worker.hh @@ -320,6 +320,21 @@ public: */ static size_t execute_on_all(std::auto_ptr sTask); + /** + * Executes a task on all workers in serial mode. + * + * The task is executed on at most one worker thread at a time. + * + * @param pTask The task to be executed. + * + * @return How many workers the task was posted to. + * + * @warning This function is extremely inefficient and will be slow compared + * to the other functions. Only use this function when printing thread-specific + * data to stdout. + */ + static size_t execute_on_all_serially(Task* pTask); + /** * Post a message to a worker. * diff --git a/server/core/worker.cc b/server/core/worker.cc index a369067b8..ff7cd61df 100644 --- a/server/core/worker.cc +++ b/server/core/worker.cc @@ -603,6 +603,25 @@ size_t Worker::execute_on_all(std::auto_ptr sTask) return n; } +//static +size_t Worker::execute_on_all_serially(Task* pTask) +{ + Semaphore sem; + size_t n = 0; + + for (int i = 0; i < this_unit.n_workers; ++i) + { + Worker* pWorker = this_unit.ppWorkers[i]; + + if (pWorker->execute(pTask, &sem)) + { + sem.wait(); + ++n; + } + } + + return n; +} bool Worker::post_message(uint32_t msg_id, intptr_t arg1, intptr_t arg2) {