[gforth] / gforth / tasker.fs  

gforth: gforth/tasker.fs


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

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help