Merge commit 'f30e1bdf97ae8b2b2918da585f887a4d3a23a347' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / continuations.c
1 /* Copyright (C) 1995,1996,1998,2000,2001,2004, 2006 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 */
17
18
19 \f
20
21 #include "libguile/_scm.h"
22
23 #include <string.h>
24 #include <stdio.h>
25
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"
35
36 #include "libguile/validate.h"
37 #include "libguile/continuations.h"
38
39 \f
40
41 /* {Continuations}
42 */
43
44 scm_t_bits scm_tc16_continuation;
45
46
47 static int
48 continuation_print (SCM obj, SCM port, scm_print_state *state SCM_UNUSED)
49 {
50 scm_t_contregs *continuation = SCM_CONTREGS (obj);
51
52 scm_puts ("#<continuation ", port);
53 scm_intprint (continuation->num_stack_items, 10, port);
54 scm_puts (" @ ", port);
55 scm_uintprint (SCM_CELL_WORD_1 (obj), 16, port);
56 scm_putc ('>', port);
57 return 1;
58 }
59
60 /* this may return more than once: the first time with the escape
61 procedure, then subsequently with the value to be passed to the
62 continuation. */
63 #define FUNC_NAME "scm_make_continuation"
64 SCM
65 scm_make_continuation (int *first)
66 {
67 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
68 SCM cont;
69 scm_t_contregs *continuation;
70 long stack_size;
71 SCM_STACKITEM * src;
72
73 SCM_FLUSH_REGISTER_WINDOWS;
74 stack_size = scm_stack_size (thread->continuation_base);
75 continuation = scm_gc_malloc (sizeof (scm_t_contregs)
76 + (stack_size - 1) * sizeof (SCM_STACKITEM),
77 "continuation");
78 continuation->num_stack_items = stack_size;
79 continuation->dynenv = scm_i_dynwinds ();
80 continuation->throw_value = SCM_EOL;
81 continuation->root = thread->continuation_root;
82 continuation->dframe = scm_i_last_debug_frame ();
83 src = thread->continuation_base;
84 SCM_NEWSMOB (cont, scm_tc16_continuation, continuation);
85
86 #if ! SCM_STACK_GROWS_UP
87 src -= stack_size;
88 #endif
89 continuation->offset = continuation->stack - src;
90 memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size);
91
92 #ifdef __ia64__
93 continuation->fresh = 1;
94 getcontext (&continuation->ctx);
95 if (continuation->fresh)
96 {
97 continuation->backing_store_size =
98 (char *) scm_ia64_ar_bsp(&continuation->ctx)
99 -
100 (char *) scm_ia64_register_backing_store_base ();
101 continuation->backing_store = NULL;
102 continuation->backing_store =
103 scm_gc_malloc (continuation->backing_store_size,
104 "continuation backing store");
105 memcpy (continuation->backing_store,
106 (void *) scm_ia64_register_backing_store_base (),
107 continuation->backing_store_size);
108 *first = 1;
109 continuation->fresh = 0;
110 return cont;
111 }
112 else
113 {
114 SCM ret = continuation->throw_value;
115 *first = 0;
116 continuation->throw_value = SCM_BOOL_F;
117 return ret;
118 }
119 #else /* !__ia64__ */
120 if (setjmp (continuation->jmpbuf))
121 {
122 SCM ret = continuation->throw_value;
123 *first = 0;
124 continuation->throw_value = SCM_BOOL_F;
125 return ret;
126 }
127 else
128 {
129 *first = 1;
130 return cont;
131 }
132 #endif /* !__ia64__ */
133 }
134 #undef FUNC_NAME
135
136
137 /* Invoking a continuation proceeds as follows:
138 *
139 * - the stack is made large enough for the called continuation
140 * - the old windchain is unwound down to the branching point
141 * - the continuation stack is copied into place
142 * - the windchain is rewound up to the continuation's context
143 * - the continuation is invoked via longjmp (or setcontext)
144 *
145 * This order is important so that unwind and rewind handlers are run
146 * with their correct stack.
147 */
148
149 static void scm_dynthrow (SCM, SCM);
150
151 /* Grow the stack by a fixed amount to provide space to copy in the
152 * continuation. Possibly this function has to be called several times
153 * recursively before enough space is available. Make sure the compiler does
154 * not optimize the growth array away by storing it's address into a global
155 * variable.
156 */
157
158 scm_t_bits scm_i_dummy;
159
160 static void
161 grow_stack (SCM cont, SCM val)
162 {
163 scm_t_bits growth[100];
164
165 scm_i_dummy = (scm_t_bits) growth;
166 scm_dynthrow (cont, val);
167 }
168
169
170 /* Copy the continuation stack into the current stack. Calling functions from
171 * within this function is safe, since only stack frames below this function's
172 * own frame are overwritten. Thus, memcpy can be used for best performance.
173 */
174
175 typedef struct {
176 scm_t_contregs *continuation;
177 SCM_STACKITEM *dst;
178 } copy_stack_data;
179
180 static void
181 copy_stack (void *data)
182 {
183 copy_stack_data *d = (copy_stack_data *)data;
184 memcpy (d->dst, d->continuation->stack,
185 sizeof (SCM_STACKITEM) * d->continuation->num_stack_items);
186 }
187
188 static void
189 copy_stack_and_call (scm_t_contregs *continuation, SCM val,
190 SCM_STACKITEM * dst)
191 {
192 long delta;
193 copy_stack_data data;
194
195 delta = scm_ilength (scm_i_dynwinds ()) - scm_ilength (continuation->dynenv);
196 data.continuation = continuation;
197 data.dst = dst;
198 scm_i_dowinds (continuation->dynenv, delta, copy_stack, &data);
199
200 scm_i_set_last_debug_frame (continuation->dframe);
201
202 continuation->throw_value = val;
203 #ifdef __ia64__
204 memcpy (scm_ia64_register_backing_store_base (),
205 continuation->backing_store,
206 continuation->backing_store_size);
207 setcontext (&continuation->ctx);
208 #else
209 longjmp (continuation->jmpbuf, 1);
210 #endif
211 }
212
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 {
220 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
221 scm_t_contregs *continuation = SCM_CONTREGS (cont);
222 SCM_STACKITEM *dst = thread->continuation_base;
223 SCM_STACKITEM stack_top_element;
224
225 if (scm_i_critical_section_level)
226 {
227 fprintf (stderr, "continuation invoked from within critical section.\n");
228 abort ();
229 }
230
231 #if SCM_STACK_GROWS_UP
232 if (dst + continuation->num_stack_items >= &stack_top_element)
233 grow_stack (cont, val);
234 #else
235 dst -= continuation->num_stack_items;
236 if (dst <= &stack_top_element)
237 grow_stack (cont, val);
238 #endif /* def SCM_STACK_GROWS_UP */
239
240 SCM_FLUSH_REGISTER_WINDOWS;
241 copy_stack_and_call (continuation, val, dst);
242 }
243
244
245 static SCM
246 continuation_apply (SCM cont, SCM args)
247 #define FUNC_NAME "continuation_apply"
248 {
249 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
250 scm_t_contregs *continuation = SCM_CONTREGS (cont);
251
252 if (continuation->root != thread->continuation_root)
253 {
254 SCM_MISC_ERROR
255 ("invoking continuation would cross continuation barrier: ~A",
256 scm_list_1 (cont));
257 }
258
259 scm_dynthrow (cont, scm_values (args));
260 return SCM_UNSPECIFIED; /* not reached */
261 }
262 #undef FUNC_NAME
263
264 SCM
265 scm_i_with_continuation_barrier (scm_t_catch_body body,
266 void *body_data,
267 scm_t_catch_handler handler,
268 void *handler_data,
269 scm_t_catch_handler pre_unwind_handler,
270 void *pre_unwind_handler_data)
271 {
272 SCM_STACKITEM stack_item;
273 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
274 SCM old_controot;
275 SCM_STACKITEM *old_contbase;
276 scm_t_debug_frame *old_lastframe;
277 SCM result;
278
279 /* Establish a fresh continuation root.
280 */
281 old_controot = thread->continuation_root;
282 old_contbase = thread->continuation_base;
283 old_lastframe = thread->last_debug_frame;
284 thread->continuation_root = scm_cons (thread->handle, old_controot);
285 thread->continuation_base = &stack_item;
286 thread->last_debug_frame = NULL;
287
288 /* Call FUNC inside a catch all. This is now guaranteed to return
289 directly and exactly once.
290 */
291 result = scm_c_catch (SCM_BOOL_T,
292 body, body_data,
293 handler, handler_data,
294 pre_unwind_handler, pre_unwind_handler_data);
295
296 /* Return to old continuation root.
297 */
298 thread->last_debug_frame = old_lastframe;
299 thread->continuation_base = old_contbase;
300 thread->continuation_root = old_controot;
301
302 return result;
303 }
304
305 struct c_data {
306 void *(*func) (void *);
307 void *data;
308 void *result;
309 };
310
311 static SCM
312 c_body (void *d)
313 {
314 struct c_data *data = (struct c_data *)d;
315 data->result = data->func (data->data);
316 return SCM_UNSPECIFIED;
317 }
318
319 static SCM
320 c_handler (void *d, SCM tag, SCM args)
321 {
322 struct c_data *data = (struct c_data *)d;
323 data->result = NULL;
324 return SCM_UNSPECIFIED;
325 }
326
327 void *
328 scm_c_with_continuation_barrier (void *(*func) (void *), void *data)
329 {
330 struct c_data c_data;
331 c_data.func = func;
332 c_data.data = data;
333 scm_i_with_continuation_barrier (c_body, &c_data,
334 c_handler, &c_data,
335 scm_handle_by_message_noexit, NULL);
336 return c_data.result;
337 }
338
339 struct scm_data {
340 SCM proc;
341 };
342
343 static SCM
344 scm_body (void *d)
345 {
346 struct scm_data *data = (struct scm_data *)d;
347 return scm_call_0 (data->proc);
348 }
349
350 static SCM
351 scm_handler (void *d, SCM tag, SCM args)
352 {
353 return SCM_BOOL_F;
354 }
355
356 SCM_DEFINE (scm_with_continuation_barrier, "with-continuation-barrier", 1,0,0,
357 (SCM proc),
358 "Call @var{proc} and return its result. Do not allow the invocation of\n"
359 "continuations that would leave or enter the dynamic extent of the call\n"
360 "to @code{with-continuation-barrier}. Such an attempt causes an error\n"
361 "to be signaled.\n"
362 "\n"
363 "Throws (such as errors) that are not caught from within @var{proc} are\n"
364 "caught by @code{with-continuation-barrier}. In that case, a short\n"
365 "message is printed to the current error port and @code{#f} is returned.\n"
366 "\n"
367 "Thus, @code{with-continuation-barrier} returns exactly once.\n")
368 #define FUNC_NAME s_scm_with_continuation_barrier
369 {
370 struct scm_data scm_data;
371 scm_data.proc = proc;
372 return scm_i_with_continuation_barrier (scm_body, &scm_data,
373 scm_handler, &scm_data,
374 scm_handle_by_message_noexit, NULL);
375 }
376 #undef FUNC_NAME
377
378 void
379 scm_init_continuations ()
380 {
381 scm_tc16_continuation = scm_make_smob_type ("continuation", 0);
382 scm_set_smob_print (scm_tc16_continuation, continuation_print);
383 scm_set_smob_apply (scm_tc16_continuation, continuation_apply, 0, 0, 1);
384 #include "libguile/continuations.x"
385 }
386
387 /*
388 Local Variables:
389 c-file-style: "gnu"
390 End:
391 */