MULTI PROCESSING
In multiprocessing there are two kinds of threads: (that I'm aware
of) preemptive and cooperative. In preemptive threading, the OS will
forcefully interrupt one process to allow another process time. In
cooperative threading, the processes must willfully relinquish control.
This setup is cooperative.
A while back someone mentioned multiprocessing in RW. That
prompted me to actually impliment an idea I've been toying with for a while.
Here's the core of it.
next-process:
q' recall vstore
q' recall 1 + dup
52 {max process number} > next-process.wrap if
dup q' store
vrecall
return
next-process.wrap:
swap drop 50 swap
return
In this system, all the processes are stored in the vector,
starting at index 50. Q stores the current process number. When you want
to relinquish time to the next process, simply call next-process. It will
sture the return address at the current process, increment Q, and switch to
the next process.
When a process calls next-process, it is halted at that call. It
will resume immediately after the call.
To use the system, you need to first know several things: how many
processes there will be, where they start from, and how often you want to
call next-process.
Before calling next-process, you need to initialize the vector and
Q. Here is an example robot:
########Totally ineffective demo robot#############
####50 shields & 50 processor
search 50 vstore ###First process stored at 50
move 51 vstore ###Next at 51
defend 52 vstore ###Last at 52, notice '52' within next-process
50 q' store ###init Q to 50, the first process
search jump ###Jump to the first process to start multiprocessing
next-process:
q' recall vstore
q' recall 1 + dup
52 > next-process.wrap if
dup q' store
vrecall
return
next-process.wrap:
swap drop 50 swap
return
search: ###Yields control every 10 chronons
aim 127 + aim' store
range shoot if
chronon 10 mod not next-process if
search jump
shoot:
1 fire' store
return
move: ###Does its thing & vields control
150 x - 150 y - arctan 75 +
dup
2 sin speedx' store
-2 cos speedy' store
next-process call
move jump
defend: ####Ditto
50 shield' sto
next-process call
defend jump
#######################################
Optimization:
If you're willing to give up the low end of your vector you can use
a simpler next-process:
next-process:
q' recall vstore
q' recall 1 + 3 {3 = max process number + 1} mod
dup q' store
vrecall
return
Naturally, you'd initialize the vector startiong at zero rather
than 50. You don't have to initialize Q--it's automatically zero.