| 1 |
-------------------------------------------------------------------------------- |
|---|
| 2 |
-- Title: LUTask.lua |
|---|
| 3 |
-- Description: Like a square peg in a round hole |
|---|
| 4 |
-- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ |
|---|
| 5 |
-- Creation Date: February 1, 2005 |
|---|
| 6 |
-- Legal: Copyright (C) 2005 Raphaël Szwarc |
|---|
| 7 |
-------------------------------------------------------------------------------- |
|---|
| 8 |
|
|---|
| 9 |
-- import dependencies |
|---|
| 10 |
local LUObject = require( "LUObject" ) |
|---|
| 11 |
local LURunLoop = require( "LURunLoop" ) |
|---|
| 12 |
local LULog = require( "LULog" ) |
|---|
| 13 |
local coroutine = require( "coroutine" ) |
|---|
| 14 |
|
|---|
| 15 |
-- define the class |
|---|
| 16 |
local super = LUObject |
|---|
| 17 |
local self = super() |
|---|
| 18 |
|
|---|
| 19 |
-- class variable(s) |
|---|
| 20 |
local _currentTask = nil |
|---|
| 21 |
|
|---|
| 22 |
-- method to access the current task |
|---|
| 23 |
function self:currentTask() |
|---|
| 24 |
return _currentTask |
|---|
| 25 |
end |
|---|
| 26 |
|
|---|
| 27 |
-- initialization method |
|---|
| 28 |
function self:init( aTarget, aRunLoop ) |
|---|
| 29 |
self = super.init( self ) |
|---|
| 30 |
|
|---|
| 31 |
self._target = aTarget |
|---|
| 32 |
self._runloop = aRunLoop |
|---|
| 33 |
|
|---|
| 34 |
return self |
|---|
| 35 |
end |
|---|
| 36 |
|
|---|
| 37 |
-- method to create a (co)routine |
|---|
| 38 |
function self:routine() |
|---|
| 39 |
if self._routine == nil then |
|---|
| 40 |
local aFunction = function() |
|---|
| 41 |
self:target():run() |
|---|
| 42 |
end |
|---|
| 43 |
|
|---|
| 44 |
self._routine = coroutine.create( aFunction ) |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
return self._routine |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|
| 50 |
-- method to 'run' the coroutine by registering it with the class run loop |
|---|
| 51 |
function self:run() |
|---|
| 52 |
if self._routine == nil then |
|---|
| 53 |
self:routine() |
|---|
| 54 |
|
|---|
| 55 |
self:runloop():addTask( self ) |
|---|
| 56 |
end |
|---|
| 57 |
|
|---|
| 58 |
return self |
|---|
| 59 |
end |
|---|
| 60 |
|
|---|
| 61 |
-- method to access this task run loop |
|---|
| 62 |
function self:runloop() |
|---|
| 63 |
if self._runloop == nil then |
|---|
| 64 |
self._runloop = LURunLoop:default() |
|---|
| 65 |
end |
|---|
| 66 |
|
|---|
| 67 |
return self._runloop |
|---|
| 68 |
end |
|---|
| 69 |
|
|---|
| 70 |
-- method to access this task target |
|---|
| 71 |
function self:target() |
|---|
| 72 |
return self._target |
|---|
| 73 |
end |
|---|
| 74 |
|
|---|
| 75 |
-- method to return the coroutine status |
|---|
| 76 |
function self:status() |
|---|
| 77 |
local aRoutine = self._routine |
|---|
| 78 |
|
|---|
| 79 |
if aRoutine ~= nil then |
|---|
| 80 |
return coroutine.status( aRoutine ) |
|---|
| 81 |
end |
|---|
| 82 |
|
|---|
| 83 |
return nil |
|---|
| 84 |
end |
|---|
| 85 |
|
|---|
| 86 |
-- method to resume the coroutine |
|---|
| 87 |
function self:resume( ... ) |
|---|
| 88 |
local aRoutine = self._routine |
|---|
| 89 |
|
|---|
| 90 |
if aRoutine ~= nil then |
|---|
| 91 |
_currentTask = self |
|---|
| 92 |
|
|---|
| 93 |
local aStatus, aMessage = coroutine.resume( aRoutine, ... ) |
|---|
| 94 |
|
|---|
| 95 |
_currentTask = nil |
|---|
| 96 |
|
|---|
| 97 |
if aStatus == false then |
|---|
| 98 |
LULog:warning( aMessage ) |
|---|
| 99 |
end |
|---|
| 100 |
|
|---|
| 101 |
return aStatus, aMessage |
|---|
| 102 |
end |
|---|
| 103 |
|
|---|
| 104 |
return nil |
|---|
| 105 |
end |
|---|
| 106 |
|
|---|
| 107 |
-- method to yield the coroutine |
|---|
| 108 |
function self:yield( ... ) |
|---|
| 109 |
return coroutine.yield( ... ) |
|---|
| 110 |
end |
|---|
| 111 |
|
|---|
| 112 |
return self |
|---|