engine.scm 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ; Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Christopher P. Haynes and Daniel P. Friedman.
  3. ; Engines build process abstractions.
  4. ; 1984 ACM Symposium on Lisp and Functional Programming, pages 18-24.
  5. ; This is incompatible with the threads package.
  6. ; ,open primitives interrupts
  7. (define interrupt/alarm (enum interrupt alarm))
  8. (define (run thunk interval when-done when-timeout)
  9. (let ((save (vector-ref interrupt-handlers interrupt/alarm)))
  10. (let ((finish
  11. (call-with-current-continuation
  12. (lambda (k)
  13. (vector-set! interrupt-handlers
  14. interrupt/alarm
  15. (lambda (tem ei)
  16. (set-enabled-interrupts! ei)
  17. (call-with-current-continuation
  18. (lambda (resume)
  19. (k (lambda ()
  20. (when-timeout (lambda ()
  21. (resume #f)))))))))
  22. (schedule-interrupt interval *exponent* #f)
  23. (call-with-values thunk
  24. (lambda vals
  25. (let ((time-remaining (schedule-interrupt 0 0 #f)))
  26. (lambda ()
  27. (apply when-done time-remaining vals)))))))))
  28. (vector-set! interrupt-handlers
  29. interrupt/alarm
  30. save)
  31. (finish))))
  32. (define *exponent* -3)
  33. (define-syntax engine
  34. (syntax-rules ()
  35. ((engine ?E) (%engine (lambda () ?E)))))
  36. (define (%engine thunk)
  37. (lambda (ticks done out)
  38. (run thunk
  39. ticks
  40. (lambda (ticks val)
  41. (done val ticks))
  42. (lambda (new-thunk)
  43. (out (%engine new-thunk))))))
  44. ; Example from the LFP '84 paper (verbatim)
  45. ;(define-syntax rec
  46. ; (syntax-rules () ((rec ?X ?E) (letrec ((?X ?E)) ?X))))
  47. ;
  48. ;(define complete
  49. ; (lambda (eng)
  50. ; ((rec loop
  51. ; (lambda (eng count)
  52. ; (eng 1000
  53. ; (lambda (val ticks-left)
  54. ; (cons val
  55. ; (+ (- 1000 ticks-left)
  56. ; count)))
  57. ; (lambda (eng)
  58. ; (loop eng (+ count 1000))))))
  59. ; eng 0)))