See ChangeLog from 2005-03-02.
[bpt/guile.git] / libguile / continuations.c
1 /* Copyright (C) 1995,1996,1998,2000,2001,2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18
19 \f
20
21 #include "libguile/_scm.h"
22
23 #include <string.h>
24
25 #include "libguile/debug.h"
26 #include "libguile/root.h"
27 #include "libguile/stackchk.h"
28 #include "libguile/smob.h"
29 #include "libguile/ports.h"
30 #include "libguile/dynwind.h"
31 #include "libguile/values.h"
32 #include "libguile/eval.h"
33
34 #include "libguile/validate.h"
35 #include "libguile/continuations.h"
36
37 \f
38
39 /* {Continuations}
40 */
41
42 scm_t_bits scm_tc16_continuation;
43
44 static SCM
45 continuation_mark (SCM obj)
46 {
47 scm_t_contregs *continuation = SCM_CONTREGS (obj);
48
49 scm_gc_mark (continuation->root);
50 scm_gc_mark (continuation->throw_value);
51 scm_mark_locations (continuation->stack, continuation->num_stack_items);
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__ */
58 return continuation->dynenv;
59 }
60
61 static size_t
62 continuation_free (SCM obj)
63 {
64 scm_t_contregs *continuation = SCM_CONTREGS (obj);
65 /* stack array size is 1 if num_stack_items is 0. */
66 size_t extra_items = (continuation->num_stack_items > 0)
67 ? (continuation->num_stack_items - 1)
68 : 0;
69 size_t bytes_free = sizeof (scm_t_contregs)
70 + extra_items * sizeof (SCM_STACKITEM);
71
72 #ifdef __ia64__
73 scm_gc_free (continuation->backing_store, continuation->backing_store_size,
74 "continuation backing store");
75 #endif /* __ia64__ */
76 scm_gc_free (continuation, bytes_free, "continuation");
77 return 0;
78 }
79
80 static int
81 continuation_print (SCM obj, SCM port, scm_print_state *state SCM_UNUSED)
82 {
83 scm_t_contregs *continuation = SCM_CONTREGS (obj);
84
85 scm_puts ("#<continuation ", port);
86 scm_intprint (continuation->num_stack_items, 10, port);
87 scm_puts (" @ ", port);
88 scm_uintprint (SCM_CELL_WORD_1 (obj), 16, port);
89 scm_putc ('>', port);
90 return 1;
91 }
92
93 #ifdef __ia64__
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(). */
97 struct rv
98 {
99 long retval;
100 long first_return;
101 };
102 extern struct rv ia64_getcontext (ucontext_t *) __asm__ ("getcontext");
103 #endif /* __ia64__ */
104
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"
109 SCM
110 scm_make_continuation (int *first)
111 {
112 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
113 SCM cont;
114 scm_t_contregs *continuation;
115 long stack_size;
116 SCM_STACKITEM * src;
117 #ifdef __ia64__
118 struct rv rv;
119 #endif /* __ia64__ */
120
121 SCM_CRITICAL_SECTION_START;
122 SCM_FLUSH_REGISTER_WINDOWS;
123 stack_size = scm_stack_size (thread->continuation_base);
124 continuation = scm_gc_malloc (sizeof (scm_t_contregs)
125 + (stack_size - 1) * sizeof (SCM_STACKITEM),
126 "continuation");
127 continuation->num_stack_items = stack_size;
128 continuation->dynenv = scm_i_dynwinds ();
129 continuation->throw_value = SCM_EOL;
130 continuation->root = thread->continuation_root;
131 continuation->dframe = scm_i_last_debug_frame ();
132 src = thread->continuation_base;
133 SCM_NEWSMOB (cont, scm_tc16_continuation, continuation);
134 SCM_CRITICAL_SECTION_END;
135
136 #if ! SCM_STACK_GROWS_UP
137 src -= stack_size;
138 #endif
139 continuation->offset = continuation->stack - src;
140 memcpy (continuation->stack, src, sizeof (SCM_STACKITEM) * stack_size);
141
142 #ifdef __ia64__
143 rv = ia64_getcontext (&continuation->ctx);
144 if (rv.first_return)
145 {
146 continuation->backing_store_size =
147 continuation->ctx.uc_mcontext.sc_ar_bsp -
148 (unsigned long) __libc_ia64_register_backing_store_base;
149 continuation->backing_store = NULL;
150 continuation->backing_store =
151 scm_gc_malloc (continuation->backing_store_size,
152 "continuation backing store");
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 {
161 SCM ret = continuation->throw_value;
162 *first = 0;
163 continuation->throw_value = SCM_BOOL_F;
164 return ret;
165 }
166 #else /* !__ia64__ */
167 if (setjmp (continuation->jmpbuf))
168 {
169 SCM ret = continuation->throw_value;
170 *first = 0;
171 continuation->throw_value = SCM_BOOL_F;
172 return ret;
173 }
174 else
175 {
176 *first = 1;
177 return cont;
178 }
179 #endif /* !__ia64__ */
180 }
181 #undef FUNC_NAME
182
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
196 static void scm_dynthrow (SCM, SCM);
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
205 scm_t_bits scm_i_dummy;
206
207 static void
208 grow_stack (SCM cont, SCM val)
209 {
210 scm_t_bits growth[100];
211
212 scm_i_dummy = (scm_t_bits) growth;
213 scm_dynthrow (cont, val);
214 }
215
216
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 */
221
222 typedef struct {
223 scm_t_contregs *continuation;
224 SCM_STACKITEM *dst;
225 } copy_stack_data;
226
227 static void
228 copy_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
235 static void
236 copy_stack_and_call (scm_t_contregs *continuation, SCM val,
237 SCM_STACKITEM * dst)
238 {
239 long delta;
240 copy_stack_data data;
241
242 delta = scm_ilength (scm_i_dynwinds ()) - scm_ilength (continuation->dynenv);
243 data.continuation = continuation;
244 data.dst = dst;
245 scm_i_dowinds (continuation->dynenv, delta, copy_stack, &data);
246
247 scm_i_set_last_debug_frame (continuation->dframe);
248
249 continuation->throw_value = val;
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
256 longjmp (continuation->jmpbuf, 1);
257 #endif
258 }
259
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 */
264 static void
265 scm_dynthrow (SCM cont, SCM val)
266 {
267 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
268 scm_t_contregs *continuation = SCM_CONTREGS (cont);
269 SCM_STACKITEM *dst = thread->continuation_base;
270 SCM_STACKITEM stack_top_element;
271
272 #if SCM_STACK_GROWS_UP
273 if (dst + continuation->num_stack_items >= &stack_top_element)
274 grow_stack (cont, val);
275 #else
276 dst -= continuation->num_stack_items;
277 if (dst <= &stack_top_element)
278 grow_stack (cont, val);
279 #endif /* def SCM_STACK_GROWS_UP */
280
281 SCM_FLUSH_REGISTER_WINDOWS;
282 copy_stack_and_call (continuation, val, dst);
283 }
284
285
286 static SCM
287 continuation_apply (SCM cont, SCM args)
288 #define FUNC_NAME "continuation_apply"
289 {
290 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
291 scm_t_contregs *continuation = SCM_CONTREGS (cont);
292
293 if (continuation->root != thread->continuation_root)
294 {
295 SCM_MISC_ERROR
296 ("invoking continuation would cross continuation barrier: ~A",
297 scm_list_1 (cont));
298 }
299
300 scm_dynthrow (cont, scm_values (args));
301 return SCM_UNSPECIFIED; /* not reached */
302 }
303 #undef FUNC_NAME
304
305 SCM
306 scm_i_with_continuation_barrier (scm_t_catch_body body,
307 void *body_data,
308 scm_t_catch_handler handler,
309 void *handler_data)
310 {
311 SCM_STACKITEM stack_item;
312 scm_i_thread *thread = SCM_I_CURRENT_THREAD;
313 SCM old_controot;
314 SCM_STACKITEM *old_contbase;
315 scm_t_debug_frame *old_lastframe;
316 SCM result;
317
318 /* Establish a fresh continuation root.
319 */
320 old_controot = thread->continuation_root;
321 old_contbase = thread->continuation_base;
322 old_lastframe = thread->last_debug_frame;
323 thread->continuation_root = scm_cons (thread->handle, old_controot);
324 thread->continuation_base = &stack_item;
325 thread->last_debug_frame = NULL;
326
327 /* Call FUNC inside a catch all. This is now guaranteed to return
328 directly and exactly once.
329 */
330 result = scm_internal_catch (SCM_BOOL_T,
331 body, body_data,
332 handler, handler_data);
333
334 /* Return to old continuation root.
335 */
336 thread->last_debug_frame = old_lastframe;
337 thread->continuation_base = old_contbase;
338 thread->continuation_root = old_controot;
339
340 return result;
341 }
342
343 struct c_data {
344 void *(*func) (void *);
345 void *data;
346 void *result;
347 };
348
349 static SCM
350 c_body (void *d)
351 {
352 struct c_data *data = (struct c_data *)d;
353 data->result = data->func (data->data);
354 return SCM_UNSPECIFIED;
355 }
356
357 static SCM
358 c_handler (void *d, SCM tag, SCM args)
359 {
360 struct c_data *data = (struct c_data *)d;
361 scm_handle_by_message_noexit (NULL, tag, args);
362 data->result = NULL;
363 return SCM_UNSPECIFIED;
364 }
365
366 void *
367 scm_c_with_continuation_barrier (void *(*func) (void *), void *data)
368 {
369 struct c_data c_data;
370 c_data.func = func;
371 c_data.data = data;
372 scm_i_with_continuation_barrier (c_body, &c_data,
373 c_handler, &c_data);
374 return c_data.result;
375 }
376
377 struct scm_data {
378 SCM proc;
379 };
380
381 static SCM
382 scm_body (void *d)
383 {
384 struct scm_data *data = (struct scm_data *)d;
385 return scm_call_0 (data->proc);
386 }
387
388 static SCM
389 scm_handler (void *d, SCM tag, SCM args)
390 {
391 scm_handle_by_message_noexit (NULL, tag, args);
392 return SCM_BOOL_F;
393 }
394
395 SCM_DEFINE (scm_with_continuation_barrier, "with-continuation-barrier", 1,0,0,
396 (SCM proc),
397 "Call @var{proc} and return the returned value but do not allow the invocation of continuations that would exit or reenter the dynamic extent of the call to @var{proc}. When a uncaught throw happens during the call to @var{proc}, a message is printed to the current error port and @code{#f} is returned.")
398 #define FUNC_NAME s_scm_with_continuation_barrier
399 {
400 struct scm_data scm_data;
401 scm_data.proc = proc;
402 return scm_i_with_continuation_barrier (scm_body, &scm_data,
403 scm_handler, &scm_data);
404 }
405 #undef FUNC_NAME
406
407 void
408 scm_init_continuations ()
409 {
410 scm_tc16_continuation = scm_make_smob_type ("continuation", 0);
411 scm_set_smob_mark (scm_tc16_continuation, continuation_mark);
412 scm_set_smob_free (scm_tc16_continuation, continuation_free);
413 scm_set_smob_print (scm_tc16_continuation, continuation_print);
414 scm_set_smob_apply (scm_tc16_continuation, continuation_apply, 0, 0, 1);
415 #include "libguile/continuations.x"
416 }
417
418 /*
419 Local Variables:
420 c-file-style: "gnu"
421 End:
422 */