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