en:pfw:muti_tasker
**This is an old revision of the document!**
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:
noForth multitasker glossary
2023-09-04
Multi Tasker Words
- activate
( xt task -- )
Given a tasktaskand 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> histo 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 -- )
Givennvalues, a tasktask, and the numbern, it pushes thesenvalues 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-stateset 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-statetofalse. - 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 (readyorsleeping), link, error number, stack pointer (sp), and return stack pointer (rp). - unlock
( addr -- )
Releases the lock from a semaphore addressaddrby setting its value to 0. - User
( x -- )
Creates a new user variable at a positionxrelative to the user pointer (up@). - wake
( task -- )
Given a tasktask, it sets the task’s state to ready (i.e., not sleeping, user variabletask-statetotrue).
Extra words
- rp!
( x -- )
Sets the Return Stack Pointer (RP) from the valuexon the stack. - rp@
( -- x )
Fetches the current value of the Return Stack Pointer (RP) and leaves it asxon the stack. - sp!
( x -- )
Sets the Data Stack Pointer (SP) from the valuexon the stack. - sp@
( -- x )
Fetches the current value of the Data Stack Pointer (SP) and leaves it asxon the stack. - up!
( x -- )
Sets the User Pointer (UP, which can be a register labeled ZZ) from the valuexon the stack. - up@
( -- x )
Fetches the current value of the User Pointer (UP) and leaves it asxon 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 (truefor ready,falsefor sleeping).
en/pfw/muti_tasker.1693848596.txt.gz · Last modified: 2023-09-04 19:29 by uho