Use `scheme-mode` for the Guile init file
[emacs.git] / m4 / pthread_sigmask.m4
blob437869f6dbec4e54d8e8c51ba19684086dc31672
1 # pthread_sigmask.m4
2 # serial 23
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], [
19 #include <pthread.h>
20 #include <signal.h>
21 #ifdef pthread_sigmask
22  headers_define_pthread_sigmask
23 #endif],
24        [gl_cv_func_pthread_sigmask_macro=yes],
25        [gl_cv_func_pthread_sigmask_macro=no])
26     ])
28   PTHREAD_SIGMASK_LIB=
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
35   else
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.
46           :
47         else
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"
53                AC_LINK_IFELSE(
54                  [AC_LANG_PROGRAM(
55                     [[#include <pthread.h>
56                       #include <signal.h>
57                     ]],
58                     [[return pthread_sigmask (0, (sigset_t *) 0, (sigset_t *) 0);]])
59                  ],
60                  [gl_cv_func_pthread_sigmask_in_LIBMULTITHREAD=yes],
61                  [gl_cv_func_pthread_sigmask_in_LIBMULTITHREAD=no])
62                LIBS="$gl_saved_LIBS"
63               ])
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"
67             else
68               dnl pthread_sigmask is not available at all.
69               HAVE_PTHREAD_SIGMASK=0
70             fi
71           else
72             dnl pthread_sigmask is not available at all.
73             HAVE_PTHREAD_SIGMASK=0
74           fi
75         fi
76       else
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
81         else
82           HAVE_PTHREAD_SIGMASK=0
83         fi
84       fi
85     ], [
86       dnl The module 'threadlib' is not in use, due to --avoid=threadlib being
87       dnl specified.
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.
93         :
94       else
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
101       fi
102     ])
103   fi
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
111   dnl same.
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
122       case " $LIBS " in
123         *' -pthread '*) ;;
124         *' -lpthread '*) ;;
125         *)
126           AC_CACHE_CHECK([whether pthread_sigmask works without -lpthread],
127             [gl_cv_func_pthread_sigmask_in_libc_works],
128             [
129               AC_RUN_IFELSE(
130                 [AC_LANG_SOURCE([[
131                    #include <pthread.h>
132                    #include <signal.h>
133                    #include <stddef.h>
134                    int main ()
135                    {
136                      sigset_t set;
137                      sigemptyset (&set);
138                      return pthread_sigmask (1729, &set, NULL) != 0;
139                    }]])],
140                 [gl_cv_func_pthread_sigmask_in_libc_works=no],
141                 [gl_cv_func_pthread_sigmask_in_libc_works=yes],
142                 [
143                  changequote(,)dnl
144                  case "$host_os" in
145                    freebsd* | midnightbsd* | hpux* | solaris | solaris2.[2-9]*)
146                      gl_cv_func_pthread_sigmask_in_libc_works="guessing no";;
147                    *)
148                      gl_cv_func_pthread_sigmask_in_libc_works="guessing yes";;
149                  esac
150                  changequote([,])dnl
151                 ])
152             ])
153           case "$gl_cv_func_pthread_sigmask_in_libc_works" in
154             *no)
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.])
158               ;;
159           esac;;
160       esac
161     fi
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],
167       [
168         gl_saved_LIBS="$LIBS"
169         LIBS="$LIBS $PTHREAD_SIGMASK_LIB"
170         AC_RUN_IFELSE(
171           [AC_LANG_SOURCE([[
172 #include <pthread.h>
173 #include <signal.h>
174 #include <stddef.h>
175 int main ()
177   sigset_t set;
178   sigemptyset (&set);
179   if (pthread_sigmask (1729, &set, NULL) == -1)
180     return 1;
181   return 0;
182 }]])],
183           [gl_cv_func_pthread_sigmask_return_works=yes],
184           [gl_cv_func_pthread_sigmask_return_works=no],
185           [case "$host_os" in
186              cygwin*)
187                gl_cv_func_pthread_sigmask_return_works="guessing no";;
188              *)
189                gl_cv_func_pthread_sigmask_return_works="guessing yes";;
190            esac
191           ])
192         LIBS="$gl_saved_LIBS"
193       ])
194     case "$gl_cv_func_pthread_sigmask_return_works" in
195       *no)
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.])
199         ;;
200     esac
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],
207       [
208         case "$host_os" in
209           irix*)
210             gl_cv_func_pthread_sigmask_unblock_works="guessing no";;
211           *)
212             gl_cv_func_pthread_sigmask_unblock_works="guessing yes";;
213         esac
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".
218            gl_saved_LIBS=$LIBS
219            LIBS="$LIBS $LIBMULTITHREAD"])
220         AC_RUN_IFELSE(
221           [AC_LANG_SOURCE([[
222 #include <limits.h>
223 #include <pthread.h>
224 #include <signal.h>
225 #include <stdio.h>
226 #include <stdlib.h>
227 #include <unistd.h>
228 ]GL_MDA_DEFINES[
229 static volatile int sigint_occurred;
230 static void
231 sigint_handler (int sig)
233   sigint_occurred++;
235 int main ()
237   sigset_t set;
238   pid_t pid = getpid ();
239   char command[80];
240   if (LONG_MAX < pid)
241     return 6;
242   signal (SIGINT, sigint_handler);
243   sigemptyset (&set);
244   sigaddset (&set, SIGINT);
245   if (!(pthread_sigmask (SIG_BLOCK, &set, NULL) == 0))
246     return 1;
247   sprintf (command, "sh -c 'sleep 1; kill -INT %ld' &", (long) pid);
248   if (!(system (command) == 0))
249     return 2;
250   sleep (2);
251   if (!(sigint_occurred == 0))
252     return 3;
253   if (!(pthread_sigmask (SIG_UNBLOCK, &set, NULL) == 0))
254     return 4;
255   if (!(sigint_occurred == 1)) /* This fails on IRIX.  */
256     return 5;
257   return 0;
258 }]])],
259           [:],
260           [gl_cv_func_pthread_sigmask_unblock_works=no],
261           [:])
262         m4_ifdef([gl_][THREADLIB], [LIBS=$gl_saved_LIBS])
263       ])
264     case "$gl_cv_func_pthread_sigmask_unblock_works" in
265       *no)
266         REPLACE_PTHREAD_SIGMASK=1
267         AC_DEFINE([PTHREAD_SIGMASK_UNBLOCK_BUG], [1],
268           [Define to 1 if pthread_sigmask() unblocks signals incorrectly.])
269         ;;
270     esac
271   fi
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).])
280   fi