;;;;;;;;; scheduler ; the queue of ready threads (define cont (list)) ; adds a thread to the ready queue (define (schedule f) (set! cont (append cont (list f)))) ; starts execution of the next threaad in the ready queue (define (run-next) (cond ((null? cont) #f) ; no more threads to schedule (else (let ((next (car cont)) (rest (cdr cont))) (begin (set! cont rest) ; remove the first thread from the queue (next #t)))))) ; run it ; starts execution of the first thread in the ready queue (define (start) (run-next)) ; stops execution of the current thread; starts execution of the ; first thread in the ready queue (define (exit) (run-next)) ; suspend the current thread and (define (yield) (call-with-current-continuation (lambda (f) (begin (schedule f) (run-next))))) ;;;;;;;;; user code (define (proc s n) (begin (display s) (display n) (display "\n") (yield) (cond ((> n 0) (proc s (- n 1))) (else (exit))))) (schedule (lambda (f) (proc "proc1: " 10))) (schedule (lambda (f) (proc "proc2: " 6))) (schedule (lambda (f) (proc "proc3: " 4))) (display "let's start\n") (start) (display "game over\n")