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 task ''%%task%%'' and an execution token ''%%xt%%'', 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 task ''%%task%%''. Used in the form ''%% his%%'' to translate from the current task’s user variable address to that of ''%%task%%''. * **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?%%'', and ''%%key%%'') to their multitasking counterparts (''%%multi-ms%%'', ''%%multi-emit%%'', ''%%multi-key?%%'', and ''%%multi-key%%''). * **operator**: ''%%( -- task )%%''\\ A predefined task structure for the foreground task. * **pass** ''%%( x1 ... xn task n -- )%%''\\ Given ''%%n%%'' values, a task ''%%task%%'', and the number ''%%n%%'', it pushes these ''%%n%%'' 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 variable ''%%task-state%%'' set to ''%%true%%''. * **single** (''%%( -- )%%'')\\ Reverts the system behavior from multitasking mode back to a standard, single-tasking mode. * **sleep** ''%%( task -- )%%''\\ Given a task ''%%task%%'', it sets the task’s state to sleeping, i.e. the user variable ''%%task-state%%'' to ''%%false%%''. * **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 ID ''%%task%%''. * **tasks** ''%%( -- )%%''\\ Displays the current state of all tasks, including their task ID, state (''%%ready%%'' or ''%%sleeping%%''), link, error number, stack pointer (''%%sp%%''), and return stack pointer (''%%rp%%''). * **unlock** ''%%( addr -- )%%''\\ Releases the lock from a semaphore address ''%%addr%%'' by setting its value to 0. * **User** ''%%( x -- )%%''\\ Creates a new user variable at a position ''%%x%%'' relative to the user pointer (''%%up@%%''). * **wake** ''%%( task -- )%%''\\ Given a task ''%%task%%'', it sets the task’s state to ready (i.e., not sleeping, user variable ''%%task-state%%'' to ''%%true%%''). ==== Extra words ==== * **rp!** ''%%( x -- )%%''\\ Sets the Return Stack Pointer (RP) from the value ''%%x%%'' on the stack. * **rp@** ''%%( -- x )%%''\\ Fetches the current value of the Return Stack Pointer (RP) and leaves it as ''%%x%%'' on the stack. * **sp!** ''%%( x -- )%%''\\ Sets the Data Stack Pointer (SP) from the value ''%%x%%'' on the stack. * **sp@** ''%%( -- x )%%''\\ Fetches the current value of the Data Stack Pointer (SP) and leaves it as ''%%x%%'' on the stack. * **up!** ''%%( x -- )%%''\\ Sets the User Pointer (UP, which can be a register labeled ZZ) from the value ''%%x%%'' on the stack. * **up@** ''%%( -- x )%%''\\ Fetches the current value of the User Pointer (UP) and leaves it as ''%%x%%'' 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).