wingo pushed a commit to branch wip-whippet
in repository guile.

commit 8b12d6f26c468768a4cdd9c1e1de7abb8087326f
Author: Andy Wingo <wi...@pobox.com>
AuthorDate: Thu May 15 11:50:47 2025 +0200

    Excise BDW API use from threads.c
    
    * libguile/threads.c (with_guile, scm_without_guile): Use gc_activate /
    gc_deactivate API.  It's a noop on BDW, but all we lose is a bit of
    precision.
    (%call-with-new-thread): Remove GC_collect_a_little call, it was
    useless.
---
 libguile/threads.c | 61 ++++++++++++++++++++++--------------------------------
 1 file changed, 25 insertions(+), 36 deletions(-)

diff --git a/libguile/threads.c b/libguile/threads.c
index 380864f6f..8dbd75982 100644
--- a/libguile/threads.c
+++ b/libguile/threads.c
@@ -40,7 +40,6 @@
 #endif
 
 #include "async.h"
-#include "bdw-gc.h"
 #include "boolean.h"
 #include "continuations.h"
 #include "deprecation.h"
@@ -72,8 +71,6 @@
 
 #include "threads.h"
 
-#include <gc/gc_mark.h>
-
 
 
 
@@ -612,19 +609,11 @@ scm_init_guile (void)
 
 struct with_guile_args
 {
-  GC_fn_type func;
+  void* (*func) (void*);
   void *data;
   SCM dynamic_state;
 };
 
-static void *
-with_guile_trampoline (void *data)
-{
-  struct with_guile_args *args = data;
-
-  return scm_c_with_continuation_barrier (args->func, args->data);
-}
-  
 static void *
 with_guile (struct gc_stack_addr base, void *data)
 {
@@ -635,22 +624,10 @@ with_guile (struct gc_stack_addr base, void *data)
 
   new_thread = scm_i_init_thread_for_guile (base, args->dynamic_state);
   t = SCM_I_CURRENT_THREAD;
-  if (new_thread)
-    {
-      /* We are in Guile mode.  */
-      assert (t->guile_mode);
-
-      res = scm_c_with_continuation_barrier (args->func, args->data);
+  int reactivate = !t->guile_mode;
+  int deactivate = reactivate || new_thread;
 
-      /* Leave Guile mode.  */
-      t->guile_mode = 0;
-    }
-  else if (t->guile_mode)
-    {
-      /* Already in Guile mode.  */
-      res = scm_c_with_continuation_barrier (args->func, args->data);
-    }
-  else
+  if (reactivate)
     {
       /* We are not in Guile mode, either because we are not within a
          scm_with_guile, or because we are within a scm_without_guile.
@@ -668,9 +645,17 @@ with_guile (struct gc_stack_addr base, void *data)
 #endif
 
       t->guile_mode = 1;
-      res = GC_call_with_gc_active (with_guile_trampoline, args);
+      gc_reactivate (t->mutator);
+    }
+
+  res = scm_c_with_continuation_barrier (args->func, args->data);
+
+  if (deactivate)
+    {
+      gc_deactivate (t->mutator);
       t->guile_mode = 0;
     }
+
   return res;
 }
 
@@ -697,16 +682,21 @@ scm_without_guile (void *(*func)(void *), void *data)
 {
   void *result;
   scm_thread *t = SCM_I_CURRENT_THREAD;
+  int was_active = t->guile_mode;
 
-  if (t->guile_mode)
+  if (was_active)
     {
-      SCM_I_CURRENT_THREAD->guile_mode = 0;
-      result = GC_do_blocking (func, data);
-      SCM_I_CURRENT_THREAD->guile_mode = 1;
+      t->guile_mode = 0;
+      gc_deactivate (t->mutator);
+    }
+
+  result = func (data);
+
+  if (was_active)
+    {
+      gc_reactivate (t->mutator);
+      t->guile_mode = 1;
     }
-  else
-    /* Otherwise we're not in guile mode, so nothing to do.  */
-    result = func (data);
 
   return result;
 }
@@ -797,7 +787,6 @@ SCM_DEFINE (scm_sys_call_with_new_thread, 
"%call-with-new-thread", 1, 0, 0,
 
   SCM_ASSERT (scm_is_true (scm_thunk_p (thunk)), thunk, SCM_ARG1, FUNC_NAME);
 
-  GC_collect_a_little ();
   data = scm_gc_typed_calloc (launch_data);
   data->dynamic_state = scm_current_dynamic_state ();
   data->thunk = thunk;

Reply via email to