1: \ Multitasker 19aug94py
2:
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:
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: :noname ' >body @ ;
45: :noname ' >body @ postpone literal ;
46: interpret/compile: user' ( 'user' -- n )
47:
48: \ NEWTASK creates a new, sleeping task
49: : NewTask ( n -- Task ) dup 2* 2* udp @ + dup
50: allocate throw + >r
51: r@ over - udp @ - next-task over udp @ move
52: r> over user' rp0 + ! dup >r
53: dup r@ user' lp0 + ! over -
54: dup r@ user' fp0 + ! over -
55: dup r@ user' sp0 + ! over -
56: dup r@ user' normal-dp + dup >r !
57: r> r@ user' dpp + ! + $10 +
58: r@ user' >tib + !
59: r> dup 2dup 2! dup sleep ;
60:
61: : kill-task
62: next-task @ up! save-task @ sp!
63: lp! fp! rp! prev-task @ dup dup link-task user' normal-dp + @ free throw ;
64:
65: : (pass) ( x1 .. xn n task -- ) rdrop
66: [ ' kill-task >body ] ALiteral r>
67: rot >r r@ user' rp0 + @ 2 cells - dup >r 2!
68: r> swap 1+
69: r@ user' fp0 + @ swap 1+
70: r@ user' lp0 + @ swap 1+
71: cells r@ user' sp0 + @ tuck swap - dup r@ user' save-task + !
72: ?DO I ! cell +LOOP r> wake ;
73:
74: : activate ( task -- ) 0 swap (pass) ;
75: : pass ( x1 .. xn n task -- ) (pass) ;
76:
77: : single-tasking? ( -- flag )
78: next-task dup @ = ;
79:
80: : task-key BEGIN pause key? single-tasking? or UNTIL (key) ;
81: : task-emit (emit) pause ;
82: : task-type (type) pause ;
83:
84: ' task-key IS key
85: ' task-emit IS emit
86: ' task-type IS type
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>