File:  [gforth] / gforth / tasker.fs
Revision 1.10: download - view: text, annotated - select for diffs
Tue Jun 12 10:49:40 2001 UTC (18 years, 1 month ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Fixed kill-task
Added glossary entries

    1: \ Multitasker                                          19aug94py
    2: 
    3: \ Copyright (C) 1995,1996,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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   20: 
   21: Create sleepers  sleepers A, sleepers A, 0 ,
   22: 
   23: : link-task ( task1 task2 -- )
   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 ;
   34: 
   35: : pause ( -- )
   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! ;
   40: 
   41: : stop ( -- )
   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 ;
   46: 
   47: :noname    ' >body @ ;
   48: :noname    ' >body @ postpone literal ; 
   49: interpret/compile: user' ( 'user' -- n )
   50: \G USER' computes the task offset of a user variable
   51: 
   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
   62:     0 r@ user' current-input + !
   63:     r> dup 2dup 2! dup sleep ;
   64: 
   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 ;
   75: 
   76: : (pass) ( x1 .. xn n task -- )  rdrop
   77:   [ ' kill-task >body ] ALiteral r>
   78:   rot >r r@ user' rp0 + @ 2 cells - dup >r 2!
   79:   r>              swap 1+
   80:   r@ user' fp0 + @ swap 1+
   81:   r@ user' lp0 + @ swap 1+
   82:   cells r@ user' sp0 + @ tuck swap - dup r@ user' save-task + !
   83:   ?DO  I !  cell  +LOOP  r> wake ;
   84: 
   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) ;
   93: 
   94: : single-tasking? ( -- flag )
   95:     \G checks if only one task is running
   96:     next-task dup @ = ;
   97: 
   98: : task-key   BEGIN  pause key? single-tasking? or  UNTIL  (key) ;
   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>