Can I do cooperative multi-tasking?

Started by rrq, June 04, 2014, 07:10:39 PM

Previous topic - Next topic

rrq

Is there a possibility of a function that lets me pivot the execution, in the way of saving the current environment (i.e., stacks etc) into a pdl of such, then pop and continue with a saved environment?

Thus, I imagine a function, say (pivot store), that firstly makes a blob (copy) of the current exection state, then pushes that onto the given "store" (list of blobs) or, if nil, onto an internal store, then pops a blob from the internal store, before restoring this as execution state, which thus continues. (Of course, the continuation should be as an immediate return from that other call of "pivot", rather than pushing the blob again)

That kind of primitive would open the gate for cooperative multi-tasking, and allow programs with thousands of light-weight "virutally concurrent" pursuits, such as large scale simulations, as well as other domains where "parallel pursuits" design is desired.

I've started with the coding, but I'm not fully on top of the extent of things to include in the execution state blob, and wanted to check here for pointers and thoughts first.

Lutz

#1
A Unix thread like API, like you are suggesting, is very expensive to implement in a LISP interpreter managing many stacks beside the processor stack. Unix processes need very little overhead in the interpreter and the OS will also take care of distributing different processes on different cores in modern processors.



newLISP uses Unix forked child processes to implement multiprocessing. You can use either fork and do synchronization yourself using semaphore and wait-pid and pipe for message communications. Or you can use the Cilk based API with spawn, sync and send and receive for agent based messaging implemented with socket pairs.



All this only on OSX, Linux and other Unix. The Cygwin based implementation for fork on Windows has too much problems.



Some code links:



http://www.newlisp.org/newlisp-10.6.0/examples/prodcons.lsp">http://www.newlisp.org/newlisp-10.6.0/e ... odcons.lsp">http://www.newlisp.org/newlisp-10.6.0/examples/prodcons.lsp

http://www.newlisp.org/newlisp-10.6.0/qa-specific-tests/qa-pipefork">http://www.newlisp.org/newlisp-10.6.0/q ... a-pipefork">http://www.newlisp.org/newlisp-10.6.0/qa-specific-tests/qa-pipefork

http://www.newlisp.org/newlisp-10.6.0/qa-specific-tests/qa-cilk">http://www.newlisp.org/newlisp-10.6.0/q ... ts/qa-cilk">http://www.newlisp.org/newlisp-10.6.0/qa-specific-tests/qa-cilk

http://www.newlisp.org/newlisp-10.6.0/qa-specific-tests/qa-message">http://www.newlisp.org/newlisp-10.6.0/q ... qa-message">http://www.newlisp.org/newlisp-10.6.0/qa-specific-tests/qa-message

http://www.newlisp.org/newlisp-10.6.0/qa-specific-tests/qa-msgbig">http://www.newlisp.org/newlisp-10.6.0/q ... /qa-msgbig">http://www.newlisp.org/newlisp-10.6.0/qa-specific-tests/qa-msgbig

rrq

#2
Great. Thanks. I suppose mostly it tells me that no sane person should venture into this even if they can make a good argument for having it :-)

Fair enough; and indeed, the newlisp as is, sure is a pleasure to work with anyhow.