en:pfw:muti_tasker
Table of Contents
This idea will explain how a typical cooperative Forth multi tasker works.
For now here is a glossary of the noForth multi tasker (in development):
noForth multitasker glossary
2023-09-04
Multi Tasker Words
- activate
( xt task -- )
Given a tasktask
and an execution tokenxt
, make the task execute that token and wake up the task. If an error is cought within the task, the throw code is stored in the task’s ´error#´ user variable and the task is stopped. - his
( task addr -- addr' )
Allows to access a user variable of tasktask
. Used in the form<task> <user-variable> his
to translate from the current task’s user variable address to that oftask
. - lock
( addr -- )
This word takes an address of a semaphore variable that controls exclusive access to a ressource. It locks access to it by checking if the semaphore is owned by the current task. If not, the task pauses until it gains access and then locks the address by storing its task ID. - multi (
multi ( -- )
)
Adjusts the system behavior to a multitasking mode. It reassigns the behavior of some system words (ms
,emit
,key?
, andkey
) to their multitasking counterparts (multi-ms
,multi-emit
,multi-key?
, andmulti-key
). - operator:
( -- task )
A predefined task structure for the foreground task. - pass
( x1 ... xn task n -- )
Givenn
values, a tasktask
, and the numbern
, it pushes thesen
values onto the task’s stack. - pause
( -- )
Saves the current task’s state, then looks for other tasks to run. It resumes the original task when the task’s state indicates it should run again, i.a. its user variabletask-state
set totrue
. - single (
( -- )
)
Reverts the system behavior from multitasking mode back to a standard, single-tasking mode. - sleep
( task -- )
Given a tasktask
, it sets the task’s state to sleeping, i.e. the user variabletask-state
tofalse
. - stop
( -- )
Makes the current task sleep and then passes control on, effectively ending the task’s execution. - task
( stacksize rstacksize -- task )
Creates a new task with a given stack size and return stack size, returning the task IDtask
. - tasks
( -- )
Displays the current state of all tasks, including their task ID, state (ready
orsleeping
), link, error number, stack pointer (sp
), and return stack pointer (rp
). - unlock
( addr -- )
Releases the lock from a semaphore addressaddr
by setting its value to 0. - User
( x -- )
Creates a new user variable at a positionx
relative to the user pointer (up@
). - wake
( task -- )
Given a tasktask
, it sets the task’s state to ready (i.e., not sleeping, user variabletask-state
totrue
).
Extra words
- rp!
( x -- )
Sets the Return Stack Pointer (RP) from the valuex
on the stack. - rp@
( -- x )
Fetches the current value of the Return Stack Pointer (RP) and leaves it asx
on the stack. - sp!
( x -- )
Sets the Data Stack Pointer (SP) from the valuex
on the stack. - sp@
( -- x )
Fetches the current value of the Data Stack Pointer (SP) and leaves it asx
on the stack. - up!
( x -- )
Sets the User Pointer (UP, which can be a register labeled ZZ) from the valuex
on the stack. - up@
( -- x )
Fetches the current value of the User Pointer (UP) and leaves it asx
on the stack.
Task Area / User Variables
- error#
A user variable representing the task’s last error number. Within a task structure,error#
is used to store the last error number/trow code that was raised during the task’s execution. If a task encounters an error, this value will be updated to represent the specific error code and the task will halt. - task-link
A user variable that stores a link to the next task. - task-size
A constant that represents the size of a task’s control block in memory. - task-state
A boolean user variable that indicates the current state of the task (true
for ready,false
for sleeping).
en/pfw/muti_tasker.txt · Last modified: 2023-09-04 19:30 by uho