* Protecting C code from continuations
@ 2004-07-04 14:06 Neil Jerram
2004-07-04 19:40 ` Steve Tell
0 siblings, 1 reply; 6+ messages in thread
From: Neil Jerram @ 2004-07-04 14:06 UTC (permalink / raw)
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?
Thanks,
Neil
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Protecting C code from continuations
2004-07-04 14:06 Protecting C code from continuations Neil Jerram
@ 2004-07-04 19:40 ` Steve Tell
2004-07-05 22:31 ` Neil Jerram
0 siblings, 1 reply; 6+ messages in thread
From: Steve Tell @ 2004-07-04 19:40 UTC (permalink / raw)
Cc: Guile Users
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
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Protecting C code from continuations
2004-07-04 19:40 ` Steve Tell
@ 2004-07-05 22:31 ` Neil Jerram
2004-07-06 19:50 ` Gary Houston
[not found] ` <200407061945.i66JjKT15824@s1.uklinux.net>
0 siblings, 2 replies; 6+ messages in thread
From: Neil Jerram @ 2004-07-05 22:31 UTC (permalink / raw)
Cc: Guile Users
Steve Tell wrote:
> On Sun, 4 Jul 2004, Neil Jerram wrote:
>
>
>> Hi all,
>>
>> I'm working with some C code that is not continuation-safe; [...]
>>
>> Any comments/ideas?
>
>
> Might (call-with-dynamic-root thunk) be what you want?
Thanks for reminding me - I had forgotten about call-with-dynamic-root
(cwdr). However, I'm not sure that it gives me what I need, because I
believe that a continuation captured within one cwdr cannot then be
invoked within a different cwdr.
To explain further: in my program, the main loop is in C; some events
from the main loop are handled purely within C code, while others are
dispatched into Scheme. The way I want to use continuations is such
that a Scheme coder can write
(begin
(do-thing-1)
(do-thing-2)
(do-thing-3))
without having to worry about the fact that (do-thing-2) will usually
have to escape and then let the C main loop run for a few iterations
until the information needed to complete (do-thing-2) comes back.
So, the continuation to "finish (do-thing-2) and then continue" needs to
be captured within one call from C into Scheme, but invoked within some
later call from C into Scheme (not necessarily the next such call).
Obviously (I think!) I can implement this using CPS
(continuation-passing style), and then define convenience macros to try
to hide the CPS, but it would be much nicer to use real continuations if
that is possible.
Regards,
Neil
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Protecting C code from continuations
2004-07-05 22:31 ` Neil Jerram
@ 2004-07-06 19:50 ` Gary Houston
[not found] ` <200407061945.i66JjKT15824@s1.uklinux.net>
1 sibling, 0 replies; 6+ messages in thread
From: Gary Houston @ 2004-07-06 19:50 UTC (permalink / raw)
Cc: guile-user
> Date: Mon, 05 Jul 2004 23:31:04 +0100
> From: Neil Jerram <neil@ossau.uklinux.net>
>
> Steve Tell wrote:
>
> > On Sun, 4 Jul 2004, Neil Jerram wrote:
> >
> >
> >> Hi all,
> >>
> >> I'm working with some C code that is not continuation-safe; [...]
> >>
> >> Any comments/ideas?
> >
> >
> > Might (call-with-dynamic-root thunk) be what you want?
>
> Thanks for reminding me - I had forgotten about call-with-dynamic-root
> (cwdr). However, I'm not sure that it gives me what I need, because I
> believe that a continuation captured within one cwdr cannot then be
> invoked within a different cwdr.
>
> To explain further: in my program, the main loop is in C; some events
> from the main loop are handled purely within C code, while others are
> dispatched into Scheme. The way I want to use continuations is such
> that a Scheme coder can write
>
> (begin
> (do-thing-1)
> (do-thing-2)
> (do-thing-3))
>
> without having to worry about the fact that (do-thing-2) will usually
> have to escape and then let the C main loop run for a few iterations
> until the information needed to complete (do-thing-2) comes back.
>
> So, the continuation to "finish (do-thing-2) and then continue" needs to
> be captured within one call from C into Scheme, but invoked within some
> later call from C into Scheme (not necessarily the next such call).
>
> Obviously (I think!) I can implement this using CPS
> (continuation-passing style), and then define convenience macros to try
> to hide the CPS, but it would be much nicer to use real continuations if
> that is possible.
I wrote something a while ago that I think is relevant to this. In CVS:
guile/workbook/extension/dynamic-root.text
Regards,
Gary
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Protecting C code from continuations
[not found] ` <200407061945.i66JjKT15824@s1.uklinux.net>
@ 2004-07-07 20:26 ` Neil Jerram
2004-07-08 23:14 ` Gary Houston
0 siblings, 1 reply; 6+ messages in thread
From: Neil Jerram @ 2004-07-07 20:26 UTC (permalink / raw)
Cc: guile-user
Gary Houston wrote:
> I wrote something a while ago that I think is relevant to this. In CVS:
>
> guile/workbook/extension/dynamic-root.text
Thanks Gary, that's interesting. However, it still doesn't do what I'm
looking for, I'm afraid, because I actually want to be able to invoke a
continuation within one C->Scheme entry point that was captured during a
previous C->Scheme entry point.
I now think that I can achieve what I want by making all the C->Scheme
entry points look like this:
(define last-c-continuation #f)
(define (entry-point ...)
(call/cc (lambda (cont)
(set! last-c-continuation cont)
(catch #t
interesting-stuff
exception-handler)
(last-c-continuation #f))))
Then, even if `interesting-stuff' invokes a continuation, it will jump
into a call stack that is identical as far as the high level frames
shown here are concerned; and the new call stack will still invoke
last-c-continuation, thus restoring the stack for the containing C code.
Any comments on this, including whether it will or won't work?
Thanks,
Neil
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Protecting C code from continuations
2004-07-07 20:26 ` Neil Jerram
@ 2004-07-08 23:14 ` Gary Houston
0 siblings, 0 replies; 6+ messages in thread
From: Gary Houston @ 2004-07-08 23:14 UTC (permalink / raw)
Cc: guile-user
> Date: Wed, 07 Jul 2004 21:26:21 +0100
> From: Neil Jerram <neil@ossau.uklinux.net>
> Cc: guile-user@gnu.org
>
> Gary Houston wrote:
>
> > I wrote something a while ago that I think is relevant to this. In CVS:
> >
> > guile/workbook/extension/dynamic-root.text
>
> Thanks Gary, that's interesting. However, it still doesn't do what I'm
> looking for, I'm afraid, because I actually want to be able to invoke a
> continuation within one C->Scheme entry point that was captured during a
> previous C->Scheme entry point.
>
> I now think that I can achieve what I want by making all the C->Scheme
> entry points look like this:
>
> (define last-c-continuation #f)
>
> (define (entry-point ...)
> (call/cc (lambda (cont)
> (set! last-c-continuation cont)
> (catch #t
> interesting-stuff
> exception-handler)
> (last-c-continuation #f))))
>
> Then, even if `interesting-stuff' invokes a continuation, it will jump
> into a call stack that is identical as far as the high level frames
> shown here are concerned; and the new call stack will still invoke
> last-c-continuation, thus restoring the stack for the containing C code.
>
> Any comments on this, including whether it will or won't work?
It's devious, but I can't think of any reason why it wouldn't work.
Gary
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2004-07-08 23:14 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-07-04 14:06 Protecting C code from continuations Neil Jerram
2004-07-04 19:40 ` Steve Tell
2004-07-05 22:31 ` Neil Jerram
2004-07-06 19:50 ` Gary Houston
[not found] ` <200407061945.i66JjKT15824@s1.uklinux.net>
2004-07-07 20:26 ` Neil Jerram
2004-07-08 23:14 ` Gary Houston
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).