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:
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -477,6 +477,51 @@ bool Worker::post_disposable(DisposableTask* pTask, enum execute_mode_t mode)
|
||||
return posted;
|
||||
}
|
||||
|
||||
bool Worker::post(GenericFunction func, Semaphore* pSem, execute_mode_t mode)
|
||||
{
|
||||
|
||||
class CustomTask : public maxscale::WorkerTask
|
||||
{
|
||||
public:
|
||||
|
||||
CustomTask(GenericFunction func)
|
||||
: m_func(func)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
GenericFunction m_func;
|
||||
|
||||
void execute(maxscale::Worker& worker)
|
||||
{
|
||||
m_func();
|
||||
|
||||
// The task needs to delete itself only after the task has been executed
|
||||
delete this;
|
||||
}
|
||||
};
|
||||
|
||||
bool rval = false;
|
||||
CustomTask* task = new (std::nothrow) CustomTask(func);
|
||||
|
||||
if (task)
|
||||
{
|
||||
if (!(rval = post(task, pSem, mode)))
|
||||
{
|
||||
// Posting the task failed, it needs to be deleted now
|
||||
delete task;
|
||||
}
|
||||
}
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
bool Worker::execute(GenericFunction func)
|
||||
{
|
||||
Semaphore sem;
|
||||
return post(func, &sem, EXECUTE_AUTO) && sem.wait();
|
||||
}
|
||||
|
||||
bool Worker::post_message(uint32_t msg_id, intptr_t arg1, intptr_t arg2)
|
||||
{
|
||||
// NOTE: No logging here, this function must be signal safe.
|
||||
|
||||
Reference in New Issue
Block a user