From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=3.0 tests=AWL,BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 9F04B1F5AE for ; Sat, 27 Jun 2020 21:57:59 +0000 (UTC) Received: from localhost ([::1]:42836 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jpIpi-0007y2-7J for normalperson@yhbt.net; Sat, 27 Jun 2020 17:57:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:56086) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jpIpe-0007wQ-K5 for bug-gnulib@gnu.org; Sat, 27 Jun 2020 17:57:55 -0400 Received: from mo4-p00-ob.smtp.rzone.de ([85.215.255.25]:12500) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jpIpc-00057B-1H for bug-gnulib@gnu.org; Sat, 27 Jun 2020 17:57:54 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1593295069; s=strato-dkim-0002; d=clisp.org; h=Message-ID:Date:Subject:To:From:X-RZG-CLASS-ID:X-RZG-AUTH:From: Subject:Sender; bh=y947NZ4MmGpgms0F9lXoXIKnZLugSjKkCJmr4Ncz8VQ=; b=BB26ieuHgZuXoqbTrTfuNEQjg5LwbDh4tI6hGPkY4yT4Mb+P3+nun0AcUV/hlfKO+h k+Hgp9IdMyVjsRsWnKhLsG2IxxXvMZq3M70E3qdsyty46MkPRA9IqyA8e4hyPjEplKiP FyvLCmf5GoBdJWibHYZIYRlCExqBUw2tOoqc1YxKORKe64PhUSov4IKiIjgp6lHxUFNX 3zj2hKlliBvIcZfx6nLTPOPhwIpOVZIzNLDFn9idhGxQoEOgoIFgfI/Qy46CwLJ3Ly9E 9YoHnHp9vbZbccmNQuwI5P5NwSrzjIaJbinaC5KvSLnR07DgRBrvQICiHYLb4K7Zjc87 TSLA== X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH+AHjwLuWOH6fzxfs=" X-RZG-CLASS-ID: mo00 Received: from bruno.haible.de by smtp.strato.de (RZmta 46.10.5 DYNA|AUTH) with ESMTPSA id R03d1aw5RLvmMrj (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (curve X9_62_prime256v1 with 256 ECDH bits, eq. 3072 bits RSA)) (Client did not present a certificate); Sat, 27 Jun 2020 23:57:48 +0200 (CEST) From: Bruno Haible To: bug-gnulib@gnu.org Subject: fatal-signal: make multithread-safe Date: Sat, 27 Jun 2020 23:57:48 +0200 Message-ID: <2350079.bc2zunp2nT@omega> User-Agent: KMail/5.1.3 (Linux/4.4.0-179-generic; KDE/5.18.0; x86_64; ; ) MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Received-SPF: none client-ip=85.215.255.25; envelope-from=bruno@clisp.org; helo=mo4-p00-ob.smtp.rzone.de X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/27 17:57:49 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_PASS=-0.001, SPF_NONE=0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" 2020-06-27 Bruno Haible fatal-signal: Make multithread-safe. * lib/fatal-signal.c: Include glthread/lock.h. (at_fatal_signal_lock): New variable. (at_fatal_signal): Use it. (fatal_signals_block_lock, fatal_signals_block_counter): New variables. (block_fatal_signals, unblock_fatal_signals): Use them. * modules/fatal-signal (Depends-on): Add lock. diff --git a/lib/fatal-signal.c b/lib/fatal-signal.c index c8ff338..975393b 100644 --- a/lib/fatal-signal.c +++ b/lib/fatal-signal.c @@ -26,6 +26,7 @@ #include #include +#include "glthread/lock.h" #include "sig-handler.h" #include "xalloc.h" @@ -200,11 +201,16 @@ install_handlers (void) } +/* Lock that makes at_fatal_signal multi-thread safe. */ +gl_lock_define_initialized (static, at_fatal_signal_lock) + /* Register a cleanup function to be executed when a catchable fatal signal occurs. */ void at_fatal_signal (action_t action) { + gl_lock_lock (at_fatal_signal_lock); + static bool cleanup_initialized = false; if (!cleanup_initialized) { @@ -242,6 +248,8 @@ at_fatal_signal (action_t action) actions[actions_count]. */ actions[actions_count].action = action; actions_count++; + + gl_lock_unlock (at_fatal_signal_lock); } @@ -269,20 +277,43 @@ init_fatal_signal_set (void) } } +/* Lock and counter that allow block_fatal_signals/unblock_fatal_signals pairs + to occur in different threads and even overlap in time. */ +gl_lock_define_initialized (static, fatal_signals_block_lock) +static unsigned int fatal_signals_block_counter = 0; + /* Temporarily delay the catchable fatal signals. */ void block_fatal_signals (void) { - init_fatal_signal_set (); - sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL); + gl_lock_lock (fatal_signals_block_lock); + + if (fatal_signals_block_counter++ == 0) + { + init_fatal_signal_set (); + sigprocmask (SIG_BLOCK, &fatal_signal_set, NULL); + } + + gl_lock_unlock (fatal_signals_block_lock); } /* Stop delaying the catchable fatal signals. */ void unblock_fatal_signals (void) { - init_fatal_signal_set (); - sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL); + gl_lock_lock (fatal_signals_block_lock); + + if (fatal_signals_block_counter == 0) + /* There are more calls to unblock_fatal_signals() than to + block_fatal_signals(). */ + abort (); + if (--fatal_signals_block_counter == 0) + { + init_fatal_signal_set (); + sigprocmask (SIG_UNBLOCK, &fatal_signal_set, NULL); + } + + gl_lock_unlock (fatal_signals_block_lock); } diff --git a/modules/fatal-signal b/modules/fatal-signal index 6683c79..de53de7 100644 --- a/modules/fatal-signal +++ b/modules/fatal-signal @@ -12,6 +12,7 @@ xalloc stdbool unistd sigaction +lock sigprocmask raise