;; -*- Mode: Irken -*-
;; Version of coro0.scm that uses getcc/putcc directly.
;;
;; At first blush, it seems that this will be more efficient, since
;; it's not creating closures upon each yield. However, we have to
;; stop yield from being inlined (otherwise the wrong continuation
;; will be captured), and this may completely undo the advantage?
;;
;; The truth is that using getcc/putcc requires knowledge of what
;; the compiler is doing, and that's bad.
(include "lib/basis.scm")
(define run-queue (queue/make))
(define (enqueue k)
(queue/add run-queue k))
(define (dispatch)
(match (queue/pop run-queue) with
(maybe:yes k) -> (putcc k #u)
(maybe:no) -> #u))
(define (^fork f)
(enqueue (getcc))
(f)
(dispatch))
(define fork ^fork)
(define (^yield)
(enqueue (getcc))
(dispatch))
(define yield ^yield)
(define (thread n)
(for-range
i 10
(print-string (format "thread# " (int n) ":" (int i) "\n"))
(yield)
))
(print-string "first fork\n")
(fork (lambda () (thread 0)))
(print-string "second fork\n")
(fork (lambda () (thread 1)))
(thread 2)
(print-string (format "queue length = " (int run-queue.length) "\n"))