1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
/*
* SPL - The SPL Programming Language
* Copyright (C) 2004, 2005 Clifford Wolf <clifford@clifford.at>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* mod_task.spl: Simple task management library
*/
var __task_list;
var __task_late_kill_list;
function __task_late_kill_check()
{
foreach t (__task_late_kill_list)
if (!task_check(t)) {
delete __task_list[t];
delete __task_late_kill_list[t];
}
}
/**
* This function kills the specified task.
*
* If the task name is ommitted, the current task will be affected by
* this function.
*/
function task_kill(name)
{
__task_late_kill_check();
if (defined name)
delete __task_list.[name];
else
delete __task_list.[task_getname()];
__task_kill(name);
task_pause(name);
}
/**
* This function schedules the specified task to be killed. The task
* will be killed when becomes paused the next time (e.g. because
* [[task_pause()]] is called on it). If the task is already paused,
* it will be killed emediatly.
*
* If the task name is ommitted, the current task will be affected by
* this function.
*/
function task_late_kill(name)
{
__task_late_kill_check();
if (defined name)
__task_late_kill_list.[name] = 1;
else
__task_late_kill_list.[task_getname()] = 1;
__task_kill(name);
}
/**
* This function pauses the current task and wakes up the specified one.
* It is using [[task_system()]] to make the operation atomic and so avoid
* race conditions.
*/
function task_switch(name)
{
__task_late_kill_check();
task_system();
task_continue(name);
task_pause();
}
/**
* This function is like, [[task_switch()]] but registers the current task as
* co-routine caller. That means, when the specified task call
* [[task_co_return()]], the calling task will be woken up again and the
* parameter to [[task_co_return()]] will be returned by this function.
*/
function task_co_call(name)
{
__task_late_kill_check();
if (declared __task_list.[name].["co_caller"])
panic "Trying to run already running co routine '$name'.";
__task_list.[name].["co_caller"] = task_getname();
task_switch(name);
var retval = __task_list.[name].["co_retval"];
delete __task_list.[name].["co_caller"];
delete __task_list.[name].["co_retval"];
return retval;
}
/**
* Return to the task which switched to this task using [[task_co_call()]]. If
* the current task was not entered using this mechanism, control is passed to
* the task specified by [[task_co_setdefault()]], or a runtime error is
* triggered if no default has been defined.
*/
function task_co_return(retval)
{
__task_late_kill_check();
if (not declared __task_list.[task_getname()].["co_caller"])
{
if (not declared __task_list.[task_getname()].["co_default"])
panic "Trying to co_return from a task which isn't a co routine.";
else
task_switch(__task_list.[task_getname()].["co_default"]);
}
else
{
__task_list.[task_getname()].["co_retval"] = retval;
task_switch(__task_list.[task_getname()].["co_caller"]);
}
}
/**
* Set the task "def" as default task to be returned when the task "name"
* calls [[task_co_return()]] was not entered thru [[task_co_call()]].
*/
function task_co_setdefault(name, def)
{
__task_late_kill_check();
__task_list.[name].["co_default"] = def;
}
/**
* An eval, implemented as own task. It is recommended to use the "eval"
* statement instead. The only difference is that this function allows you to
* specify a different function context in which the code should be executed.
*/
function task_eval(code, ctx)
{
__task_late_kill_check();
task_create("${task_getname()}_eval", "task_co_return( ({ ${code} }) );", ctx);
return task_co_call("${task_getname()}_eval");
}
|