Page 1 of 1

Can I do cooperative multi-tasking?

PostPosted: Thu Jun 05, 2014 2:10 am
by ralph.ronnquist
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.

Re: Can I do cooperative multi-tasking?

PostPosted: Fri Jun 06, 2014 1:55 pm
by Lutz
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: ... odcons.lsp ... a-pipefork ... ts/qa-cilk ... qa-message ... /qa-msgbig

Re: Can I do cooperative multi-tasking?

PostPosted: Sat Jun 07, 2014 1:39 am
by ralph.ronnquist
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.