Hi,

I think it would be good to have an at-exit-hook which is run atexit(). The
motivation is that Guile can provide bindings for libraries which may want
to clean up resources at exit. If the at-exit-hook exists, this would then
be one way to make sure that the linked in library can do this at exit. You
could say that it is not needed since the bindings themselves could call
atexit(), but this presumes that the bindings are written in C. Nowadays
with (system foreign) and Nyacc, the bindings could very well be written
entirely in Scheme.

An alternative would then, of course, be to create a Guile binding for
atexit(), but as you can see from the included patch, providing this
functionality as a hook gives us better control with regards to running
cleanup code in the proper context.

I can apply this patch with proper additions to NEWS etc myself, but I
wanted to first give you a chance to protest.

So, what do you say?

Best regards,
Mikael

diff --git a/libguile/init.c b/libguile/init.c
index 3df8c5ae5..2bf69c9f9 100644
--- a/libguile/init.c
+++ b/libguile/init.c
@@ -327,9 +327,12 @@ invoke_main_func (void *body_data)
 scm_i_pthread_mutex_t scm_i_init_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
 int scm_initialized_p = 0;

+SCM scm_at_exit_hook;
+
 static void *
 really_cleanup_for_exit (void *unused)
 {
+  scm_c_run_hook (scm_at_exit_hook, SCM_EOL);
   scm_flush_all_ports ();
   return NULL;
 }
@@ -351,6 +354,13 @@ cleanup_for_exit ()
   scm_with_guile (really_cleanup_for_exit, NULL);
 }

+static void
+init_at_exit_hook ()
+{
+  scm_at_exit_hook = scm_make_hook (SCM_INUM0);
+  scm_c_define ("at-exit-hook", scm_at_exit_hook);
+}
+
 void
 scm_i_init_guile (void *base)
 {
@@ -504,6 +514,7 @@ scm_i_init_guile (void *base)
   scm_init_rw ();
   scm_init_extensions ();

+  init_at_exit_hook ();
   atexit (cleanup_for_exit);
   scm_load_startup_files ();
   scm_init_load_should_auto_compile ();

Reply via email to