3 dnl Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 AC_DEFUN([gl_FUNC_PTHREAD_SIGMASK],
10 AC_REQUIRE([gl_SIGNAL_H_DEFAULTS])
12 AC_CHECK_FUNCS_ONCE([pthread_sigmask])
14 dnl On MinGW pthread_sigmask is just a macro which always returns 0.
15 dnl It does not exist as a real function, which is required by POSIX.
16 AC_CACHE_CHECK([whether pthread_sigmask is a macro],
17 [gl_cv_func_pthread_sigmask_macro],
18 [AC_EGREP_CPP([headers_define_pthread_sigmask], [
21 #ifdef pthread_sigmask
22 headers_define_pthread_sigmask
24 [gl_cv_func_pthread_sigmask_macro=yes],
25 [gl_cv_func_pthread_sigmask_macro=no])
30 if test $gl_cv_func_pthread_sigmask_macro = yes; then
31 dnl pthread_sigmask is a dummy macro.
32 HAVE_PTHREAD_SIGMASK=0
33 dnl Make sure to '#undef pthread_sigmask' before defining it.
34 REPLACE_PTHREAD_SIGMASK=1
36 dnl Test whether the gnulib module 'threadlib' is in use.
37 dnl Some packages like Emacs use --avoid=threadlib.
38 dnl Write the symbol in such a way that it does not cause 'aclocal' to pick
39 dnl the threadlib.m4 file that is installed in $PREFIX/share/aclocal/.
40 m4_ifdef([gl_][THREADLIB], [
41 AC_REQUIRE([gl_][THREADLIB])
43 if test "$gl_threads_api" = posix; then
44 if test $ac_cv_func_pthread_sigmask = yes; then
45 dnl pthread_sigmask is available without -lpthread.
48 if test -n "$LIBMULTITHREAD"; then
49 AC_CACHE_CHECK([for pthread_sigmask in $LIBMULTITHREAD],
50 [gl_cv_func_pthread_sigmask_in_LIBMULTITHREAD],
51 [gl_saved_LIBS="$LIBS"
52 LIBS="$LIBS $LIBMULTITHREAD"
55 [[#include <pthread.h>
58 [[return pthread_sigmask (0, (sigset_t *) 0, (sigset_t *) 0);]])
60 [gl_cv_func_pthread_sigmask_in_LIBMULTITHREAD=yes],
61 [gl_cv_func_pthread_sigmask_in_LIBMULTITHREAD=no])
64 if test $gl_cv_func_pthread_sigmask_in_LIBMULTITHREAD = yes; then
65 dnl pthread_sigmask is available with -pthread or -lpthread.
66 PTHREAD_SIGMASK_LIB="$LIBMULTITHREAD"
68 dnl pthread_sigmask is not available at all.
69 HAVE_PTHREAD_SIGMASK=0
72 dnl pthread_sigmask is not available at all.
73 HAVE_PTHREAD_SIGMASK=0
77 dnl pthread_sigmask may exist but does not interoperate with the chosen
78 dnl multithreading facility.
79 if test $ac_cv_func_pthread_sigmask = yes; then
80 REPLACE_PTHREAD_SIGMASK=1
82 HAVE_PTHREAD_SIGMASK=0
86 dnl The module 'threadlib' is not in use, due to --avoid=threadlib being
88 dnl The package either has prepared CPPFLAGS and LIBS for use of
89 dnl POSIX:2008 threads, or wants to build single-threaded programs.
90 if test $ac_cv_func_pthread_sigmask = yes; then
91 dnl pthread_sigmask exists and does not require extra libraries.
92 dnl Assume that it is declared.
95 dnl pthread_sigmask either does not exist or needs extra libraries.
96 HAVE_PTHREAD_SIGMASK=0
97 dnl Define the symbol rpl_pthread_sigmask, not pthread_sigmask,
98 dnl so as to not accidentally override the system's pthread_sigmask
99 dnl symbol from libpthread. This is necessary on IRIX 6.5.
100 REPLACE_PTHREAD_SIGMASK=1
105 AC_SUBST([PTHREAD_SIGMASK_LIB])
106 dnl For backward compatibility.
107 LIB_PTHREAD_SIGMASK="$PTHREAD_SIGMASK_LIB"
108 AC_SUBST([LIB_PTHREAD_SIGMASK])
109 dnl We don't need a variable LTLIB_PTHREAD_SIGMASK, because when
110 dnl "$gl_threads_api" = posix, $LTLIBMULTITHREAD and $LIBMULTITHREAD are the
113 dnl Now test for some bugs in the system function.
114 if test $HAVE_PTHREAD_SIGMASK = 1; then
115 AC_REQUIRE([AC_PROG_CC])
116 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
118 dnl On FreeBSD 13.0, MidnightBSD 1.1, HP-UX 11.31, Solaris 9, in programs
119 dnl that are not linked with -lpthread, the pthread_sigmask() function
120 dnl always returns 0 and has no effect.
121 if test -z "$PTHREAD_SIGMASK_LIB"; then
126 AC_CACHE_CHECK([whether pthread_sigmask works without -lpthread],
127 [gl_cv_func_pthread_sigmask_in_libc_works],
138 return pthread_sigmask (1729, &set, NULL) != 0;
140 [gl_cv_func_pthread_sigmask_in_libc_works=no],
141 [gl_cv_func_pthread_sigmask_in_libc_works=yes],
145 freebsd* | midnightbsd* | hpux* | solaris | solaris2.[2-9]*)
146 gl_cv_func_pthread_sigmask_in_libc_works="guessing no";;
148 gl_cv_func_pthread_sigmask_in_libc_works="guessing yes";;
153 case "$gl_cv_func_pthread_sigmask_in_libc_works" in
155 REPLACE_PTHREAD_SIGMASK=1
156 AC_DEFINE([PTHREAD_SIGMASK_INEFFECTIVE], [1],
157 [Define to 1 if pthread_sigmask may return 0 and have no effect.])
163 dnl On Cygwin 1.7.5, the pthread_sigmask() has a wrong return value
164 dnl convention: Upon failure, it returns -1 and sets errno.
165 AC_CACHE_CHECK([whether pthread_sigmask returns error numbers],
166 [gl_cv_func_pthread_sigmask_return_works],
168 gl_saved_LIBS="$LIBS"
169 LIBS="$LIBS $PTHREAD_SIGMASK_LIB"
179 if (pthread_sigmask (1729, &set, NULL) == -1)
183 [gl_cv_func_pthread_sigmask_return_works=yes],
184 [gl_cv_func_pthread_sigmask_return_works=no],
187 gl_cv_func_pthread_sigmask_return_works="guessing no";;
189 gl_cv_func_pthread_sigmask_return_works="guessing yes";;
192 LIBS="$gl_saved_LIBS"
194 case "$gl_cv_func_pthread_sigmask_return_works" in
196 REPLACE_PTHREAD_SIGMASK=1
197 AC_DEFINE([PTHREAD_SIGMASK_FAILS_WITH_ERRNO], [1],
198 [Define to 1 if pthread_sigmask(), when it fails, returns -1 and sets errno.])
202 dnl On IRIX 6.5, in a single-threaded program, pending signals are not
203 dnl immediately delivered when they are unblocked through pthread_sigmask,
204 dnl only a little while later.
205 AC_CACHE_CHECK([whether pthread_sigmask unblocks signals correctly],
206 [gl_cv_func_pthread_sigmask_unblock_works],
210 gl_cv_func_pthread_sigmask_unblock_works="guessing no";;
212 gl_cv_func_pthread_sigmask_unblock_works="guessing yes";;
214 m4_ifdef([gl_][THREADLIB],
215 [dnl Link against $LIBMULTITHREAD, not only $PTHREAD_SIGMASK_LIB.
216 dnl Otherwise we get a false positive on those platforms where
217 dnl $gl_cv_func_pthread_sigmask_in_libc_works is "no".
219 LIBS="$LIBS $LIBMULTITHREAD"])
229 static volatile int sigint_occurred;
231 sigint_handler (int sig)
238 pid_t pid = getpid ();
242 signal (SIGINT, sigint_handler);
244 sigaddset (&set, SIGINT);
245 if (!(pthread_sigmask (SIG_BLOCK, &set, NULL) == 0))
247 sprintf (command, "sh -c 'sleep 1; kill -INT %ld' &", (long) pid);
248 if (!(system (command) == 0))
251 if (!(sigint_occurred == 0))
253 if (!(pthread_sigmask (SIG_UNBLOCK, &set, NULL) == 0))
255 if (!(sigint_occurred == 1)) /* This fails on IRIX. */
260 [gl_cv_func_pthread_sigmask_unblock_works=no],
262 m4_ifdef([gl_][THREADLIB], [LIBS=$gl_saved_LIBS])
264 case "$gl_cv_func_pthread_sigmask_unblock_works" in
266 REPLACE_PTHREAD_SIGMASK=1
267 AC_DEFINE([PTHREAD_SIGMASK_UNBLOCK_BUG], [1],
268 [Define to 1 if pthread_sigmask() unblocks signals incorrectly.])
274 # Prerequisite of lib/pthread_sigmask.c.
275 AC_DEFUN([gl_PREREQ_PTHREAD_SIGMASK],
277 if test $HAVE_PTHREAD_SIGMASK = 1; then
278 AC_DEFINE([HAVE_PTHREAD_SIGMASK], [1],
279 [Define to 1 if the pthread_sigmask function can be used (despite bugs).])