coroutines: make yield() return a configured uword so that a task subroutine can get reused for multiple different things

This commit is contained in:
Irmen de Jong
2024-12-26 18:55:32 +01:00
parent 4daa909f32
commit f50899c6fa
4 changed files with 73 additions and 34 deletions
+52 -20
View File
@@ -11,18 +11,50 @@ main {
sub start() {
txt.print("cooperative multitasking / coroutines\n\n")
txt.print("here are couple of routines that each\nrun a few loops bouncing a digit around.\n")
txt.print("press any key to add a new counter task.")
coroutines.killall()
void coroutines.add(&task1)
void coroutines.add(&task2)
void coroutines.add(&task3)
void coroutines.add(&task4)
void coroutines.add(&delaytask)
void coroutines.add(&task1, sc:'1')
void coroutines.add(&task2, sc:'2')
void coroutines.add(&task3, sc:'3')
void coroutines.add(&task4, sc:'4')
void coroutines.add(&keyhandler, 0)
void coroutines.add(&delaytask, 0)
coroutines.run()
txt.print("we're all done!\n")
}
sub keyhandler() {
repeat {
ubyte key = cbm.GETIN2()
if key!=0 {
void coroutines.add(&countertask, key)
}
void coroutines.yield()
}
}
ubyte[coroutines.MAX_TASKS] counters = [100] * coroutines.MAX_TASKS
sub countertask() {
repeat {
uword userdata = coroutines.yield() ; yield and obtain our userdata
ubyte tid = coroutines.current() ; what task are we?
txt.plot(15, 10 + tid)
counters[tid]-- ; our counter is in the array, cannot be a local variable (shared state)
if counters[tid] == 0 {
txt.print(" ")
return ; done, exit the task
} else {
txt.chrout(lsb(userdata))
txt.chrout(':')
txt.print_uw(counters[tid])
txt.spc()
}
}
}
sub task1() {
const ubyte x = 5
ubyte y
@@ -30,12 +62,12 @@ main {
for y in 10 to 24 {
txt.setchr(x, y-1, sc:' ')
txt.setchr(x, y, sc:'1')
coroutines.yield()
void coroutines.yield()
}
for y in 24 downto 10 {
txt.setchr(x, y+1, sc:' ')
txt.setchr(x, y, sc:'1')
coroutines.yield()
void coroutines.yield()
}
}
txt.setchr(x, 10, sc:' ')
@@ -45,21 +77,21 @@ main {
const ubyte x = 10
ubyte y
repeat 2 {
for y in 5 to 18 {
for y in 9 to 22 {
txt.setchr(x, y-1, sc:' ')
txt.setchr(x, y, sc:'2')
coroutines.yield()
void coroutines.yield()
}
for y in 18 downto 5 {
for y in 22 downto 9 {
txt.setchr(x, y+1, sc:' ')
txt.setchr(x, y, sc:'2')
coroutines.yield()
void coroutines.yield()
}
}
txt.setchr(x, 5, sc:' ')
txt.setchr(x, 9, sc:' ')
; add a new task dynamically
void coroutines.add(&task5)
void coroutines.add(&task5, 0)
}
sub task3() {
@@ -69,12 +101,12 @@ main {
for x in 14 to 38 {
txt.setchr(x-1, y, sc:' ')
txt.setchr(x, y, sc:'3')
coroutines.yield()
void coroutines.yield()
}
for x in 38 downto 14 {
txt.setchr(x+1, y, sc:' ')
txt.setchr(x, y, sc:'3')
coroutines.yield()
void coroutines.yield()
}
}
txt.setchr(14, y, sc:' ')
@@ -87,12 +119,12 @@ main {
for x in 15 to 30 {
txt.setchr(x-1, y, sc:' ')
txt.setchr(x, y, sc:'4')
coroutines.yield()
void coroutines.yield()
}
for x in 30 downto 15 {
txt.setchr(x+1, y, sc:' ')
txt.setchr(x, y, sc:'4')
coroutines.yield()
void coroutines.yield()
}
}
txt.setchr(15, y, sc:' ')
@@ -105,12 +137,12 @@ main {
for x in 15 to 30 {
txt.setchr(x-1, y, sc:' ')
txt.setchr(x, y, sc:'5')
coroutines.yield()
void coroutines.yield()
}
for x in 30 downto 15 {
txt.setchr(x+1, y, sc:' ')
txt.setchr(x, y, sc:'5')
coroutines.yield()
void coroutines.yield()
}
}
txt.setchr(15, y, sc:' ')
@@ -120,7 +152,7 @@ main {
repeat 200 {
sys.waitvsync()
sys.waitvsync()
coroutines.yield()
void coroutines.yield()
}
}
}