Move execute_worker_task into mxs::Worker

The function has use outside of the monitors as it makes execution of
worker tasks much more convenient. Currently, this change only moves the
code and takes it into use: there should be no functional changes.
This commit is contained in:
Markus Mäkelä
2018-08-02 08:21:10 +03:00
parent 107395f608
commit d412b8d729
5 changed files with 91 additions and 68 deletions

View File

@ -2968,45 +2968,4 @@ void MonitorInstance::run_one_tick()
store_server_journal(m_monitor, m_master);
}
bool MonitorInstance::execute_worker_task(GenericFunction func, execute_mode_t mode)
{
/* The worker message system works on objects of class Task, each representing a different action.
* Let's use a function object inside a task to construct a generic action. */
class CustomTask : public maxscale::Worker::Task
{
public:
CustomTask(GenericFunction func)
: m_func(func)
{}
private:
GenericFunction m_func;
void execute(maxscale::Worker& worker)
{
m_func();
delete this; // Ok, since this object is not touched afterwards.
}
};
CustomTask* task = new (std::nothrow) CustomTask(func);
bool sent = false;
if (mode == Worker::EXECUTE_AUTO)
{
maxscale::Semaphore done(0);
/* Although the current method is being ran in the admin thread, 'post' sends the task to the
* worker thread of "this". */
sent = post(task, &done, mode);
if (sent)
{
done.wait();
}
}
else
{
sent = post(task, NULL, mode);
}
return sent;
}
}