callback.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ; Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; This code, along with C code in c/external.c, handles the interaction between
  3. ; callbacks from external code to Scheme functions and uses of continuations in
  4. ; Scheme. The problem is that Scheme 48 uses multiple continuations while
  5. ; operating with only one process stack.
  6. ;
  7. ; Suppose we have Scheme procedures s1 and s2 and C procedure c1 such that
  8. ; s1 calls c1 and c1 calls s2. There are two trampoline functions that are
  9. ; used to do this. The VM uses s48_external_call to call c1 and c1 uses
  10. ; s48_call_scheme to start the VM running s2. While in s2 the process stack will
  11. ; look like this:
  12. ;
  13. ; <C frame for VM running s2>
  14. ; <C frame for s48_call_scheme>
  15. ; <C frame for c1>
  16. ; <C frame for s48_external_call>
  17. ; <C frame for VM running s1>
  18. ; <base>
  19. ;
  20. ; The C code in c/external.scm keeps a record of the portions of the process
  21. ; stack that are running external code. Each of these stack portions has an
  22. ; s48_external_call frame at the base and an s48_call_scheme frame at the top.
  23. ; The stack is represented as linked list of records, called `stack-block's,
  24. ; each of which contains the following values:
  25. ; free? ; true if this frame is no longer needed
  26. ; unwind ; the longjmp target used to skip over this frame
  27. ; proc-name ; the name of the procedure this block is executing
  28. ; placeholder ; either #f or a placeholder, see the section on threads below
  29. ; next ; the next stack-block below this one
  30. ; These are Scheme records and are traced by the GC.
  31. (define-record-type stack-block :stack-block
  32. (stack-blocks-are-made-from-c)
  33. stack-block?
  34. (free? stack-block-free? set-stack-block-free?!)
  35. (unwind stack-block-unwind)
  36. (proc-name stack-block-proc-name)
  37. (placeholder stack-block-placeholder set-stack-block-placeholder!)
  38. (next stack-block-next))
  39. ; Stack-blocks are made from C, so we need to export the type.
  40. (define-exported-binding "s48-stack-block-type" :stack-block)
  41. ; There is no need to keep track of the VM frames. These are all interchangable
  42. ; because 1) the VM's state is kept in top-level variables and 2) we have
  43. ; arranged it so that the relevent VM opcodes, call-external-value and
  44. ; return-from-callback, are all the same length and are always immediately
  45. ; followed by a return instruction. s48_call_scheme can safely overwrite the
  46. ; template and code-pointer registers in the VM as they always point to a
  47. ; one-byte instruction followed by a return instruction. When the VM returns
  48. ; from the callback, via a return-from-callback instruction, that too is a
  49. ; one-byte instruction followed by a return instruction. The VM can proceed,
  50. ; happily ignorant of all this fooling around.
  51. ;
  52. ; On entry, s48_external_call saves a longjump target. This is used when
  53. ; raising exceptions from with the external code and for unwinding the process
  54. ; stack. Each invocation of s48_call_scheme creates a new stack-block, saving
  55. ; within it the longjump target of the corresponding s48_external_call. `Free?'
  56. ; and `placeholder' are initially false and `next' points to existing list of
  57. ; stack-blocks.
  58. ;
  59. ; When a callback returns to s48_call_scheme, the corresponding block is popped
  60. ; off the list of stack-blocks.
  61. ;
  62. ; So far so good, and if that were all that happened there would be no need for
  63. ; all this mechanism. There are two problems: call/cc and threads. Call/cc is
  64. ; simpler to deal with. We have downward continuations in C, as implemented
  65. ; by longjmp(), so we simply limit continuations that cross callbacks to being
  66. ; downwards only. We also need to arrange for any jumped-over stack portions
  67. ; to be popped off of the stack.
  68. ;
  69. ; The popping off is handled by s48_external_call. Just before returning to the
  70. ; VM it checks to see if the top stack-block is free. If so, it loops through
  71. ; the list of stack-blocks to find the first non-free stack portion. A longjump
  72. ; is performed to the target in the last free block, removing any unneeded frames
  73. ; from the stack.
  74. ;
  75. ; s48_call_scheme starts the VM running the following CALLBACK procedure. The
  76. ; arguments are BLOCK, the stack-block just created for this callback, and
  77. ; the procedure and arguments for the actual callback. It prevents jumps back
  78. ; into the callback and frees BLOCK if a throw out occurs.
  79. ;
  80. ; We disable interrupts to ensure that nothing intervenes between setting DONE?
  81. ; and returning from the callback. BLOCK is then either freed or returned to,
  82. ; but not both or neither. RETURN-FROM-CALLBACK reenables interrupts.
  83. (define (callback block proc . args)
  84. (let ((done? #f))
  85. (return-from-callback block
  86. (dynamic-wind
  87. (lambda ()
  88. (if done?
  89. (error "attempt to throw into a callback"
  90. (cons proc args))))
  91. (lambda ()
  92. (let ((result (apply proc args)))
  93. (disable-interrupts!)
  94. (set! done? #t)
  95. result))
  96. (lambda ()
  97. (if (not done?)
  98. (begin
  99. (set! done? #t)
  100. (set-stack-block-free?! block #t)
  101. (clear-stack-top!))))))))
  102. (define-exported-binding "s48-callback" callback)
  103. ; CLEAR-STACK-TOP! is an empty C procedure. When it returns, s48_external_call
  104. ; will automatically clear any free frames off of the stack.
  105. (import-lambda-definition clear-stack-top! () "s48_clear_stack_top")
  106. ; Dealing with threads.
  107. ;
  108. ; The difficulty here is that each stack-block belongs to some thread. Thread A
  109. ; can call a C procedures which calls back into Scheme. At that point a context
  110. ; switch occurs and we start running thread B, which promptly does the same
  111. ; calls. THe process stack then looks like this:
  112. ;
  113. ; <C frame for VM running B1>
  114. ; <C frame for s48_call_scheme>
  115. ; <C frame for B's c code>
  116. ; <C frame for s48_external_call>
  117. ; <C frame for VM running A1 and then B0>
  118. ; <C frame for s48_call_scheme>
  119. ; <C frame for A's c code>
  120. ; <C frame for s48_external_call>
  121. ; <C frame for VM running A0>
  122. ; <base>
  123. ;
  124. ; At this point A cannot return from its callback before B does, because B's
  125. ; portion of the process stack is above A's. If A does try to return it must
  126. ; block until it again is at the top of the stack.
  127. ;
  128. ; This is handled by s48_call_scheme, which checks to see if the stack-block
  129. ; being returned to is at the top of the stack. If not, it does a second
  130. ; callback to DELAY-CALLBACK-RETURN, defined below, with the same stack-block.
  131. ; DELAY-CALLBACK-RETURN creates a placeholder, puts it in the stack-block, and
  132. ; then blocks on it. When the placeholder gets a value the procedure attempts
  133. ; another return-from-callback.
  134. ;
  135. ; This is called with interrupts disabled, as we need to avoid having BLOCK
  136. ; reach the top of the stack before the placeholder is installed.
  137. (define (delay-callback-return block value)
  138. (let ((placeholder (make-placeholder)))
  139. (set-stack-block-placeholder! block placeholder)
  140. (enable-interrupts!)
  141. (placeholder-value placeholder)
  142. value))
  143. (define-exported-binding "s48-delay-callback-return" delay-callback-return)
  144. ; Finally, s48_external_call looks to see if the top stack-block has a
  145. ; placeholder. If it does, it raises an exception instead of doing a normal
  146. ; return. The exception handler sets the placeholder's value, allowing the
  147. ; blocked thread to continue. The handler then returns the external call's
  148. ; value to its own thread, or, if the callback-return-uncovered is piggybacked
  149. ; on another exception, we raise that exception.
  150. ;
  151. ; Because of the all of the games played above, the callback-return-uncovered
  152. ; exception may appear to have come from either the call-external-value, or
  153. ; return-from-callback opcodes.
  154. (define uncovered-return-handler
  155. (lambda (opcode reason . args)
  156. (if (= reason (enum exception callback-return-uncovered))
  157. (call-with-values
  158. (lambda ()
  159. (if (= 2 (length args))
  160. (values (car args)
  161. (cadr args)
  162. #f)
  163. (let ((args (reverse args)))
  164. (values (car args)
  165. (cadr args)
  166. (reverse (cddr args))))))
  167. (lambda (block return-value exception-args)
  168. (let ((placeholder (stack-block-placeholder block)))
  169. (set-stack-block-placeholder! block #f)
  170. (placeholder-set! placeholder #t)
  171. (if exception-args
  172. (apply signal-vm-exception opcode return-value exception-args)
  173. return-value))))
  174. (apply signal-vm-exception opcode reason args))))
  175. (define (block-depth block)
  176. (if block
  177. (+ 1 (block-depth (stack-block-next block)))
  178. 0))
  179. (for-each (lambda (opcode)
  180. (define-vm-exception-handler opcode uncovered-return-handler))
  181. (list (enum op call-external-value)
  182. (enum op return-from-callback)))
  183. ;----------------
  184. ; Utility for the common case of calling an imported binding.
  185. (define (call-imported-binding proc . args)
  186. (if (and (shared-binding? proc)
  187. (shared-binding-is-import? proc))
  188. (let ((value (shared-binding-ref proc)))
  189. (if (byte-vector? value)
  190. (apply call-external-value
  191. value
  192. (shared-binding-name proc)
  193. args)
  194. (apply call-error "bad procedure" call-imported-binding proc args)))
  195. (apply call-error "bad procedure" call-imported-binding proc args)))
  196. ;----------------
  197. ; We export the record-type type so that external code can check to see if
  198. ; supposed record types really are such.
  199. (define-exported-binding "s48-the-record-type" :record-type)
  200. ;----------------
  201. ; Testing
  202. ;
  203. ; `s48_trampoline' is a C routine that calls its Scheme argument with between
  204. ; zero and three arguments. The arguments are 100, 200, and 300.
  205. ;
  206. ;(import-lambda-definition trampoline (proc nargs)
  207. ; "s48_trampoline")
  208. ;
  209. ;(define (foo . args)
  210. ; (for-each display (list "[foo " args "]"))
  211. ; (newline)
  212. ; (cons 'foo-return args))
  213. ;
  214. ;; This should return 1100.
  215. ;
  216. ;(define (test0)
  217. ; (trampoline (lambda ()
  218. ; (call-with-current-continuation
  219. ; (lambda (c)
  220. ; (trampoline (lambda (x)
  221. ; (c (+ x 1000)))
  222. ; 1))))
  223. ; 0))
  224. ;
  225. ;; ,open threads locks debug-messages
  226. ;
  227. ;(define (test1 error?)
  228. ; (let ((lock (make-lock))
  229. ; (repl-lock (make-lock)))
  230. ; (obtain-lock repl-lock)
  231. ; (spawn (lambda ()
  232. ; (obtain-lock lock)
  233. ; (debug-message "A returned "
  234. ; (trampoline (lambda ()
  235. ; (obtain-lock lock) ; we block
  236. ; 'a)
  237. ; 0))
  238. ; (release-lock repl-lock))
  239. ; 'thread-a)
  240. ; (spawn (lambda ()
  241. ; (debug-message "B returned "
  242. ; (trampoline (lambda ()
  243. ; (release-lock lock) ; A can run
  244. ; (relinquish-timeslice) ; let A run
  245. ; (if error? #f 'b))
  246. ; 0)))
  247. ; 'thread-b)
  248. ; (obtain-lock repl-lock)))