unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH] At-exit hook
@ 2024-11-06 19:52 Mikael Djurfeldt
  2024-11-07 11:23 ` Maxime Devos
  0 siblings, 1 reply; 10+ messages in thread
From: Mikael Djurfeldt @ 2024-11-06 19:52 UTC (permalink / raw)
  To: guile-devel; +Cc: Mikael Djurfeldt

[-- Attachment #1: Type: text/plain, Size: 1922 bytes --]

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 ();

[-- Attachment #2: Type: text/html, Size: 2284 bytes --]

^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-11-08  9:09 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-06 19:52 [PATCH] At-exit hook Mikael Djurfeldt
2024-11-07 11:23 ` Maxime Devos
2024-11-07 12:08   ` Nala Ginrut
2024-11-07 12:09   ` Mailer
2024-11-07 12:27     ` Mailer
2024-11-07 13:28       ` Mikael Djurfeldt
2024-11-07 16:10     ` Maxime Devos
2024-11-07 19:51       ` Mailer
2024-11-07 22:18         ` Maxime Devos
2024-11-08  9:09           ` Mikael Djurfeldt

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).