try it out
You can try running
test.js a few times and see different results each time as it context switches on a timer!
how it works
Currently, each thread is just a generator function which
yields at any point (between any two statements). we drop back into a thread manager which checks how much time elapsed, and if it's beyond the threshold of 1ms, we context switch. threads are scheduled in FIFO order. additionally, once a thread finishes (returns from the function rather than yield), we stop scheduling it (duh).
Technically this is non-preemptive scheduling, since if we had no
yields the threads would run in full. But the plan is to write a compiler that will look for specific directives and spit out a file with yields following every statement/expression, allowing for random context switches at any time.
- remove the need to call
- concur now stipulates that there be a main thread from which all other threads fork. it makes the code a bit nicer, and I may streamline the creation of the main thread in the future (so that it is more of a specialized thread than a normal one), which should take care of the call to
- better scheduling algo!
- write a compiler from js to concur-ready js
concur(context, func[, func[, ...func]])to execute the argument functions concurrently.
contextis the shared context between the functions. each
functakes a context as its sole argument.