]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
asyncsafe-spin: Prefer pthread_sigmask over sigprocmask.
authorBruno Haible <bruno@clisp.org>
Sat, 4 Apr 2026 20:44:10 +0000 (22:44 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 4 Apr 2026 20:44:10 +0000 (22:44 +0200)
Suggested by Paul Eggert.

* lib/asyncsafe-spin.c (asyncsafe_spin_lock, asyncsafe_spin_unlock): Use
pthread_sigmask instead of sigprocmask.
* modules/asyncsafe-spin (Depends-on): Add pthread_sigmask. Remove
sigprocmask.
(Link): New section.
* modules/asyncsafe-spin-tests (Makefile.am): Link test-asyncsafe-spin1
with $(PTHREAD_SIGMASK_LIB).
* modules/jit/cache-tests (Makefile.am): Link test-cache with
$(PTHREAD_SIGMASK_LIB).

ChangeLog
lib/asyncsafe-spin.c
modules/asyncsafe-spin
modules/asyncsafe-spin-tests
modules/jit/cache-tests

index 168151c70c04f7b8ef269fa9d658df92d0a0d945..8db20e508d2f6f879f256b0d382776a586d64d01 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2026-04-04  Bruno Haible  <bruno@clisp.org>
+
+       asyncsafe-spin: Prefer pthread_sigmask over sigprocmask.
+       Suggested by Paul Eggert.
+       * lib/asyncsafe-spin.c (asyncsafe_spin_lock, asyncsafe_spin_unlock): Use
+       pthread_sigmask instead of sigprocmask.
+       * modules/asyncsafe-spin (Depends-on): Add pthread_sigmask. Remove
+       sigprocmask.
+       (Link): New section.
+       * modules/asyncsafe-spin-tests (Makefile.am): Link test-asyncsafe-spin1
+       with $(PTHREAD_SIGMASK_LIB).
+       * modules/jit/cache-tests (Makefile.am): Link test-cache with
+       $(PTHREAD_SIGMASK_LIB).
+
 2026-04-04  Bruno Haible  <bruno@clisp.org>
 
        spawn-pipe: Prefer pthread_sigmask over sigprocmask.
index 8400ad87a3762e550d00d46997284a7072ebf6dd..1c2c5950a7b7b2c23a2a0d139b35f8056721342c 100644 (file)
@@ -42,22 +42,19 @@ asyncsafe_spin_lock (asyncsafe_spinlock_t *lock,
      On platforms other than native Windows, it is useful to do the same
      thing also within a signal handler, since signals may remain enabled
      while a signal handler runs.  It is possible to do this because
-     sigprocmask() is safe to call from within a signal handler, see
+     pthread_sigmask() is safe to call from within a signal handler, see
      POSIX section "Signal Actions"
      <https://pubs.opengroup.org/onlinepubs/9799919799/functions/V2_chap02.html#tag_16_04_03>.
-     (In other words, sigprocmask() is atomic, because it is implemented as a
+     (In other words, pthread_sigmask() is atomic, because it is implemented as a
      system call.)
-     Whereas on native Windows, sigprocmask() is not atomic, because it
-     manipulates global variables.  Therefore in this case, we are *not*
-     allowed to call it from within a signal handler.  */
+     Whereas on native Windows, pthread_sigmask() = sigprocmask() is not atomic,
+     because it manipulates global variables.  Therefore in this case, we are
+     *not* allowed to call it from within a signal handler.  */
 
-  /* FIXME: Use pthread_sigmask, not sigprocmask, as the two functions
-     behave differently on macOS and the sigprocmask behavior can cause
-     this thread to race with other threads in harmful ways.  */
 #if defined _WIN32 && !defined __CYGWIN__
   if (!from_signal_handler)
 #endif
-    sigprocmask (SIG_BLOCK, mask, saved_mask);
+    pthread_sigmask (SIG_BLOCK, mask, saved_mask);
 
   glthread_spinlock_lock (lock);
 }
@@ -70,13 +67,10 @@ asyncsafe_spin_unlock (asyncsafe_spinlock_t *lock,
   if (glthread_spinlock_unlock (lock))
     abort ();
 
-  /* FIXME: Use pthread_sigmask, not sigprocmask, as the two functions
-     behave differently on macOS and the sigprocmask behavior can cause
-     this thread to race with other threads in harmful ways.  */
 #if defined _WIN32 && !defined __CYGWIN__
   if (!from_signal_handler)
 #endif
-    sigprocmask (SIG_SETMASK, saved_mask, NULL);
+    pthread_sigmask (SIG_SETMASK, saved_mask, NULL);
 }
 
 void
index 9c3d0097f1ca0b02a11bcca79b3ae5b88a17861c..c90cf2a336040ff4a97ceb965ab01eaaf1c4966b 100644 (file)
@@ -7,8 +7,8 @@ lib/asyncsafe-spin.c
 
 Depends-on:
 signal-h
+pthread_sigmask
 bool
-sigprocmask
 spin
 
 configure.ac:
@@ -19,6 +19,9 @@ lib_SOURCES += asyncsafe-spin.c
 Include:
 "asyncsafe-spin.h"
 
+Link:
+$(PTHREAD_SIGMASK_LIB)
+
 License:
 LGPLv2+
 
index dacb26aaace52bd316b24d6af1c93c0116396594..c39d11438d1c28d277a278962a51c823593f8ca6 100644 (file)
@@ -18,4 +18,5 @@ AC_REQUIRE([gl_SEMAPHORE])
 Makefile.am:
 TESTS += test-asyncsafe-spin1 test-asyncsafe-spin2
 check_PROGRAMS += test-asyncsafe-spin1 test-asyncsafe-spin2
+test_asyncsafe_spin1_LDADD = $(LDADD) @PTHREAD_SIGMASK_LIB@
 test_asyncsafe_spin2_LDADD = $(LDADD) @LIBMULTITHREAD@ @YIELD_LIB@ @LIB_SEMAPHORE@
index 36fe90b8e997ad385b107a55fc9a507c69a2bd6b..360d18472332304e992055fc99f22799e0594d74 100644 (file)
@@ -31,4 +31,4 @@ TESTS += test-cache
 check_PROGRAMS += test-cache
 test_cache_SOURCES = jit/test-cache.c
 test_cache_CFLAGS = $(AM_CFLAGS) $(DISABLE_OPENBSD_RETGUARD)
-test_cache_LDADD = $(LDADD) $(LIBINTL) @LIBDL@
+test_cache_LDADD = $(LDADD) $(LIBINTL) $(PTHREAD_SIGMASK_LIB) @LIBDL@