Skip to content

Commit

Permalink
tasks refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroflag committed Dec 17, 2017
1 parent 9e3ca57 commit 1129430
Showing 1 changed file with 40 additions and 54 deletions.
94 changes: 40 additions & 54 deletions arch/esp8266/forth/tasks.forth
Original file line number Diff line number Diff line change
Expand Up @@ -18,104 +18,90 @@ SKIPPED INTERPRETER .status !
_s0 INTERPRETER .s0 !
_r0 INTERPRETER .r0 !

112 init-variable: var-task-stack-size
112 init-variable: var-task-rstack-size
INTERPRETER init-variable: var-last-task
INTERPRETER init-variable: var-current-task
112 init-variable: task-stack-size
112 init-variable: task-rstack-size
INTERPRETER init-variable: last
INTERPRETER init-variable: current

: last-task ( -- task ) var-last-task @ ;
: last-task! ( task -- ) var-last-task ! ;
: current-task ( -- task ) var-current-task @ ;
: current-task! ( task -- ) var-current-task ! ;

: alloc-data-stack ( -- a ) var-task-stack-size @ allot here ;
: alloc-return-stack ( -- a ) var-task-rstack-size @ allot here ;
: alloc-stack ( -- a ) task-stack-size @ allot here ;
: alloc-rstack ( -- a ) task-rstack-size @ allot here ;

: task: ( user-space-size "name" ) ( -- task )
create:
here \ task header begins here
swap Task + allot \ make room for task header + user space
SKIPPED over .status ! \ new status is SKIPPED
last-task .next @ over .next ! \ this.next = last-task.next
dup last-task .next ! \ last-task.next = this
alloc-data-stack over .sp ! \ this.sp = allocated
alloc-return-stack over .rp ! \ this.sp = allocated
last @ .next @ over .next ! \ this.next = last-task.next
dup last @ .next ! \ last-task.next = this
alloc-stack over .sp ! \ this.sp = allocated
alloc-rstack over .rp ! \ this.sp = allocated
0 over .handler ! \ exception handler of this thread
dup .sp @ over .s0 ! \ init s0 = top of stack address
dup .rp @ over .r0 ! \ init r0 = top of rstack address
last-task! ; \ last-task = this

: task-choose-next ( -- ) current-task begin .next @ dup .status @ PAUSED = until ;
last ! ; \ last-task = this

: task-save-context ( sp ip rp -- ) \ XXX temporal coupling
current-task .rp !
current-task .ip !
current-task .sp ! ;
: choose ( -- ) current @ begin .next @ dup .status @ PAUSED = until ;

: task-restore-context ( -- )
current-task .sp @ sp!
current-task .rp @ rp!
current-task .ip @ >r ;
: save ( sp ip rp -- ) \ XXX temporal coupling
current @ .rp !
current @ .ip !
current @ .sp ! ;

: task-run ( task -- )
current-task!
SKIPPED current-task .status !
task-restore-context ;
: restore ( -- )
current @ .sp @ sp!
current @ .rp @ rp!
current @ .ip @ >r ;

: task-user-space ( task -- a ) Task + ;
: switch ( task -- )
current !
SKIPPED current @ .status !
restore ;

: user-space ( -- a ) current-task task-user-space ;
: user-space ( -- a ) current @ Task + ;

defer: pause

: pause-multi ( -- )
PAUSED current-task .status !
sp@ r> rp@ task-save-context
task-choose-next task-run ;

: pause-single ( -- ) ;
PAUSED current @ .status !
sp@ r> rp@ save
choose switch ;

: s0-multi ( -- top-stack-adr ) current-task .s0 @ ;
: r0-multi ( -- top-rstack-adr ) current-task .r0 @ ;
: s0-multi ( -- top-stack-adr ) current @ .s0 @ ;
: r0-multi ( -- top-rstack-adr ) current @ .r0 @ ;

' s0 is: s0-multi
' r0 is: r0-multi

: activate ( task -- )
r> over .ip !
PAUSED current-task .status ! \ pause current task
sp@ cell + r> rp@ task-save-context
task-run ;
PAUSED current @ .status ! \ pause current task
sp@ cell + r> rp@ save
switch ;

: stop ( task -- )
SKIPPED swap .status !
task-choose-next task-run ;

: deactivate ( -- ) current-task stop ;
: stop ( task -- ) SKIPPED swap .status ! choose switch ;
: deactivate ( -- ) current @ stop ;

: task-find ( task -- link )
lastword
begin
dup
while
2dup
link>body cell + = if \ XXX skip behaviour pointer
nip exit
then
link>body cell + = if nip exit then \ XXX skip behaviour pointer
@
repeat
2drop 0 ;

: tasks-print ( -- )
current-task
current @
begin
dup task-find ?dup if
link-type cr
else
println: "interpreter"
then
.next @ dup
current-task =
current @ =
until
drop ;

Expand All @@ -125,7 +111,7 @@ defer: pause
: wait ( semaphore -- ) begin pause dup @ until -1 swap +! ;
: signal ( semaphore -- ) 1 swap +! pause ;

: multi-handler ( -- a ) current-task .handler ;
: multi-handler ( -- a ) current @ .handler ;

: multi ( -- ) \ switch to multi-task mode
['] handler is: multi-handler \ each tasks should have its own exception handler
Expand All @@ -135,6 +121,6 @@ defer: pause
: single ( -- ) \ switch to signle-task mode
['] handler is: single-handler \ use global handler
0 xpause !
['] pause is: pause-single ;
['] pause is: nop ;

single

0 comments on commit 1129430

Please sign in to comment.