Annotation of gforth/tasker.fs, revision 1.12

1.1       pazsan      1: \ Multitasker                                          19aug94py
                      2: 
1.12    ! anton       3: \ Copyright (C) 1995,1996,1997,2001,2003 Free Software Foundation, Inc.
1.4       anton       4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
                      9: \ as published by the Free Software Foundation; either version 2
                     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
                     18: \ along with this program; if not, write to the Free Software
1.8       anton      19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.4       anton      20: 
1.1       pazsan     21: Create sleepers  sleepers A, sleepers A, 0 ,
                     22: 
                     23: : link-task ( task1 task2 -- )
1.10      pazsan     24:     \G LINK-TASK links task1 into the task chain of task2
                     25:     over 2@  2dup cell+ ! swap !  \ unlink task1
                     26:     2dup @ cell+ !  2dup dup @ rot 2!  ! ;
                     27: 
                     28: : sleep ( task -- )
                     29:     \G deactivates task
                     30:     sleepers  link-task ;
                     31: : wake  ( task -- )
                     32:     \G activates task
                     33:     next-task link-task ;
1.1       pazsan     34: 
                     35: : pause ( -- )
1.10      pazsan     36:     \G PAUSE is the task-switcher
                     37:     rp@ fp@ lp@ sp@ save-task !
                     38:     next-task @ up! save-task @ sp!
                     39:     lp! fp! rp! ;
1.1       pazsan     40: 
                     41: : stop ( -- )
1.10      pazsan     42:     \G STOP sleeps a task and switches to the next
                     43:     rp@ fp@ lp@ sp@ save-task !
                     44:     next-task @ up! save-task @ sp!
                     45:     lp! fp! rp! prev-task @ sleep ;
1.1       pazsan     46: 
1.6       jwilke     47: :noname    ' >body @ ;
                     48: :noname    ' >body @ postpone literal ; 
                     49: interpret/compile: user' ( 'user' -- n )
1.10      pazsan     50: \G USER' computes the task offset of a user variable
1.1       pazsan     51: 
1.10      pazsan     52: : NewTask ( stacksize -- Task )  dup 2* 2* udp @ + dup
                     53:     \G NEWTASK creates a new, sleeping task
                     54:     allocate throw  + >r
                     55:     r@ over - udp @ - next-task over udp @ move
                     56:     r> over user' rp0 + ! dup >r
                     57:     dup r@ user' lp0   + ! over -
                     58:     dup r@ user' fp0   + ! over -
                     59:     dup r@ user' sp0   + ! over -
                     60:     dup r@ user' normal-dp + dup >r !
                     61:     r> r@ user' dpp  + ! 2drop
1.9       pazsan     62:     0 r@ user' current-input + !
1.10      pazsan     63:     r> dup 2dup 2! dup sleep ;
1.1       pazsan     64: 
1.10      pazsan     65: Create killer killer A, killer A,
                     66: : kill ( task -- )
                     67:     \G kills a task - deactivate and free task area
                     68:     dup killer link-task  killer dup dup 2!
                     69:     user' normal-dp + @ free throw ;
                     70: 
                     71: : kill-task ( -- )
                     72:     \G kills the current task, also on bottom of return stack of a new task
                     73:     next-task @ up! save-task @ sp!
                     74:     lp! fp! rp! prev-task @ kill ;
1.1       pazsan     75: 
                     76: : (pass) ( x1 .. xn n task -- )  rdrop
                     77:   [ ' kill-task >body ] ALiteral r>
1.5       jwilke     78:   rot >r r@ user' rp0 + @ 2 cells - dup >r 2!
1.1       pazsan     79:   r>              swap 1+
1.5       jwilke     80:   r@ user' fp0 + @ swap 1+
                     81:   r@ user' lp0 + @ swap 1+
                     82:   cells r@ user' sp0 + @ tuck swap - dup r@ user' save-task + !
1.1       pazsan     83:   ?DO  I !  cell  +LOOP  r> wake ;
                     84: 
1.10      pazsan     85: : activate ( task -- )
                     86:     \G activates the task.
                     87:     \G Continues execution with the caller of ACTIVATE.
                     88:     0 swap (pass) ;
                     89: : pass ( x1 .. xn n task -- )
                     90:     \G passes n parameters to the task and activates that task.
                     91:     \G Continues execution with the caller of PASS.
                     92:     (pass) ;
1.1       pazsan     93: 
1.2       pazsan     94: : single-tasking? ( -- flag )
1.10      pazsan     95:     \G checks if only one task is running
1.2       pazsan     96:     next-task dup @ = ;
                     97: 
                     98: : task-key   BEGIN  pause key? single-tasking? or  UNTIL  (key) ;
1.1       pazsan     99: : task-emit  (emit) pause ;
                    100: : task-type  (type) pause ;
                    101: 
                    102: ' task-key  IS key
                    103: ' task-emit IS emit
                    104: ' task-type IS type

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>