1 /* Copyright (C) 1995,1996,1998,2000,2001,2004, 2006 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libguile/_scm.h"
26 #include "libguile/async.h"
27 #include "libguile/debug.h"
28 #include "libguile/root.h"
29 #include "libguile/stackchk.h"
30 #include "libguile/smob.h"
31 #include "libguile/ports.h"
32 #include "libguile/dynwind.h"
33 #include "libguile/values.h"
34 #include "libguile/eval.h"
36 #include "libguile/validate.h"
37 #include "libguile/continuations.h"
44 scm_t_bits scm_tc16_continuation
;
47 continuation_mark (SCM obj
)
49 scm_t_contregs
*continuation
= SCM_CONTREGS (obj
);
51 scm_gc_mark (continuation
->root
);
52 scm_gc_mark (continuation
->throw_value
);
53 scm_mark_locations (continuation
->stack
, continuation
->num_stack_items
);
55 if (continuation
->backing_store
)
56 scm_mark_locations (continuation
->backing_store
,
57 continuation
->backing_store_size
/
58 sizeof (SCM_STACKITEM
));
60 return continuation
->dynenv
;
64 continuation_free (SCM obj
)
66 scm_t_contregs
*continuation
= SCM_CONTREGS (obj
);
67 /* stack array size is 1 if num_stack_items is 0. */
68 size_t extra_items
= (continuation
->num_stack_items
> 0)
69 ? (continuation
->num_stack_items
- 1)
71 size_t bytes_free
= sizeof (scm_t_contregs
)
72 + extra_items
* sizeof (SCM_STACKITEM
);
75 scm_gc_free (continuation
->backing_store
, continuation
->backing_store_size
,
76 "continuation backing store");
78 scm_gc_free (continuation
, bytes_free
, "continuation");
83 continuation_print (SCM obj
, SCM port
, scm_print_state
*state SCM_UNUSED
)
85 scm_t_contregs
*continuation
= SCM_CONTREGS (obj
);
87 scm_puts ("#<continuation ", port
);
88 scm_intprint (continuation
->num_stack_items
, 10, port
);
89 scm_puts (" @ ", port
);
90 scm_uintprint (SCM_CELL_WORD_1 (obj
), 16, port
);
96 /* Extern declaration of getcontext()/setcontext() in order to redefine
97 getcontext() since on ia64-linux the second return value indicates whether
98 it returned from getcontext() itself or by running setcontext(). */
106 __attribute__ ((returns_twice
))
107 #endif /* __GNUC__ */
108 extern struct rv
ia64_getcontext (ucontext_t
*) __asm__ ("getcontext");
109 #endif /* __ia64__ */
111 /* this may return more than once: the first time with the escape
112 procedure, then subsequently with the value to be passed to the
114 #define FUNC_NAME "scm_make_continuation"
116 scm_make_continuation (int *first
)
118 scm_i_thread
*thread
= SCM_I_CURRENT_THREAD
;
120 scm_t_contregs
*continuation
;
125 #endif /* __ia64__ */
127 SCM_FLUSH_REGISTER_WINDOWS
;
128 stack_size
= scm_stack_size (thread
->continuation_base
);
129 continuation
= scm_gc_malloc (sizeof (scm_t_contregs
)
130 + (stack_size
- 1) * sizeof (SCM_STACKITEM
),
132 continuation
->num_stack_items
= stack_size
;
133 continuation
->dynenv
= scm_i_dynwinds ();
134 continuation
->throw_value
= SCM_EOL
;
135 continuation
->root
= thread
->continuation_root
;
136 continuation
->dframe
= scm_i_last_debug_frame ();
137 src
= thread
->continuation_base
;
138 SCM_NEWSMOB (cont
, scm_tc16_continuation
, continuation
);
140 #if ! SCM_STACK_GROWS_UP
143 continuation
->offset
= continuation
->stack
- src
;
144 memcpy (continuation
->stack
, src
, sizeof (SCM_STACKITEM
) * stack_size
);
147 rv
= ia64_getcontext (&continuation
->ctx
);
150 continuation
->backing_store_size
=
151 continuation
->ctx
.uc_mcontext
.sc_ar_bsp
-
152 (unsigned long) __libc_ia64_register_backing_store_base
;
153 continuation
->backing_store
= NULL
;
154 continuation
->backing_store
=
155 scm_gc_malloc (continuation
->backing_store_size
,
156 "continuation backing store");
157 memcpy (continuation
->backing_store
,
158 (void *) __libc_ia64_register_backing_store_base
,
159 continuation
->backing_store_size
);
165 SCM ret
= continuation
->throw_value
;
167 continuation
->throw_value
= SCM_BOOL_F
;
170 #else /* !__ia64__ */
171 if (setjmp (continuation
->jmpbuf
))
173 SCM ret
= continuation
->throw_value
;
175 continuation
->throw_value
= SCM_BOOL_F
;
183 #endif /* !__ia64__ */
188 /* Invoking a continuation proceeds as follows:
190 * - the stack is made large enough for the called continuation
191 * - the old windchain is unwound down to the branching point
192 * - the continuation stack is copied into place
193 * - the windchain is rewound up to the continuation's context
194 * - the continuation is invoked via longjmp (or setcontext)
196 * This order is important so that unwind and rewind handlers are run
197 * with their correct stack.
200 static void scm_dynthrow (SCM
, SCM
);
202 /* Grow the stack by a fixed amount to provide space to copy in the
203 * continuation. Possibly this function has to be called several times
204 * recursively before enough space is available. Make sure the compiler does
205 * not optimize the growth array away by storing it's address into a global
209 scm_t_bits scm_i_dummy
;
212 grow_stack (SCM cont
, SCM val
)
214 scm_t_bits growth
[100];
216 scm_i_dummy
= (scm_t_bits
) growth
;
217 scm_dynthrow (cont
, val
);
221 /* Copy the continuation stack into the current stack. Calling functions from
222 * within this function is safe, since only stack frames below this function's
223 * own frame are overwritten. Thus, memcpy can be used for best performance.
227 scm_t_contregs
*continuation
;
232 copy_stack (void *data
)
234 copy_stack_data
*d
= (copy_stack_data
*)data
;
235 memcpy (d
->dst
, d
->continuation
->stack
,
236 sizeof (SCM_STACKITEM
) * d
->continuation
->num_stack_items
);
240 copy_stack_and_call (scm_t_contregs
*continuation
, SCM val
,
244 copy_stack_data data
;
246 delta
= scm_ilength (scm_i_dynwinds ()) - scm_ilength (continuation
->dynenv
);
247 data
.continuation
= continuation
;
249 scm_i_dowinds (continuation
->dynenv
, delta
, copy_stack
, &data
);
251 scm_i_set_last_debug_frame (continuation
->dframe
);
253 continuation
->throw_value
= val
;
255 memcpy ((void *) __libc_ia64_register_backing_store_base
,
256 continuation
->backing_store
,
257 continuation
->backing_store_size
);
258 setcontext (&continuation
->ctx
);
260 longjmp (continuation
->jmpbuf
, 1);
264 /* Call grow_stack until the stack space is large enough, then, as the current
265 * stack frame might get overwritten, let copy_stack_and_call perform the
266 * actual copying and continuation calling.
269 scm_dynthrow (SCM cont
, SCM val
)
271 scm_i_thread
*thread
= SCM_I_CURRENT_THREAD
;
272 scm_t_contregs
*continuation
= SCM_CONTREGS (cont
);
273 SCM_STACKITEM
*dst
= thread
->continuation_base
;
274 SCM_STACKITEM stack_top_element
;
276 if (scm_i_critical_section_level
)
278 fprintf (stderr
, "continuation invoked from within critical section.\n");
282 #if SCM_STACK_GROWS_UP
283 if (dst
+ continuation
->num_stack_items
>= &stack_top_element
)
284 grow_stack (cont
, val
);
286 dst
-= continuation
->num_stack_items
;
287 if (dst
<= &stack_top_element
)
288 grow_stack (cont
, val
);
289 #endif /* def SCM_STACK_GROWS_UP */
291 SCM_FLUSH_REGISTER_WINDOWS
;
292 copy_stack_and_call (continuation
, val
, dst
);
297 continuation_apply (SCM cont
, SCM args
)
298 #define FUNC_NAME "continuation_apply"
300 scm_i_thread
*thread
= SCM_I_CURRENT_THREAD
;
301 scm_t_contregs
*continuation
= SCM_CONTREGS (cont
);
303 if (continuation
->root
!= thread
->continuation_root
)
306 ("invoking continuation would cross continuation barrier: ~A",
310 scm_dynthrow (cont
, scm_values (args
));
311 return SCM_UNSPECIFIED
; /* not reached */
316 scm_i_with_continuation_barrier (scm_t_catch_body body
,
318 scm_t_catch_handler handler
,
320 scm_t_catch_handler pre_unwind_handler
,
321 void *pre_unwind_handler_data
)
323 SCM_STACKITEM stack_item
;
324 scm_i_thread
*thread
= SCM_I_CURRENT_THREAD
;
326 SCM_STACKITEM
*old_contbase
;
327 scm_t_debug_frame
*old_lastframe
;
330 /* Establish a fresh continuation root.
332 old_controot
= thread
->continuation_root
;
333 old_contbase
= thread
->continuation_base
;
334 old_lastframe
= thread
->last_debug_frame
;
335 thread
->continuation_root
= scm_cons (thread
->handle
, old_controot
);
336 thread
->continuation_base
= &stack_item
;
337 thread
->last_debug_frame
= NULL
;
339 /* Call FUNC inside a catch all. This is now guaranteed to return
340 directly and exactly once.
342 result
= scm_c_catch (SCM_BOOL_T
,
344 handler
, handler_data
,
345 pre_unwind_handler
, pre_unwind_handler_data
);
347 /* Return to old continuation root.
349 thread
->last_debug_frame
= old_lastframe
;
350 thread
->continuation_base
= old_contbase
;
351 thread
->continuation_root
= old_controot
;
357 void *(*func
) (void *);
365 struct c_data
*data
= (struct c_data
*)d
;
366 data
->result
= data
->func (data
->data
);
367 return SCM_UNSPECIFIED
;
371 c_handler (void *d
, SCM tag
, SCM args
)
373 struct c_data
*data
= (struct c_data
*)d
;
375 return SCM_UNSPECIFIED
;
379 scm_c_with_continuation_barrier (void *(*func
) (void *), void *data
)
381 struct c_data c_data
;
384 scm_i_with_continuation_barrier (c_body
, &c_data
,
386 scm_handle_by_message_noexit
, NULL
);
387 return c_data
.result
;
397 struct scm_data
*data
= (struct scm_data
*)d
;
398 return scm_call_0 (data
->proc
);
402 scm_handler (void *d
, SCM tag
, SCM args
)
407 SCM_DEFINE (scm_with_continuation_barrier
, "with-continuation-barrier", 1,0,0,
409 "Call @var{proc} and return its result. Do not allow the invocation of\n"
410 "continuations that would leave or enter the dynamic extent of the call\n"
411 "to @code{with-continuation-barrier}. Such an attempt causes an error\n"
414 "Throws (such as errors) that are not caught from within @var{proc} are\n"
415 "caught by @code{with-continuation-barrier}. In that case, a short\n"
416 "message is printed to the current error port and @code{#f} is returned.\n"
418 "Thus, @code{with-continuation-barrier} returns exactly once.\n")
419 #define FUNC_NAME s_scm_with_continuation_barrier
421 struct scm_data scm_data
;
422 scm_data
.proc
= proc
;
423 return scm_i_with_continuation_barrier (scm_body
, &scm_data
,
424 scm_handler
, &scm_data
,
425 scm_handle_by_message_noexit
, NULL
);
430 scm_init_continuations ()
432 scm_tc16_continuation
= scm_make_smob_type ("continuation", 0);
433 scm_set_smob_mark (scm_tc16_continuation
, continuation_mark
);
434 scm_set_smob_free (scm_tc16_continuation
, continuation_free
);
435 scm_set_smob_print (scm_tc16_continuation
, continuation_print
);
436 scm_set_smob_apply (scm_tc16_continuation
, continuation_apply
, 0, 0, 1);
437 #include "libguile/continuations.x"