bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org
Subject: Re: fatal-signal: make multithread-safe
Date: Sat, 04 Jul 2020 12:45:53 +0200	[thread overview]
Message-ID: <3197655.nlokK2ypgg@omega> (raw)
In-Reply-To: <2116674.OGhGKdnA1K@omega>

Another multithread-safety issue was lurking in module 'fatal-signal'.
This patch fixes it.


2020-07-04  Bruno Haible  <bruno@clisp.org>

	fatal-signal: Make multithread-safe.
	* lib/fatal-signal.c (init_fatal_signals): Add comment.
	(do_init_fatal_signal_set): New function, extracted from
	init_fatal_signal_set.
	(fatal_signal_set_once): New variable.
	(init_fatal_signal_set): Use gl_once.

diff --git a/lib/fatal-signal.c b/lib/fatal-signal.c
index c6f8dac..8b27cc6 100644
--- a/lib/fatal-signal.c
+++ b/lib/fatal-signal.c
@@ -86,6 +86,10 @@ static int fatal_signals[] =
 static void
 init_fatal_signals (void)
 {
+  /* This function is multithread-safe even without synchronization, because
+     if two threads execute it simultaneously, the fatal_signals[] array will
+     not change any more after the first of the threads has completed this
+     function.  */
   static bool fatal_signals_initialized = false;
   if (!fatal_signals_initialized)
     {
@@ -266,22 +270,25 @@ at_fatal_signal (action_t action)
 static sigset_t fatal_signal_set;
 
 static void
-init_fatal_signal_set (void)
+do_init_fatal_signal_set (void)
 {
-  static bool fatal_signal_set_initialized = false;
-  if (!fatal_signal_set_initialized)
-    {
-      size_t i;
+  size_t i;
 
-      init_fatal_signals ();
+  init_fatal_signals ();
 
-      sigemptyset (&fatal_signal_set);
-      for (i = 0; i < num_fatal_signals; i++)
-        if (fatal_signals[i] >= 0)
-          sigaddset (&fatal_signal_set, fatal_signals[i]);
+  sigemptyset (&fatal_signal_set);
+  for (i = 0; i < num_fatal_signals; i++)
+    if (fatal_signals[i] >= 0)
+      sigaddset (&fatal_signal_set, fatal_signals[i]);
+}
 
-      fatal_signal_set_initialized = true;
-    }
+/* Ensure that do_init_fatal_signal_set is called once only.  */
+gl_once_define(static, fatal_signal_set_once)
+
+static void
+init_fatal_signal_set (void)
+{
+  gl_once (fatal_signal_set_once, do_init_fatal_signal_set);
 }
 
 /* Lock and counter that allow block_fatal_signals/unblock_fatal_signals pairs



      reply	other threads:[~2020-07-04 10:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-27 21:57 fatal-signal: make multithread-safe Bruno Haible
2020-06-27 22:42 ` Bruno Haible
2020-07-04 10:45   ` Bruno Haible [this message]

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://lists.gnu.org/mailman/listinfo/bug-gnulib

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

  git send-email \
    --in-reply-to=3197655.nlokK2ypgg@omega \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@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).