(gds-socket-type-alist): New.
[bpt/guile.git] / libguile / continuations.c
CommitLineData
2b829bbb 1/* Copyright (C) 1995,1996,1998,2000,2001,2004, 2006 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};
08e5f840
RB
104
105#ifdef __GNUC__
106__attribute__ ((returns_twice))
107#endif /* __GNUC__ */
78b6566e 108extern struct rv ia64_getcontext (ucontext_t *) __asm__ ("getcontext");
193297d8
RB
109#endif /* __ia64__ */
110
5f144b10
GH
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
113 continuation. */
114#define FUNC_NAME "scm_make_continuation"
0f2d19dd 115SCM
5f144b10 116scm_make_continuation (int *first)
0f2d19dd 117{
9de87eea
MV
118 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
119 SCM cont;
92c2555f 120 scm_t_contregs *continuation;
c014a02e 121 long stack_size;
01c8a3dd 122 SCM_STACKITEM * src;
193297d8
RB
123#ifdef __ia64__
124 struct rv rv;
87855fa2 125#endif /* __ia64__ */
0f2d19dd 126
0f2d19dd 127 SCM_FLUSH_REGISTER_WINDOWS;
9de87eea 128 stack_size = scm_stack_size (thread->continuation_base);
4c9419ac
MV
129 continuation = scm_gc_malloc (sizeof (scm_t_contregs)
130 + (stack_size - 1) * sizeof (SCM_STACKITEM),
131 "continuation");
5f144b10 132 continuation->num_stack_items = stack_size;
9de87eea 133 continuation->dynenv = scm_i_dynwinds ();
5f144b10 134 continuation->throw_value = SCM_EOL;
9de87eea
MV
135 continuation->root = thread->continuation_root;
136 continuation->dframe = scm_i_last_debug_frame ();
137 src = thread->continuation_base;
5f144b10 138 SCM_NEWSMOB (cont, scm_tc16_continuation, continuation);
01c8a3dd 139
4ccb2cd2 140#if ! SCM_STACK_GROWS_UP
5f144b10
GH
141 src -= stack_size;
142#endif
5c5c27dc 143 continuation->offset = continuation->stack - src;
5f144b10
GH
144 memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size);
145
193297d8 146#ifdef __ia64__
78b6566e 147 rv = ia64_getcontext (&continuation->ctx);
193297d8
RB
148 if (rv.first_return)
149 {
150 continuation->backing_store_size =
151 continuation->ctx.uc_mcontext.sc_ar_bsp -
87855fa2 152 (unsigned long) __libc_ia64_register_backing_store_base;
193297d8
RB
153 continuation->backing_store = NULL;
154 continuation->backing_store =
4c9419ac
MV
155 scm_gc_malloc (continuation->backing_store_size,
156 "continuation backing store");
193297d8
RB
157 memcpy (continuation->backing_store,
158 (void *) __libc_ia64_register_backing_store_base,
159 continuation->backing_store_size);
160 *first = 1;
161 return cont;
162 }
163 else
164 {
3c468478 165 SCM ret = continuation->throw_value;
193297d8 166 *first = 0;
3c468478
MV
167 continuation->throw_value = SCM_BOOL_F;
168 return ret;
193297d8
RB
169 }
170#else /* !__ia64__ */
5f144b10
GH
171 if (setjmp (continuation->jmpbuf))
172 {
3c468478 173 SCM ret = continuation->throw_value;
5f144b10 174 *first = 0;
3c468478
MV
175 continuation->throw_value = SCM_BOOL_F;
176 return ret;
5f144b10
GH
177 }
178 else
179 {
180 *first = 1;
181 return cont;
182 }
193297d8 183#endif /* !__ia64__ */
0f2d19dd 184}
5f144b10 185#undef FUNC_NAME
0f2d19dd 186
d3c6aef9
MV
187
188/* Invoking a continuation proceeds as follows:
189 *
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)
195 *
196 * This order is important so that unwind and rewind handlers are run
197 * with their correct stack.
198 */
199
5f144b10 200static void scm_dynthrow (SCM, SCM);
01c8a3dd
DH
201
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
206 * variable.
207 */
208
92c2555f 209scm_t_bits scm_i_dummy;
1cc91f1b 210
0f2d19dd 211static void
01c8a3dd
DH
212grow_stack (SCM cont, SCM val)
213{
92c2555f 214 scm_t_bits growth[100];
01c8a3dd 215
92c2555f 216 scm_i_dummy = (scm_t_bits) growth;
01c8a3dd 217 scm_dynthrow (cont, val);
0f2d19dd 218}
0f2d19dd 219
1cc91f1b 220
01c8a3dd
DH
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.
224 */
d3c6aef9
MV
225
226typedef struct {
227 scm_t_contregs *continuation;
228 SCM_STACKITEM *dst;
229} copy_stack_data;
230
01c8a3dd 231static void
d3c6aef9
MV
232copy_stack (void *data)
233{
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);
237}
238
239static void
240copy_stack_and_call (scm_t_contregs *continuation, SCM val,
5f144b10 241 SCM_STACKITEM * dst)
0f2d19dd 242{
d3c6aef9
MV
243 long delta;
244 copy_stack_data data;
245
9de87eea 246 delta = scm_ilength (scm_i_dynwinds ()) - scm_ilength (continuation->dynenv);
d3c6aef9
MV
247 data.continuation = continuation;
248 data.dst = dst;
14578fa4 249 scm_i_dowinds (continuation->dynenv, delta, copy_stack, &data);
01c8a3dd 250
9de87eea 251 scm_i_set_last_debug_frame (continuation->dframe);
01c8a3dd 252
5f144b10 253 continuation->throw_value = val;
193297d8
RB
254#ifdef __ia64__
255 memcpy ((void *) __libc_ia64_register_backing_store_base,
256 continuation->backing_store,
257 continuation->backing_store_size);
258 setcontext (&continuation->ctx);
259#else
5f144b10 260 longjmp (continuation->jmpbuf, 1);
193297d8 261#endif
01c8a3dd
DH
262}
263
01c8a3dd
DH
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.
267 */
268static void
269scm_dynthrow (SCM cont, SCM val)
270{
9de87eea 271 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
92c2555f 272 scm_t_contregs *continuation = SCM_CONTREGS (cont);
9de87eea 273 SCM_STACKITEM *dst = thread->continuation_base;
01c8a3dd
DH
274 SCM_STACKITEM stack_top_element;
275
8b7f0bb3
MV
276 if (scm_i_critical_section_level)
277 {
278 fprintf (stderr, "continuation invoked from within critical section.\n");
279 abort ();
280 }
281
4ccb2cd2 282#if SCM_STACK_GROWS_UP
5afcf08b 283 if (dst + continuation->num_stack_items >= &stack_top_element)
01c8a3dd 284 grow_stack (cont, val);
0f2d19dd 285#else
5f144b10 286 dst -= continuation->num_stack_items;
c8a1bdc4 287 if (dst <= &stack_top_element)
01c8a3dd 288 grow_stack (cont, val);
0f2d19dd 289#endif /* def SCM_STACK_GROWS_UP */
01c8a3dd 290
5f144b10
GH
291 SCM_FLUSH_REGISTER_WINDOWS;
292 copy_stack_and_call (continuation, val, dst);
0f2d19dd
JB
293}
294
db4b4ca6
DH
295
296static SCM
297continuation_apply (SCM cont, SCM args)
5f144b10 298#define FUNC_NAME "continuation_apply"
0f2d19dd 299{
9de87eea 300 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
92c2555f 301 scm_t_contregs *continuation = SCM_CONTREGS (cont);
5f144b10 302
9de87eea 303 if (continuation->root != thread->continuation_root)
5f144b10 304 {
9de87eea
MV
305 SCM_MISC_ERROR
306 ("invoking continuation would cross continuation barrier: ~A",
307 scm_list_1 (cont));
5f144b10 308 }
0f2d19dd 309
ce212434 310 scm_dynthrow (cont, scm_values (args));
0f2d19dd
JB
311 return SCM_UNSPECIFIED; /* not reached */
312}
5f144b10 313#undef FUNC_NAME
0f2d19dd 314
9de87eea
MV
315SCM
316scm_i_with_continuation_barrier (scm_t_catch_body body,
317 void *body_data,
318 scm_t_catch_handler handler,
43e01b1e
NJ
319 void *handler_data,
320 scm_t_catch_handler pre_unwind_handler,
321 void *pre_unwind_handler_data)
9de87eea
MV
322{
323 SCM_STACKITEM stack_item;
324 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
325 SCM old_controot;
326 SCM_STACKITEM *old_contbase;
327 scm_t_debug_frame *old_lastframe;
328 SCM result;
329
330 /* Establish a fresh continuation root.
331 */
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;
338
339 /* Call FUNC inside a catch all. This is now guaranteed to return
340 directly and exactly once.
341 */
43e01b1e
NJ
342 result = scm_c_catch (SCM_BOOL_T,
343 body, body_data,
344 handler, handler_data,
345 pre_unwind_handler, pre_unwind_handler_data);
9de87eea
MV
346
347 /* Return to old continuation root.
348 */
349 thread->last_debug_frame = old_lastframe;
350 thread->continuation_base = old_contbase;
351 thread->continuation_root = old_controot;
352
353 return result;
354}
355
356struct c_data {
357 void *(*func) (void *);
358 void *data;
359 void *result;
360};
361
362static SCM
363c_body (void *d)
364{
365 struct c_data *data = (struct c_data *)d;
366 data->result = data->func (data->data);
367 return SCM_UNSPECIFIED;
368}
369
370static SCM
371c_handler (void *d, SCM tag, SCM args)
372{
373 struct c_data *data = (struct c_data *)d;
9de87eea
MV
374 data->result = NULL;
375 return SCM_UNSPECIFIED;
376}
377
378void *
379scm_c_with_continuation_barrier (void *(*func) (void *), void *data)
380{
381 struct c_data c_data;
382 c_data.func = func;
383 c_data.data = data;
384 scm_i_with_continuation_barrier (c_body, &c_data,
43e01b1e
NJ
385 c_handler, &c_data,
386 scm_handle_by_message_noexit, NULL);
9de87eea
MV
387 return c_data.result;
388}
389
390struct scm_data {
391 SCM proc;
392};
393
394static SCM
395scm_body (void *d)
396{
397 struct scm_data *data = (struct scm_data *)d;
398 return scm_call_0 (data->proc);
399}
400
401static SCM
402scm_handler (void *d, SCM tag, SCM args)
403{
9de87eea
MV
404 return SCM_BOOL_F;
405}
406
407SCM_DEFINE (scm_with_continuation_barrier, "with-continuation-barrier", 1,0,0,
408 (SCM proc),
69d2000d
MV
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"
412"to be signaled.\n"
413"\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"
417"\n"
418"Thus, @code{with-continuation-barrier} returns exactly once.\n")
9de87eea
MV
419#define FUNC_NAME s_scm_with_continuation_barrier
420{
421 struct scm_data scm_data;
422 scm_data.proc = proc;
423 return scm_i_with_continuation_barrier (scm_body, &scm_data,
43e01b1e
NJ
424 scm_handler, &scm_data,
425 scm_handle_by_message_noexit, NULL);
9de87eea
MV
426}
427#undef FUNC_NAME
db4b4ca6 428
0f2d19dd
JB
429void
430scm_init_continuations ()
0f2d19dd 431{
5f144b10
GH
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);
a0599745 437#include "libguile/continuations.x"
0f2d19dd
JB
438}
439
89e00824
ML
440/*
441 Local Variables:
442 c-file-style: "gnu"
443 End:
444*/