Commit | Line | Data |
---|---|---|
14aa25e4 | 1 | /* Copyright (C) 1995,1996,1998,2000,2001,2004, 2006, 2008, 2009 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
4 | * modify it under the terms of the GNU Lesser General Public License |
5 | * as published by the Free Software Foundation; either version 3 of | |
6 | * the License, or (at your option) any later version. | |
0f2d19dd | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
0f2d19dd | 12 | * |
73be1d9e MV |
13 | * You should have received a copy of the GNU Lesser General Public |
14 | * License along with this library; if not, write to the Free Software | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
1bbd0b84 | 18 | |
1bbd0b84 | 19 | |
0f2d19dd | 20 | \f |
dbb605f5 LC |
21 | #ifdef HAVE_CONFIG_H |
22 | # include <config.h> | |
23 | #endif | |
0f2d19dd | 24 | |
bbd43f03 RB |
25 | #include "libguile/_scm.h" |
26 | ||
13070bd3 | 27 | #include <string.h> |
8b7f0bb3 | 28 | #include <stdio.h> |
13070bd3 | 29 | |
8b7f0bb3 | 30 | #include "libguile/async.h" |
d0f6ceb8 | 31 | #include "libguile/debug.h" |
a0599745 MD |
32 | #include "libguile/root.h" |
33 | #include "libguile/stackchk.h" | |
5f144b10 GH |
34 | #include "libguile/smob.h" |
35 | #include "libguile/ports.h" | |
36 | #include "libguile/dynwind.h" | |
ce212434 | 37 | #include "libguile/values.h" |
9de87eea | 38 | #include "libguile/eval.h" |
bfffd258 | 39 | #include "libguile/vm.h" |
5f144b10 | 40 | |
db4b4ca6 | 41 | #include "libguile/validate.h" |
a0599745 | 42 | #include "libguile/continuations.h" |
01c8a3dd | 43 | |
0f2d19dd JB |
44 | \f |
45 | ||
46 | /* {Continuations} | |
47 | */ | |
48 | ||
92c2555f | 49 | scm_t_bits scm_tc16_continuation; |
0f2d19dd | 50 | |
01c8a3dd | 51 | |
e841c3e0 | 52 | static int |
e81d98ec | 53 | continuation_print (SCM obj, SCM port, scm_print_state *state SCM_UNUSED) |
5f144b10 | 54 | { |
92c2555f | 55 | scm_t_contregs *continuation = SCM_CONTREGS (obj); |
5f144b10 GH |
56 | |
57 | scm_puts ("#<continuation ", port); | |
58 | scm_intprint (continuation->num_stack_items, 10, port); | |
59 | scm_puts (" @ ", port); | |
0345e278 | 60 | scm_uintprint (SCM_CELL_WORD_1 (obj), 16, port); |
5f144b10 GH |
61 | scm_putc ('>', port); |
62 | return 1; | |
63 | } | |
1cc91f1b | 64 | |
5f144b10 GH |
65 | /* this may return more than once: the first time with the escape |
66 | procedure, then subsequently with the value to be passed to the | |
67 | continuation. */ | |
68 | #define FUNC_NAME "scm_make_continuation" | |
0f2d19dd | 69 | SCM |
5f144b10 | 70 | scm_make_continuation (int *first) |
0f2d19dd | 71 | { |
9de87eea MV |
72 | scm_i_thread *thread = SCM_I_CURRENT_THREAD; |
73 | SCM cont; | |
92c2555f | 74 | scm_t_contregs *continuation; |
c014a02e | 75 | long stack_size; |
01c8a3dd | 76 | SCM_STACKITEM * src; |
0f2d19dd | 77 | |
0f2d19dd | 78 | SCM_FLUSH_REGISTER_WINDOWS; |
9de87eea | 79 | stack_size = scm_stack_size (thread->continuation_base); |
4c9419ac MV |
80 | continuation = scm_gc_malloc (sizeof (scm_t_contregs) |
81 | + (stack_size - 1) * sizeof (SCM_STACKITEM), | |
82 | "continuation"); | |
5f144b10 | 83 | continuation->num_stack_items = stack_size; |
9de87eea | 84 | continuation->dynenv = scm_i_dynwinds (); |
5f144b10 | 85 | continuation->throw_value = SCM_EOL; |
9de87eea | 86 | continuation->root = thread->continuation_root; |
9de87eea | 87 | src = thread->continuation_base; |
4ccb2cd2 | 88 | #if ! SCM_STACK_GROWS_UP |
5f144b10 GH |
89 | src -= stack_size; |
90 | #endif | |
5c5c27dc | 91 | continuation->offset = continuation->stack - src; |
5f144b10 | 92 | memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size); |
bfffd258 | 93 | continuation->vm_conts = scm_vm_capture_continuations (); |
5f144b10 | 94 | |
79824460 AW |
95 | SCM_NEWSMOB (cont, scm_tc16_continuation, continuation); |
96 | ||
a4dbe1ac | 97 | *first = !SCM_I_SETJMP (continuation->jmpbuf); |
346e4402 | 98 | if (*first) |
193297d8 | 99 | { |
346e4402 | 100 | #ifdef __ia64__ |
9a5fa6e9 | 101 | continuation->backing_store_size = |
346e4402 | 102 | (char *) scm_ia64_ar_bsp(&continuation->jmpbuf.ctx) |
9a5fa6e9 | 103 | - |
346e4402 | 104 | (char *) thread->register_backing_store_base; |
193297d8 RB |
105 | continuation->backing_store = NULL; |
106 | continuation->backing_store = | |
4c9419ac MV |
107 | scm_gc_malloc (continuation->backing_store_size, |
108 | "continuation backing store"); | |
193297d8 | 109 | memcpy (continuation->backing_store, |
346e4402 | 110 | (void *) thread->register_backing_store_base, |
193297d8 | 111 | continuation->backing_store_size); |
346e4402 | 112 | #endif /* __ia64__ */ |
193297d8 RB |
113 | return cont; |
114 | } | |
115 | else | |
116 | { | |
3c468478 | 117 | SCM ret = continuation->throw_value; |
3c468478 MV |
118 | continuation->throw_value = SCM_BOOL_F; |
119 | return ret; | |
5f144b10 | 120 | } |
0f2d19dd | 121 | } |
5f144b10 | 122 | #undef FUNC_NAME |
0f2d19dd | 123 | |
d3c6aef9 MV |
124 | |
125 | /* Invoking a continuation proceeds as follows: | |
126 | * | |
127 | * - the stack is made large enough for the called continuation | |
128 | * - the old windchain is unwound down to the branching point | |
129 | * - the continuation stack is copied into place | |
130 | * - the windchain is rewound up to the continuation's context | |
131 | * - the continuation is invoked via longjmp (or setcontext) | |
132 | * | |
133 | * This order is important so that unwind and rewind handlers are run | |
134 | * with their correct stack. | |
135 | */ | |
136 | ||
5f144b10 | 137 | static void scm_dynthrow (SCM, SCM); |
01c8a3dd DH |
138 | |
139 | /* Grow the stack by a fixed amount to provide space to copy in the | |
140 | * continuation. Possibly this function has to be called several times | |
141 | * recursively before enough space is available. Make sure the compiler does | |
142 | * not optimize the growth array away by storing it's address into a global | |
143 | * variable. | |
144 | */ | |
145 | ||
92c2555f | 146 | scm_t_bits scm_i_dummy; |
1cc91f1b | 147 | |
0f2d19dd | 148 | static void |
01c8a3dd DH |
149 | grow_stack (SCM cont, SCM val) |
150 | { | |
92c2555f | 151 | scm_t_bits growth[100]; |
01c8a3dd | 152 | |
92c2555f | 153 | scm_i_dummy = (scm_t_bits) growth; |
01c8a3dd | 154 | scm_dynthrow (cont, val); |
0f2d19dd | 155 | } |
0f2d19dd | 156 | |
1cc91f1b | 157 | |
01c8a3dd DH |
158 | /* Copy the continuation stack into the current stack. Calling functions from |
159 | * within this function is safe, since only stack frames below this function's | |
160 | * own frame are overwritten. Thus, memcpy can be used for best performance. | |
161 | */ | |
d3c6aef9 MV |
162 | |
163 | typedef struct { | |
164 | scm_t_contregs *continuation; | |
165 | SCM_STACKITEM *dst; | |
166 | } copy_stack_data; | |
167 | ||
01c8a3dd | 168 | static void |
d3c6aef9 MV |
169 | copy_stack (void *data) |
170 | { | |
171 | copy_stack_data *d = (copy_stack_data *)data; | |
172 | memcpy (d->dst, d->continuation->stack, | |
173 | sizeof (SCM_STACKITEM) * d->continuation->num_stack_items); | |
bfffd258 | 174 | scm_vm_reinstate_continuations (d->continuation->vm_conts); |
346e4402 NJ |
175 | #ifdef __ia64__ |
176 | SCM_I_CURRENT_THREAD->pending_rbs_continuation = d->continuation; | |
177 | #endif | |
d3c6aef9 MV |
178 | } |
179 | ||
180 | static void | |
181 | copy_stack_and_call (scm_t_contregs *continuation, SCM val, | |
5f144b10 | 182 | SCM_STACKITEM * dst) |
0f2d19dd | 183 | { |
d3c6aef9 MV |
184 | long delta; |
185 | copy_stack_data data; | |
186 | ||
9de87eea | 187 | delta = scm_ilength (scm_i_dynwinds ()) - scm_ilength (continuation->dynenv); |
d3c6aef9 MV |
188 | data.continuation = continuation; |
189 | data.dst = dst; | |
14578fa4 | 190 | scm_i_dowinds (continuation->dynenv, delta, copy_stack, &data); |
01c8a3dd | 191 | |
5f144b10 | 192 | continuation->throw_value = val; |
a4dbe1ac | 193 | SCM_I_LONGJMP (continuation->jmpbuf, 1); |
01c8a3dd DH |
194 | } |
195 | ||
346e4402 NJ |
196 | #ifdef __ia64__ |
197 | void | |
a4dbe1ac | 198 | scm_ia64_longjmp (scm_i_jmp_buf *JB, int VAL) |
346e4402 NJ |
199 | { |
200 | scm_i_thread *t = SCM_I_CURRENT_THREAD; | |
201 | ||
202 | if (t->pending_rbs_continuation) | |
203 | { | |
204 | memcpy (t->register_backing_store_base, | |
205 | t->pending_rbs_continuation->backing_store, | |
206 | t->pending_rbs_continuation->backing_store_size); | |
207 | t->pending_rbs_continuation = NULL; | |
208 | } | |
209 | setcontext (&JB->ctx); | |
210 | } | |
211 | #endif | |
212 | ||
01c8a3dd DH |
213 | /* Call grow_stack until the stack space is large enough, then, as the current |
214 | * stack frame might get overwritten, let copy_stack_and_call perform the | |
215 | * actual copying and continuation calling. | |
216 | */ | |
217 | static void | |
218 | scm_dynthrow (SCM cont, SCM val) | |
219 | { | |
9de87eea | 220 | scm_i_thread *thread = SCM_I_CURRENT_THREAD; |
92c2555f | 221 | scm_t_contregs *continuation = SCM_CONTREGS (cont); |
9de87eea | 222 | SCM_STACKITEM *dst = thread->continuation_base; |
01c8a3dd DH |
223 | SCM_STACKITEM stack_top_element; |
224 | ||
87f30eda | 225 | if (thread->critical_section_level) |
8b7f0bb3 MV |
226 | { |
227 | fprintf (stderr, "continuation invoked from within critical section.\n"); | |
228 | abort (); | |
229 | } | |
230 | ||
4ccb2cd2 | 231 | #if SCM_STACK_GROWS_UP |
5afcf08b | 232 | if (dst + continuation->num_stack_items >= &stack_top_element) |
01c8a3dd | 233 | grow_stack (cont, val); |
0f2d19dd | 234 | #else |
5f144b10 | 235 | dst -= continuation->num_stack_items; |
c8a1bdc4 | 236 | if (dst <= &stack_top_element) |
01c8a3dd | 237 | grow_stack (cont, val); |
0f2d19dd | 238 | #endif /* def SCM_STACK_GROWS_UP */ |
01c8a3dd | 239 | |
5f144b10 GH |
240 | SCM_FLUSH_REGISTER_WINDOWS; |
241 | copy_stack_and_call (continuation, val, dst); | |
0f2d19dd JB |
242 | } |
243 | ||
db4b4ca6 DH |
244 | |
245 | static SCM | |
246 | continuation_apply (SCM cont, SCM args) | |
5f144b10 | 247 | #define FUNC_NAME "continuation_apply" |
0f2d19dd | 248 | { |
9de87eea | 249 | scm_i_thread *thread = SCM_I_CURRENT_THREAD; |
92c2555f | 250 | scm_t_contregs *continuation = SCM_CONTREGS (cont); |
5f144b10 | 251 | |
9de87eea | 252 | if (continuation->root != thread->continuation_root) |
5f144b10 | 253 | { |
9de87eea MV |
254 | SCM_MISC_ERROR |
255 | ("invoking continuation would cross continuation barrier: ~A", | |
256 | scm_list_1 (cont)); | |
5f144b10 | 257 | } |
0f2d19dd | 258 | |
ce212434 | 259 | scm_dynthrow (cont, scm_values (args)); |
0f2d19dd JB |
260 | return SCM_UNSPECIFIED; /* not reached */ |
261 | } | |
5f144b10 | 262 | #undef FUNC_NAME |
0f2d19dd | 263 | |
9de87eea MV |
264 | SCM |
265 | scm_i_with_continuation_barrier (scm_t_catch_body body, | |
266 | void *body_data, | |
267 | scm_t_catch_handler handler, | |
43e01b1e NJ |
268 | void *handler_data, |
269 | scm_t_catch_handler pre_unwind_handler, | |
270 | void *pre_unwind_handler_data) | |
9de87eea MV |
271 | { |
272 | SCM_STACKITEM stack_item; | |
273 | scm_i_thread *thread = SCM_I_CURRENT_THREAD; | |
274 | SCM old_controot; | |
275 | SCM_STACKITEM *old_contbase; | |
9de87eea MV |
276 | SCM result; |
277 | ||
278 | /* Establish a fresh continuation root. | |
279 | */ | |
280 | old_controot = thread->continuation_root; | |
281 | old_contbase = thread->continuation_base; | |
9de87eea MV |
282 | thread->continuation_root = scm_cons (thread->handle, old_controot); |
283 | thread->continuation_base = &stack_item; | |
9de87eea MV |
284 | |
285 | /* Call FUNC inside a catch all. This is now guaranteed to return | |
286 | directly and exactly once. | |
287 | */ | |
43e01b1e NJ |
288 | result = scm_c_catch (SCM_BOOL_T, |
289 | body, body_data, | |
290 | handler, handler_data, | |
291 | pre_unwind_handler, pre_unwind_handler_data); | |
9de87eea MV |
292 | |
293 | /* Return to old continuation root. | |
294 | */ | |
9de87eea MV |
295 | thread->continuation_base = old_contbase; |
296 | thread->continuation_root = old_controot; | |
297 | ||
298 | return result; | |
299 | } | |
300 | ||
301 | struct c_data { | |
302 | void *(*func) (void *); | |
303 | void *data; | |
304 | void *result; | |
305 | }; | |
306 | ||
307 | static SCM | |
308 | c_body (void *d) | |
309 | { | |
310 | struct c_data *data = (struct c_data *)d; | |
311 | data->result = data->func (data->data); | |
312 | return SCM_UNSPECIFIED; | |
313 | } | |
314 | ||
315 | static SCM | |
316 | c_handler (void *d, SCM tag, SCM args) | |
317 | { | |
318 | struct c_data *data = (struct c_data *)d; | |
9de87eea MV |
319 | data->result = NULL; |
320 | return SCM_UNSPECIFIED; | |
321 | } | |
322 | ||
323 | void * | |
324 | scm_c_with_continuation_barrier (void *(*func) (void *), void *data) | |
325 | { | |
326 | struct c_data c_data; | |
327 | c_data.func = func; | |
328 | c_data.data = data; | |
329 | scm_i_with_continuation_barrier (c_body, &c_data, | |
43e01b1e NJ |
330 | c_handler, &c_data, |
331 | scm_handle_by_message_noexit, NULL); | |
9de87eea MV |
332 | return c_data.result; |
333 | } | |
334 | ||
335 | struct scm_data { | |
336 | SCM proc; | |
337 | }; | |
338 | ||
339 | static SCM | |
340 | scm_body (void *d) | |
341 | { | |
342 | struct scm_data *data = (struct scm_data *)d; | |
343 | return scm_call_0 (data->proc); | |
344 | } | |
345 | ||
346 | static SCM | |
347 | scm_handler (void *d, SCM tag, SCM args) | |
348 | { | |
9de87eea MV |
349 | return SCM_BOOL_F; |
350 | } | |
351 | ||
352 | SCM_DEFINE (scm_with_continuation_barrier, "with-continuation-barrier", 1,0,0, | |
353 | (SCM proc), | |
69d2000d MV |
354 | "Call @var{proc} and return its result. Do not allow the invocation of\n" |
355 | "continuations that would leave or enter the dynamic extent of the call\n" | |
356 | "to @code{with-continuation-barrier}. Such an attempt causes an error\n" | |
357 | "to be signaled.\n" | |
358 | "\n" | |
359 | "Throws (such as errors) that are not caught from within @var{proc} are\n" | |
360 | "caught by @code{with-continuation-barrier}. In that case, a short\n" | |
361 | "message is printed to the current error port and @code{#f} is returned.\n" | |
362 | "\n" | |
363 | "Thus, @code{with-continuation-barrier} returns exactly once.\n") | |
9de87eea MV |
364 | #define FUNC_NAME s_scm_with_continuation_barrier |
365 | { | |
366 | struct scm_data scm_data; | |
367 | scm_data.proc = proc; | |
368 | return scm_i_with_continuation_barrier (scm_body, &scm_data, | |
43e01b1e NJ |
369 | scm_handler, &scm_data, |
370 | scm_handle_by_message_noexit, NULL); | |
9de87eea MV |
371 | } |
372 | #undef FUNC_NAME | |
db4b4ca6 | 373 | |
0f2d19dd JB |
374 | void |
375 | scm_init_continuations () | |
0f2d19dd | 376 | { |
5f144b10 | 377 | scm_tc16_continuation = scm_make_smob_type ("continuation", 0); |
5f144b10 GH |
378 | scm_set_smob_print (scm_tc16_continuation, continuation_print); |
379 | scm_set_smob_apply (scm_tc16_continuation, continuation_apply, 0, 0, 1); | |
a0599745 | 380 | #include "libguile/continuations.x" |
0f2d19dd JB |
381 | } |
382 | ||
89e00824 ML |
383 | /* |
384 | Local Variables: | |
385 | c-file-style: "gnu" | |
386 | End: | |
387 | */ |