[ loop | defer | atmos.env | atmos.status ]
Calls the given body as a task, passing control to Atmos.
- Parameters:
f: function| body as a function
- Returns:
...| return values from the task
The loop returns when the given body terminates.
Executes the given function when the enclosing scope terminates.
- Parameters:
f: function| function to execute
- Returns:
: table| special table that requires a<close>assignment
A defer requires a <close> assignment in the scope of interest:
do
local _ <close> = defer(<...>)
<...>
end
Registers an environment table with Atmos.
- Parameters:
(This function is only used internally by environments.)
Returns the status of the given task instance.
- Parameters:
tsk: xtask| task instance to check
- Returns:
: string| task status: 'running', 'suspended', 'normal', 'dead'
[ task(f) | xtask() | xtask(T) | tasks | abort | spawn | spawn_in | do_spawn | toggle ]
A task prototype (task) is an abstract, spawnable definition.
A task instance (xtask) is a spawned protoype with its own identity.
A pool of task instances (tasks) holds a set of spawned prototypes.
Task values render distinctly: task: 0x... (prototype), xtask: 0x...
(instance), tasks: 0x... (pool).
Creates a task prototype from a function.
- Parameters:
f: function| task body as a function
- Returns:
: task| task prototype
Returns a self-reference to the running task instance.
- Parameters:
- none
- Returns:
: xtask| running task instance
Creates a task instance from a prototype.
- Parameters:
T: task| task prototype
- Returns:
: xtask| task instance
Creates a task pool.
- Parameters:
n: number| maximum number of instances
- Returns:
: tasks| task pool
Aborts a task instance or task pool.
- Parameters:
t: xtask|tasks| task instance or task pool to abort
- Returns:
nil
All nested tasks are also aborted. All nested deferred blocks execute.
Spawns a prototype as a task instance with its own identity.
- Parameters:
t: task | xtask| prototype to instantiate, or a pre-built instance to start...| extra arguments to pass to the body
- Returns:
: xtask| task instance just spawned
Spawns a task prototype into a pool.
- Parameters:
tsks: tasks| pool to spawn intot: task| task prototype...| extra arguments to pass to the body
- Returns:
: xtask| task instance just spawned (ornilif the pool is full)
Spawns a raw function as an anonymous transparent nested task.
- Parameters:
f: function| inline task body...| extra arguments to pass to the body
- Returns:
: handle| close-only lifetime handle
A transparent task has no identity, so the return is a close-only handle: it cannot be awaited, toggled, or aborted, but it can bind the body to a lexical block:
do
local _ <close> = do_spawn(function () ... end) -- aborted at block end
...
end
Toggles a task instance (or pool) on and off.
- Parameters:
tsk: xtask | tasks| task instance or pool to toggleon: boolean| toggle on (true) or off (false)
- Returns:
nil
Emits an event.
- Parameters:
e| event to emit
- Returns
nil
Emits an event into a target.
- Parameters:
to| emit targete| event to emit
- Returns
nil
The event target determines the scope of tasks affected by the emit. The following values are accepted as target:
number| level above in the task hierarchy0| current task1| parent task2| parent of parent taskn| (n times) parent of task
nilor'task'| equivalent to0'global'| all top-level tasks: task| the given task
Awaits an event pattern in the running task.
- Parameters:
pat| event pattern...| spawn arguments (ifpatis a task)
- Returns:
e| argument of matching emit
The task awakes when an emit(e) matches the given await pattern as follows:
| Group | Pattern | matches | returns |
|---|---|---|---|
| Boolean | true |
any event | e |
false |
never | — | |
| Value | {tag=t,...} |
X.gte(pat,e) |
e |
x: any |
X.is(e,x) |
e |
|
| Time | us: number |
timeout | late |
'clock' |
clock tick | dt,late |
|
| Tasks | t: xtask |
t end |
v,t |
T: task |
T end | v,t |
|
{tag='spawn',T,...} |
T end | v,t |
|
{tag='tasks',mode='any',tasks=ts} |
any pool end | v,t,ts |
|
{tag='tasks',mode='all',tasks=ts} |
all pool end | v,t,ts |
|
| Stream | s: stream |
s ends |
v,t |
| Condition | {tag='until', [p,] c} |
[p] until c | e / res |
{tag='while', [p,] c} |
[p] while c | e |
|
| Logical | {tag='not',x} |
not p |
e |
{tag='and',...} |
all subs | e |
|
{tag='or',...} |
any sub | e |
|
| Meta | mt: meta |
via __atmos |
e / res |
Note that some patterns may modify the final result:
- Time:
late: time elapsed past expiry;dt: clock tick length - Tasks: task result, terminating task, and task pool
- Condition, Meta: function result (defaults to
eiftrue)
If pat is a task prototype T, then it expands to {tag='spawn', T, ...}.
TODO
- true, false, function
- list
is
TODO
[ loop_on | watching | toggle | par | par_all | par_any ]
Compound statements combine tasks, awaits, and other primitive to provide higher-level constructs.
Executes the given body, in a loop, after every occurrence of the given event pattern.
- Parameters:
...| event patternf: function| loop body as a function
- Returns:
- never returns
A loop_on is equivalent to the code as follows:
while true do
f(await(...))
end
Executes the given body until it terminates or until the given event pattern occurs.
- Parameters:
...| event patternf: function| body as a function
- Returns:
...| return values of the body or matching event
A watching is equivalent to the call as follows:
par_any(function() return await(...) end, f)
Executes the given body until it terminates. Meanwhile, toggles it on and off based on occurrences of the given event.
- Parameters:
evt| boolean eventfilter| (optional) always-on await patternf: function| body as a function
- Returns:
...| return values of the body
A task that is toggled off does not react to upcoming events, except those matching the optional filter.
Spawn the given bodies and never terminates.
- Parameters:
...| tasks to spawn as functions
- Returns:
- never returns
Spawn the given bodies and terminate when all of them terminate.
- Parameters:
...| tasks to spawn as functions
- Returns:
...: return value of tasks (first per task)
Spawn the given bodies and terminate when any of them terminates.
- Parameters:
...| tasks to spawn as functions
- Returns:
...: return values of the terminating task
Basic documentation:
Extensions:
local S = require "atmos.streams"
-
Sources
S.on (pat): stream of await patterns
-
Combinators
S.emitter(s,tgt,as): emit each value ofsasasin targettgt(optional)S.par(...): merges all streams...into a single streamS.xpar(...): merges all streams of streams...into a single streamS.parany(...): likeparbut terminates when any of the streams...terminatesS.xparany(...): likeparanybut terminates when any of the streams returned in...terminates
TODO: parall / xparall
Spawns a function to execute in an OS thread and awaits its termination.
- Parameters:
f: function| function to execute in a separate thread
- Returns:
...| function return values
The function receives copies of its upvalues, but cannot access Atmos
primitives (await, emit, spawn).
The module atmos.x provides value relations and helpers:
local X = require "atmos.x"
| function | result | meaning |
|---|---|---|
X.gte(a,b) |
boolean |
a is a supertype of/conforms to b |
X.eq(a,b) |
boolean |
deep equality: gte(a,b) and gte(b,a) |
X.is(v,x) |
boolean |
v is-a x (identity / type / tag / kind) |
X.xin(v,t) |
boolean |
v is a member of t (as key or value) |
X.cat(a,b) |
str/table |
a .. b, else a new table merging both |
X.iter(t,...) |
iterator |
generic-for over t (arity below) |
X.tostring(v) |
string |
stable rendering (table keys sorted) |
X.print(...) |
— | print via X.tostring |
X.copy(v) |
value |
deep copy |
- TODO
X.gte:a == b, same types/metas, subtags, subsumes fieldsX.is: type name, prototask/ instancextask/ pooltasks, table tagX.iter: arity by source- 1:
number/(n,m)/nil - 2:
table/__pairs - n:
function/__call
- 1: