Annotation of gforth/tasker.fs, revision 1.5

1.1       pazsan      1: \ Multitasker                                          19aug94py
                      2: 
1.4       anton       3: \ Copyright (C) 1995-1997 Free Software Foundation, Inc.
                      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
                     19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
                     20: 
1.1       pazsan     21: Create sleepers  sleepers A, sleepers A, 0 ,
                     22: 
                     23: \ LINK-TASK links task1 into the task chain of task2
                     24: : link-task ( task1 task2 -- )
                     25:   over 2@  2dup cell+ ! swap !  \ unlink task1
                     26:   2dup @ cell+ !  2dup dup @ rot 2!  ! ;
                     27: 
                     28: : sleep ( task -- )  sleepers  link-task ;
                     29: : wake  ( task -- )  next-task link-task ;
                     30: 
                     31: \ PAUSE is the task-switcher
                     32: : pause ( -- )
                     33:   rp@ fp@ lp@ sp@ save-task !
                     34:   next-task @ up! save-task @ sp!
                     35:   lp! fp! rp! ;
                     36: 
                     37: \ STOP sleeps a task and switches to the next
                     38: : stop ( -- )
                     39:   rp@ fp@ lp@ sp@ save-task !
                     40:   next-task @ up! save-task @ sp!
                     41:   lp! fp! rp! prev-task @ sleep ;
                     42: 
                     43: \ USER' computes the task offset
                     44: : user' ( 'user' -- n )
1.3       anton      45:     ' >body @ postpone literal ; immediate
                     46: interpretation:
                     47:     ' >body @ ;
1.1       pazsan     48: 
                     49: \ NEWTASK creates a new, sleeping task
                     50: : NewTask ( n -- Task )  dup 2* 2* udp @ + dup
                     51:   allocate throw  + >r
                     52:   r@ over - udp @ - next-task over udp @ move
1.5     ! jwilke     53:   r> over user' rp0 + ! dup >r
        !            54:   dup r@ user' lp0   + ! over -
        !            55:   dup r@ user' fp0   + ! over -
        !            56:   dup r@ user' sp0   + ! over -
1.1       pazsan     57:   dup r@ user' normal-dp + dup >r !
                     58:    r> r@ user' dpp  + ! + $10 +
                     59:       r@ user' >tib + !
                     60:   r> dup 2dup 2! dup sleep ;
                     61: 
                     62: : kill-task
                     63:   next-task @ up! save-task @ sp!
                     64:   lp! fp! rp! prev-task @ dup dup link-task user' normal-dp + @ free throw ;
                     65: 
                     66: : (pass) ( x1 .. xn n task -- )  rdrop
                     67:   [ ' kill-task >body ] ALiteral r>
1.5     ! jwilke     68:   rot >r r@ user' rp0 + @ 2 cells - dup >r 2!
1.1       pazsan     69:   r>              swap 1+
1.5     ! jwilke     70:   r@ user' fp0 + @ swap 1+
        !            71:   r@ user' lp0 + @ swap 1+
        !            72:   cells r@ user' sp0 + @ tuck swap - dup r@ user' save-task + !
1.1       pazsan     73:   ?DO  I !  cell  +LOOP  r> wake ;
                     74: 
                     75: : activate ( task -- )  0 swap (pass) ;
                     76: : pass ( x1 .. xn n task -- )  (pass) ;
                     77: 
1.2       pazsan     78: : single-tasking? ( -- flag )
                     79:     next-task dup @ = ;
                     80: 
                     81: : task-key   BEGIN  pause key? single-tasking? or  UNTIL  (key) ;
1.1       pazsan     82: : task-emit  (emit) pause ;
                     83: : task-type  (type) pause ;
                     84: 
                     85: ' task-key  IS key
                     86: ' task-emit IS emit
                     87: ' task-type IS type

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