Paste number 25629: Cooperative multi-threader

Index of paste annotations: 1

Paste number 25629: Cooperative multi-threader
Pasted by: psykotic
When:7 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+JRX
Channel:None
Paste contents:
Raw Source | XML | Display As
;;; Cooperative multi-threader

(define *threads* '())
(define *bottom* #f)

(define (push-thread! thread)
  (set! *threads* (append *threads* (list thread))))

(define (pop-thread!)
  (let ((thread (car *threads*)))
    (set! *threads* (cdr *threads*))
    thread))

(define (spawn thunk)
  (push-thread! (lambda (x) (thunk))))

(define (yield)
  (call-with-current-continuation (lambda (k)
                                    (push-thread! k)
                                    (schedule))))

(define (run thunk)
  (call-with-current-continuation (lambda (k)
                                    (set! *bottom* k)
                                    (spawn thunk)
                                    (schedule))))

(define (schedule)
  (if (null? *threads*)
      (*bottom*)
      (let ((thunk (pop-thread!)))
        (thunk #f)
        (schedule))))

Annotations for this paste:

Annotation number 1: asfd
Pasted by: asdf
When:7 years, 7 months ago
Share:Tweet this! | http://paste.lisp.org/+JRX/1
Paste contents:
Raw Source | Display As
asfd

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.