Move execute_worker_task to MonitorInstance

The function is rather general and may of use to other monitor modules.
This commit is contained in:
Esa Korhonen
2018-07-23 14:35:42 +03:00
parent 27084f1368
commit b421e56d1c
4 changed files with 56 additions and 36 deletions

View File

@ -3013,4 +3013,45 @@ 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;
}
}