I played around with libCoroutine
http://www.dekorte.com/projects/opensou ... Coroutine/
I wrote a bit about it in my blog. http://sparebandwidth.blogspot.com/2006 ... tines.html
Anyway, I hacked newLISP source code (version 8.9.7) to add prmitives for coroutine stuff using the libCoroutine code.
It seems to work.
Basically, I create a context for each coroutine task using a code template commonly used by bunch of threads.
Code: Select all
;;; coroutine testing
(define (make-coroutine)
(let (temp (append (lambda) (list (first (args 1))) (rest (args 1))))
(def-new 'temp (sym (args 0) (args 0)))))
(define (coroutine-generic myname me him)
(begin
(setq taskname myname)
(while 1
(println (format "%s bytes left on stack %d" taskname (coroutine-bytes-left-on-stack me) ))
(coroutine-switch me him)
(sleep 1000))))
(define (coroutine1)
(setq task2-handle (coroutine-create))
(make-coroutine 'task2 (0 coroutine-generic))
(setq task3-handle (coroutine-create))
(make-coroutine 'task3 (0 coroutine-generic))
(setq task4-handle (coroutine-create))
(make-coroutine 'task4 (0 coroutine-generic))
(coroutine-start task1-handle task2-handle (task2:task2 "task2" task2-handle task1-handle))
(coroutine-start task1-handle task3-handle (task3:task3 "task3" task3-handle task1-handle))
(coroutine-start task1-handle task4-handle (task4:task4 "task4" task4-handle task1-handle))
(setq task-list '(task2-handle task3-handle task4-handle))
(setq num 0)
(while (< num 20)
(println (format "coroutine1: bytes left on stack %d" (coroutine-bytes-left-on-stack task1-handle) ))
(coroutine-switch task1-handle (task-list (int (first (random 0 3 1)))))
(setq num (+ 1 num))
(sleep 1000)))
(setq task-main-handle (coroutine-main))
(setq task1-handle (coroutine-create))
(coroutine-start task-main-handle task1-handle (coroutine1))
Would you be interested in something like this Lutz?