Task

Minimal usable Task class.

Serves as a base for all other task classes. Provides the following functionality: * assigning a task to an arbitrary fiber (assignTo()) * wraps the abstract run() method in a try/catch statement which rethrows any unhandled exceptions

Constructors

this
this()

Constructor. Used only to insert debug trace message.

Members

Functions

assignTo
void assignTo(WorkerFiber fiber, void delegate() entry_point)

Assigns the task to a fiber. In most cases you need to use Scheduler.schedule instead.

entryPoint
bool entryPoint()

Internal wrapper around this.run() which is used as primary fiber entry point and ensures any uncaught exception propagates to the context that has started this task.

finished
bool finished()
kill
void kill(istring file, int line)

Forces abnormal termination for the task by throwing special exception instance.

recycle
void recycle()

Method that will be run by scheduler when task finishes. Must be overridden by specific task class to reset reusable resources.

removeTerminationHook
void removeTerminationHook(void delegate() hook)

Unregisters a termination hook that would be executed when the Task is killed.

resume
void resume()

Resumes execution of this task. If task has not been started yet, starts it.

run
void run()

Method that must be overridden in actual application/library task classes to provide entry point.

suspend
void suspend()

Suspends execution of this task.

suspended
bool suspended()
terminationHook
void terminationHook(void delegate() hook)

Registers a termination hook that will be executed when the Task is killed.

Static functions

getThis
Task getThis()

Variables

fiber
WorkerFiber fiber;

Fiber this task executes in

object_pool_index
size_t object_pool_index;

Reserved index field which ensures that any Task derivative can be used with ObjectPool. That comes at minor cost of one unused size_t per Task instance if not needed which is not a problem.

Examples

// represents some limited resource used by this task (e.g. memory or a
// file handle)
class LimitedResourceHandle { }
LimitedResourceHandle getResource ( ) { return null; }
void releaseResource ( LimitedResourceHandle ) { }

// example custom task class
class MyTask : Task
{
    LimitedResourceHandle resource;

    override public void run ( )
    {
        this.resource = getResource();
    }

    override public void recycle ( )
    {
        releaseResource(this.resource);
    }
}

// Example of running a task by manually spawning a worker fiber.
// More commonly, it is instead done via ocean.task.Scheduler.
auto task = new MyTask;
// a large stack size is important for debug traces to not crash tests:
task.assignTo(new WorkerFiber(10240));
task.resume();

Meta