TaskPool

Task pool which integrates with ocean.task.Scheduler.

It is assumed that tasks that are going to be used with this class will require certain parameters to be passed to the task when it is started. These parameters are defined by the arguments of the copyArguments method, which the task class must implement. When a task is fetched from the pool to be used, its copyArguments method will be called, allowing the specified parameters to be passed into it.

If the derived tasks contains a method matching the signature of void deserialize ( void[] ) then the TaskPool with be created with support for starting tasks with a void buffer (via the restore() method). This supports serialization and deserialization of task internal state.

It is crucial for tasks to either deep copy their initial parameters or ensure that those can never change during the task's lifetime, otherwise very hard to debug fiber races can happen.

Members

Aliases

TaskType
alias TaskType = TaskT

Convenience alias for knowing task type.

Functions

awaitRunningTasks
void awaitRunningTasks()

Suspends the current task until all running tasks in the pool have finished executing.

restore
bool restore(ParametersOf!(TaskT.deserialize) args)

Starts a task in the same manner as start but instead calls the deserialize() method on the derived task with arguments supported. This is to support dumping and loading tasks from disk.

start
bool start(ParametersOf!(TaskT.copyArguments) args)

Convenience method that does preparing initial arguments of reusable task and starting it via theScheduler in one go.

startImpl
void startImpl(Task task)

Common part of start implementation reused by derivatives. Split into separate method to ensure that recycling hook won't be omitted.

Parameters

TaskT

specific task type managed by this pool

Examples

void example ( )
{
    static class DummyTask : Task
    {
        import ocean.core.Array : copy;

        // The task requires a single string, which is copied from the outside
        // by `copyArguments()`
        private mstring buffer;

        public void copyArguments ( cstring arg )
        {
            this.buffer.copy(arg);
        }

        override public void recycle ( )
        {
            this.buffer.length = 0;
            assumeSafeAppend(this.buffer);
        }

        public override void run ( )
        {
            // do good stuff
        }
    }

    auto pool = new TaskPool!(DummyTask);

    // Start some tasks, passing the required parameters to the pool's `start()`
    // method
    pool.start("abcd");
    pool.start("xyz");

    theScheduler.eventLoop();
}

Meta