wind.scm 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ; -*- Mode: Scheme; Syntax: Scheme; Package: Scheme; -*-
  2. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  3. ; This is file wind.scm. (Rhymes with "find," not "pinned.")
  4. ;;;; Dynamic-wind
  5. ; This is a version of dynamic-wind that tries to do "the right thing"
  6. ; in the presence of multiple threads of control.
  7. ; This definition of "the right thing" is due to Pavel Curtis, and is
  8. ; the one used in Scheme Xerox. It is very different from what you will
  9. ; find in, say, MIT Scheme.
  10. ;
  11. ; When we want to go to a new target state (e.g. on invoking a
  12. ; continuation), we ascend to the nearest common ancestor of the
  13. ; current state and the target state, executing the "out" (or
  14. ; "unwind") thunk for each state on the way; then we climb back down
  15. ; to the target state executing the "in" thunk for each state. Unlike
  16. ; the Hanson/Lamping algorithm, the tree of states is not altered in
  17. ; any way.
  18. ;
  19. ; Each thread starts out in the root state, but continuations capture
  20. ; the state where they're created.
  21. ; Dynamic-wind
  22. (define (dynamic-wind in body out)
  23. (in)
  24. (let ((here (get-dynamic-point)))
  25. (set-dynamic-point! (make-point (if here
  26. (+ (point-depth here) 1)
  27. 1)
  28. in
  29. out
  30. (get-dynamic-env)
  31. here))
  32. (let ((results (call-with-values body list)))
  33. (set-dynamic-point! here)
  34. (out)
  35. (apply values results))))
  36. ; call-with-current-continuation
  37. (define (call-with-current-continuation proc)
  38. (primitive-cwcc
  39. (lambda (cont)
  40. (let ((env (get-dynamic-env))
  41. (point (get-dynamic-point))
  42. (proposal (current-proposal)))
  43. ;; don't close over PROC
  44. (proc (continuation->procedure cont env point proposal))))))
  45. (define (continuation->procedure cont env point proposal)
  46. (lambda results
  47. (travel-to-point! (get-dynamic-point) point)
  48. (set-dynamic-env! env)
  49. (set-dynamic-point! point)
  50. (set-current-proposal! proposal)
  51. (with-continuation cont
  52. (lambda ()
  53. (apply values results)))))
  54. ; Point in state space = <depth, in, out, dynamic-env, parent>
  55. ; dynamic-env = dynamic environment for execution of the in and out thunks
  56. (define-record-type point :point
  57. (make-point depth in out dynamic-env parent)
  58. (depth point-depth)
  59. (in point-in)
  60. (out point-out)
  61. (dynamic-env point-dynamic-env)
  62. (parent point-parent))
  63. ; To make the modularity simpler, and to help Kali, the root point is #F.
  64. ; Go to a point in state space. This involves running out-thunks from
  65. ; the current point out to its common ancestor with the target, and
  66. ; then running in-thunks from the ancestor to the target.
  67. (define (travel-to-point! here target)
  68. (cond ((eq? here target) 'done)
  69. ((or (not here) ; HERE has reached the root.
  70. (and target
  71. (< (point-depth here)
  72. (point-depth target))))
  73. (travel-to-point! here (point-parent target))
  74. ((point-in target))
  75. (set-dynamic-env! (point-dynamic-env target))
  76. (set-dynamic-point! target))
  77. (else
  78. (set-dynamic-env! (point-dynamic-env here))
  79. (set-dynamic-point! here)
  80. ((point-out here))
  81. (travel-to-point! (point-parent here) target))))
  82. ; (put 'let-dynamic-point 'scheme-indent-hook 1)