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