sexp_context_interruptp(scheme_ctx) = 1;

7 views
Skip to first unread message

*...@speechcode.com

unread,
Sep 19, 2025, 1:33:54 AMSep 19
to chibi-scheme
Is this the correct way to interrupt a running Chibi Scheme evaluation in another thread?  I'm implementing a remote REPL, and using that statement to try to stop a loop like (let loop () (loop)).  Unless I'm mistaken, it's failing to stop the evaluation.  Have I misunderstood the API?

Thanks.

Alex Shinn

unread,
Sep 19, 2025, 1:46:33 AMSep 19
to chibi-...@googlegroups.com
That should work - it's basically what thread-interrupt! does, which is how (chibi repl) interrupts on C-c.
Not sure what might be going wrong, although tight loops in C won't get interrupted.

Note you may be able to use (chibi repl) directly as each of Read, Eval, and Print are customizable.

-- 
Alex

On Fri, Sep 19, 2025 at 2:33 PM *...@speechcode.com <*@speechcode.com> wrote:
Is this the correct way to interrupt a running Chibi Scheme evaluation in another thread?  I'm implementing a remote REPL, and using that statement to try to stop a loop like (let loop () (loop)).  Unless I'm mistaken, it's failing to stop the evaluation.  Have I misunderstood the API?

Thanks.

--
You received this message because you are subscribed to the Google Groups "chibi-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chibi-scheme...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/chibi-scheme/e8c83ef7-ee73-4bed-a710-39f2a68db25an%40googlegroups.com.

*...@speechcode.com

unread,
Sep 19, 2025, 3:27:50 PMSep 19
to chibi-scheme
Thanks.  It is ignored unless SEXP_USE_GREEN_THREADS=1, so I've set that when compiling Chibi Scheme.  Somehow, however, the context that is being checked in vm.c doesn't have the interruptp field set despite the fact that I've set it in the context that I used when starting the evaluation.  There seem to be many places where the green threads system can dynamically create and use new contexts.  I don't know how to get hold of the one that's in use at the time the interrupt check is done.  I'm probably missing something obvious.

Alex Shinn

unread,
Sep 19, 2025, 6:05:43 PMSep 19
to chibi-...@googlegroups.com
New, non-thread contexts are created only when compiling.
So an app may be running under a child context of the one you initially created in C, but it won't change unless your app uses eval.
From within Scheme you can just call (current-thread) and that won't change, but again I don't think threads play well with eval.

-- 
Alex

On Sat, Sep 20, 2025 at 4:27 AM *...@speechcode.com <*@speechcode.com> wrote:
Thanks.  It is ignored unless SEXP_USE_GREEN_THREADS=1, so I've set that when compiling Chibi Scheme.  Somehow, however, the context that is being checked in vm.c doesn't have the interruptp field set despite the fact that I've set it in the context that I used when starting the evaluation.  There seem to be many places where the green threads system can dynamically create and use new contexts.  I don't know how to get hold of the one that's in use at the time the interrupt check is done.  I'm probably missing something obvious.

--
You received this message because you are subscribed to the Google Groups "chibi-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chibi-scheme...@googlegroups.com.

Arthur A. Gleckler

unread,
Sep 19, 2025, 6:49:59 PMSep 19
to chibi-...@googlegroups.com
I'm not using Scheme threads at all.  I'm running sexp_eval in a separate Android native OS thread so that I can continue to receive expressions and commands over the network while Scheme evaluation is under way.  If the user hits C-c on the desktop client, I'm trying to interrupt the running sexp_eval by using sexp_context_interruptp.  But when I call that on the context I used to start the evaluation, it doesn't stop the evaluation.  It appears that the context that is being checked isn't the same one that I started with.

Alex Shinn

unread,
Sep 19, 2025, 8:01:35 PMSep 19
to chibi-...@googlegroups.com
Yes, specifically, assuming your code looks like:

  sexp ctx = sexp_make_eval_context(NULL, NULL, NULL, 0, 0);
  sexp_load_standard_env(ctx, NULL, SEXP_SEVEN);
  sexp_eval_string(ctx, "(run)", NULL);

then the eval code will run under a child context of ctx.
You should be able to access this as sexp_context_child(ctx)
and interrupt that, or more generally store the running ctx
somewhere global for C to access.

Alternatively, you should be able to force it to run directly
under ctx by changing the above to:

  sexp run = sexp_eval_string(ctx, "(lambda () (run))", NULL);
  sexp_apply(ctx, run, SEXP_NULL);

Running the compilation in the same context would be
a major refactoring effort, but it may be reasonable to
do something like this automatically for sexp_eval*.

-- 
Alex

On Sat, Sep 20, 2025, 7:50 AM Arthur A. Gleckler <a...@speechcode.com> wrote:
I'm not using Scheme threads at all.  I'm running sexp_eval in a separate Android native OS thread so that I can continue to receive expressions and commands over the network while Scheme evaluation is under way.  If the user hits C-c on the desktop client, I'm trying to interrupt the running sexp_eval by using sexp_context_interruptp.  But when I call that on the context I used to start the evaluation, it doesn't stop the evaluation.  It appears that the context that is being checked isn't the same one that I started with.
--
You received this message because you are subscribed to the Google Groups "chibi-scheme" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chibi-scheme...@googlegroups.com.

Alex Shinn

unread,
Sep 19, 2025, 11:37:46 PMSep 19
to chibi-...@googlegroups.com
Finally, without using threads at all you could restructure your loop as: 

(define running? #t)
(let loop () (when running? ... (loop)))

then set running? to #f either from C or via a signal handler set in Scheme.

-- 
Alex

Arthur A. Gleckler

unread,
Sep 19, 2025, 11:49:01 PMSep 19
to chibi-...@googlegroups.com
On Fri, Sep 19, 2025 at 8:37 PM Alex Shinn <alex...@gmail.com> wrote:
Finally, without using threads at all you could restructure your loop as: 

Thanks.  I'm building a remote REPL where I/O happens on a desktop client, and Chibi Scheme in an Android app evaluates the expressions the user types.  Communication is via Bluetooth.  The idea is to create an interactive environment for writing apps in Scheme.  I need to handle arbitrary Scheme code.

Now that I'm calling sexp_context_child, sexp_eval in the evaluation thread is returning, as desired.

Now I'm debugging thread synchronization problems on the client side, but it's going well.

Thanks so much for your help with this — and for Chibi Scheme, of course.
Reply all
Reply to author
Forward
0 new messages