unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Mikael Djurfeldt <mikael@djurfeldt.com>
To: guile-devel <guile-devel@gnu.org>
Cc: Mikael Djurfeldt <mikael@djurfeldt.com>
Subject: [PATCH] At-exit hook
Date: Wed, 6 Nov 2024 20:52:24 +0100	[thread overview]
Message-ID: <CAA2Xvw+a=GzcyR88MVDaZRHpnsivZ-TSovUQiORvYTch5XsUBw@mail.gmail.com> (raw)

[-- 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 --]

             reply	other threads:[~2024-11-06 19:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-06 19:52 Mikael Djurfeldt [this message]
2024-11-07 11:23 ` [PATCH] At-exit hook 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAA2Xvw+a=GzcyR88MVDaZRHpnsivZ-TSovUQiORvYTch5XsUBw@mail.gmail.com' \
    --to=mikael@djurfeldt.com \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).