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.
This commit is contained in:
Markus Mäkelä 2017-04-24 16:07:42 +03:00
parent ea39b15bbb
commit 963ff0216d
2 changed files with 34 additions and 0 deletions

View File

@ -320,6 +320,21 @@ public:
*/
static size_t execute_on_all(std::auto_ptr<DisposableTask> 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.
*

View File

@ -603,6 +603,25 @@ size_t Worker::execute_on_all(std::auto_ptr<DisposableTask> 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)
{