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