Introduce concept of Worker tasks
A Worker::Task is an object that can be sent to a worker for
execution. The task is sent to the worker using the messaging
mechanism where the `execute` function of the task will be
called in the thread context of the worker.
There are two kinds of tasks; regular tasks and disposable tasks.
The former are just sent to the worker for execution while the
latter are sent and subsequently disposed of, once the task has
been executed.
A disposable task can be sent to either one worker or to all
workers. In the latter case, the task will be deleted once it
has been executed by all workers.
A semaphore can be associated with a regular task. Once the task
has been executed by the worker, the semaphore will automatically
be posted. That way, it is trivial to send a task for execution
to a worker and wait until the task has been executed. For instance:
Semaphore sem;
MyTask task;
pWorker->execute(&task, &sem);
sem.wait();
const MyResult& result = task.result();
The low level mechanism for posting and broadcasting messages will
be removed.
This commit is contained in:
65
server/core/maxscale/workertask.hh
Normal file
65
server/core/maxscale/workertask.hh
Normal file
@ -0,0 +1,65 @@
|
||||
#pragma once
|
||||
/*
|
||||
* Copyright (c) 2016 MariaDB Corporation Ab
|
||||
*
|
||||
* Use of this software is governed by the Business Source License included
|
||||
* in the LICENSE.TXT file and at www.mariadb.com/bsl11.
|
||||
*
|
||||
* Change Date: 2019-07-01
|
||||
*
|
||||
* On the date above, in accordance with the Business Source License, use
|
||||
* of this software will be governed by version 2 or later of the General
|
||||
* Public License.
|
||||
*/
|
||||
|
||||
#include <maxscale/cppdefs.hh>
|
||||
|
||||
namespace maxscale
|
||||
{
|
||||
|
||||
class Worker;
|
||||
|
||||
/**
|
||||
* A WorkerTask represents a task to be performed by a Worker.
|
||||
*/
|
||||
class WorkerTask
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
virtual ~WorkerTask();
|
||||
|
||||
/**
|
||||
* @brief Called in the context of a specific worker.
|
||||
*
|
||||
* @param worker The worker in whose context `execute` is called.
|
||||
*
|
||||
* @attention As the function is called by a worker, the body of `execute`
|
||||
* should execute quickly and not perform any blocking operations.
|
||||
*/
|
||||
virtual void execute(Worker& worker) = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* A WorkerDisposableTask represents a task to be performed by a Worker.
|
||||
*
|
||||
* When the task has been executed, the instance will automatically be
|
||||
* deleted.
|
||||
*/
|
||||
class WorkerDisposableTask : public WorkerTask
|
||||
{
|
||||
protected:
|
||||
WorkerDisposableTask();
|
||||
|
||||
private:
|
||||
friend class Worker;
|
||||
|
||||
void inc_count();
|
||||
void dec_count();
|
||||
|
||||
private:
|
||||
int32_t m_count;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user