[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Protecting C code from continuations
From: |
Steve Tell |
Subject: |
Re: Protecting C code from continuations |
Date: |
Sun, 4 Jul 2004 15:40:31 -0400 (EDT) |
On Sun, 4 Jul 2004, Neil Jerram wrote:
> Hi all,
>
> I'm working with some C code that is not continuation-safe;
> specifically, because it will fail in some way if the points of entry
> from C into Scheme code appear to return more than once. However, it
> would be useful if I could use continuations to save and restore context
> as far as the Scheme stack is concerned.
>
> Given this, is there any way of being able to use continuations within
> Scheme but protecting the C code from them?
>
> I was wondering about using something like this at all the Scheme-from-C
> entry points ...........
>
> (define-macro (with-continuation-barrier . body)
> `(call/cc
> (lambda (cont)
> (dynamic-wind
> noop
> (lambda () ,@body)
> (lambda () (cont #f)))))
>
> ..... but I suspect this doesn't work, because when I call a
> continuation deep within the Scheme code (i.e. the continuation that I
> really want to use, not the one used as part of the barrier here), that
> continuation would replace the current dynamic-wind context with the one
> that was in effect when the continuation was created -- which is exactly
> what I don't want.
>
> Any comments/ideas?
Might (call-with-dynamic-root thunk) be what you want?
"If thunk captures a continuation, the continuation is rooted at the call
to thunk. In particular, the call to call-with-dynamic-root is not
captured. Therefore, call-with-dynamic-root always returns at most one
time."
I've been using some C code I borrowed long ago from scwm (scheme
configurable window manager, by Maciej Stachowiak and Greg J. Badros) for
such callbacks from C into scheme. It sets up the dynamic root, and also
sets up addtional handlers to save and print a stack traceback from any
errors inside such callbacks.
It uses a lot of deprecated features, so at some point I need to rewrite
it with reference to the current guile manual - and write a tutorial or
documentation addition while doing so.
Steve