From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mikael Djurfeldt Newsgroups: gmane.lisp.guile.devel Subject: [PATCH] At-exit hook Date: Wed, 6 Nov 2024 20:52:24 +0100 Message-ID: Reply-To: mikael@djurfeldt.com Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000f66891062643db07" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="35403"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Mikael Djurfeldt To: guile-devel Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Wed Nov 06 20:53:12 2024 Return-path: Envelope-to: guile-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1t8m5Y-00095s-Jq for guile-devel@m.gmane-mx.org; Wed, 06 Nov 2024 20:53:12 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1t8m56-0007tE-SR; Wed, 06 Nov 2024 14:52:45 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1t8m53-0007sw-At for guile-devel@gnu.org; Wed, 06 Nov 2024 14:52:41 -0500 Original-Received: from mail-vk1-f171.google.com ([209.85.221.171]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1t8m51-0003Rv-N7 for guile-devel@gnu.org; Wed, 06 Nov 2024 14:52:41 -0500 Original-Received: by mail-vk1-f171.google.com with SMTP id 71dfb90a1353d-50d431b0ae2so992868e0c.1 for ; Wed, 06 Nov 2024 11:52:36 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730922755; x=1731527555; h=cc:to:subject:message-id:date:from:reply-to:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZtRDCjwv3N7uhtm4I7rf7+GAu7qGQQ+IL8moK6zi778=; b=WcI2B1ijhU0O2N0LlCfb6hhc5yWp/nwzeSccAMMnDk1bphHk3DNH9FwtOKY70RcI2G eHM/qI8n6dYbGdukMaYID0Iju5rFh++m83LJO+2lYEywOOVapZEiBiPZosMk/TyoK/g/ PvKB5SW7UA7anr/YM6FtUIgTzo/w6BelJ9jLWAyv4qi1hJDi6r4M2D8N5SLZykxbuwYb 8tfZX+zVcGhgFOIydCr9wCRsws0hnHr8g9A2+vd2RYOEX/PrTWMWUbmiacme08VULeXW kbBXEOaWlMHZjTA2oi7ArHTMsBCAhFUNMi5Kioo0vYBak1HrATA16n6P7qg8iS/F9cBt Vdxg== X-Gm-Message-State: AOJu0Yxd4SKM4K24Gupo0dJ6eD6uK5Hmg60n3veFm6jdLd1iZwEyUYO4 wdddV+w/VS9BTAR7qsN2h89aI5wnBOn6O/G6qjwOFWjQE2tlPuv5IfeYHOgh1/Xf5xniozUiuz4 A8x0/1c1p4iEXVirLOJM55a3qBJWdQhbY X-Google-Smtp-Source: AGHT+IHuXl7FF8Fd0vqHZ/IxFz3TuAa+EeyHON4CUz/Xn1WX7sTrGUxV0E3YixBL+F5AE/7tdG8rQU8z6e0P+5xLYZs= X-Received: by 2002:a05:6122:2509:b0:50d:4f96:507 with SMTP id 71dfb90a1353d-513f0d12d0dmr582495e0c.6.1730922755283; Wed, 06 Nov 2024 11:52:35 -0800 (PST) Received-SPF: pass client-ip=209.85.221.171; envelope-from=mdjurfeldt@gmail.com; helo=mail-vk1-f171.google.com X-Spam_score_int: -15 X-Spam_score: -1.6 X-Spam_bar: - X-Spam_report: (-1.6 / 5.0 requ) BAYES_00=-1.9, FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.devel:22759 Archived-At: --000000000000f66891062643db07 Content-Type: text/plain; charset="UTF-8" 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 (); --000000000000f66891062643db07 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,

I think it would be good= to have an at-exit-hook which is run atexit(). The motivation is that Guil= e can provide bindings for libraries which may want to clean up resources a= t 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 n= ot needed since the bindings themselves could call atexit(), but this presu= mes 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, providin= g this functionality as a hook gives us better control with regards to runn= ing cleanup code in the proper context.

I can appl= y this patch with proper additions to NEWS etc myself, but I wanted to firs= t give you a chance to protest.

So, what do you sa= y?

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)
=C2=A0scm_i_pthread_= mutex_t scm_i_init_mutex =3D SCM_I_PTHREAD_MUTEX_INITIALIZER;
=C2=A0int = scm_initialized_p =3D 0;
=C2=A0
+SCM scm_at_exit_hook;
+
=C2=A0= static void *
=C2=A0really_cleanup_for_exit (void *unused)
=C2=A0{+ =C2=A0scm_c_run_hook (scm_at_exit_hook, SCM_EOL);
=C2=A0 =C2=A0scm_fl= ush_all_ports ();
=C2=A0 =C2=A0return NULL;
=C2=A0}
@@ -351,6 +354= ,13 @@ cleanup_for_exit ()
=C2=A0 =C2=A0scm_with_guile (really_cleanup_f= or_exit, NULL);
=C2=A0}
=C2=A0
+static void
+init_at_exit_hook = ()
+{
+ =C2=A0scm_at_exit_hook =3D scm_make_hook (SCM_INUM0);
+ = =C2=A0scm_c_define ("at-exit-hook", scm_at_exit_hook);
+}
+=
=C2=A0void
=C2=A0scm_i_init_guile (void *base)
=C2=A0{
@@ -504= ,6 +514,7 @@ scm_i_init_guile (void *base)
=C2=A0 =C2=A0scm_init_rw ();<= br>=C2=A0 =C2=A0scm_init_extensions ();
=C2=A0
+ =C2=A0init_at_exit_h= ook ();
=C2=A0 =C2=A0atexit (cleanup_for_exit);
=C2=A0 =C2=A0scm_load= _startup_files ();
=C2=A0 =C2=A0scm_init_load_should_auto_compile ();

--000000000000f66891062643db07--