| 1 | # pthread_sigmask.m4 serial 13 |
| 2 | dnl Copyright (C) 2011-2013 Free Software Foundation, Inc. |
| 3 | dnl This file is free software; the Free Software Foundation |
| 4 | dnl gives unlimited permission to copy and/or distribute it, |
| 5 | dnl with or without modifications, as long as this notice is preserved. |
| 6 | |
| 7 | AC_DEFUN([gl_FUNC_PTHREAD_SIGMASK], |
| 8 | [ |
| 9 | AC_REQUIRE([gl_SIGNAL_H_DEFAULTS]) |
| 10 | |
| 11 | AC_CHECK_FUNCS_ONCE([pthread_sigmask]) |
| 12 | LIB_PTHREAD_SIGMASK= |
| 13 | |
| 14 | dnl Test whether the gnulib module 'threadlib' is in use. |
| 15 | dnl Some packages like Emacs use --avoid=threadlib. |
| 16 | dnl Write the symbol in such a way that it does not cause 'aclocal' to pick |
| 17 | dnl the threadlib.m4 file that is installed in $PREFIX/share/aclocal/. |
| 18 | m4_ifdef([gl_][THREADLIB], [ |
| 19 | AC_REQUIRE([gl_][THREADLIB]) |
| 20 | |
| 21 | if test "$gl_threads_api" = posix; then |
| 22 | if test $ac_cv_func_pthread_sigmask = yes; then |
| 23 | dnl pthread_sigmask is available without -lpthread. |
| 24 | : |
| 25 | else |
| 26 | if test -n "$LIBMULTITHREAD"; then |
| 27 | AC_CACHE_CHECK([for pthread_sigmask in $LIBMULTITHREAD], |
| 28 | [gl_cv_func_pthread_sigmask_in_LIBMULTITHREAD], |
| 29 | [gl_save_LIBS="$LIBS" |
| 30 | LIBS="$LIBS $LIBMULTITHREAD" |
| 31 | AC_LINK_IFELSE( |
| 32 | [AC_LANG_PROGRAM( |
| 33 | [[#include <pthread.h> |
| 34 | #include <signal.h> |
| 35 | ]], |
| 36 | [[return pthread_sigmask (0, (sigset_t *) 0, (sigset_t *) 0);]]) |
| 37 | ], |
| 38 | [gl_cv_func_pthread_sigmask_in_LIBMULTITHREAD=yes], |
| 39 | [gl_cv_func_pthread_sigmask_in_LIBMULTITHREAD=no]) |
| 40 | LIBS="$gl_save_LIBS" |
| 41 | ]) |
| 42 | if test $gl_cv_func_pthread_sigmask_in_LIBMULTITHREAD = yes; then |
| 43 | dnl pthread_sigmask is available with -lpthread. |
| 44 | LIB_PTHREAD_SIGMASK="$LIBMULTITHREAD" |
| 45 | else |
| 46 | dnl pthread_sigmask is not available at all. |
| 47 | HAVE_PTHREAD_SIGMASK=0 |
| 48 | fi |
| 49 | else |
| 50 | dnl pthread_sigmask is not available at all. |
| 51 | HAVE_PTHREAD_SIGMASK=0 |
| 52 | fi |
| 53 | fi |
| 54 | else |
| 55 | dnl pthread_sigmask may exist but does not interoperate with the chosen |
| 56 | dnl multithreading facility. |
| 57 | dnl If "$gl_threads_api" = pth, we could use the function pth_sigmask, |
| 58 | dnl but it is equivalent to sigprocmask, so we choose to emulate |
| 59 | dnl pthread_sigmask with sigprocmask also in this case. This yields fewer |
| 60 | dnl link dependencies. |
| 61 | if test $ac_cv_func_pthread_sigmask = yes; then |
| 62 | REPLACE_PTHREAD_SIGMASK=1 |
| 63 | else |
| 64 | HAVE_PTHREAD_SIGMASK=0 |
| 65 | fi |
| 66 | fi |
| 67 | ], [ |
| 68 | dnl The module 'threadlib' is not in use, due to --avoid=threadlib being |
| 69 | dnl specified. |
| 70 | dnl The package either has prepared CPPFLAGS and LIBS for use of POSIX:2008 |
| 71 | dnl threads, or wants to build single-threaded programs. |
| 72 | if test $ac_cv_func_pthread_sigmask = yes; then |
| 73 | dnl pthread_sigmask exists and does not require extra libraries. |
| 74 | dnl Assume that it is declared. |
| 75 | : |
| 76 | else |
| 77 | dnl pthread_sigmask either does not exist or needs extra libraries. |
| 78 | HAVE_PTHREAD_SIGMASK=0 |
| 79 | dnl Define the symbol rpl_pthread_sigmask, not pthread_sigmask, |
| 80 | dnl so as to not accidentally override the system's pthread_sigmask |
| 81 | dnl symbol from libpthread. This is necessary on IRIX 6.5. |
| 82 | REPLACE_PTHREAD_SIGMASK=1 |
| 83 | fi |
| 84 | ]) |
| 85 | |
| 86 | AC_SUBST([LIB_PTHREAD_SIGMASK]) |
| 87 | dnl We don't need a variable LTLIB_PTHREAD_SIGMASK, because when |
| 88 | dnl "$gl_threads_api" = posix, $LTLIBMULTITHREAD and $LIBMULTITHREAD are the |
| 89 | dnl same: either both empty or both "-lpthread". |
| 90 | |
| 91 | dnl Now test for some bugs in the system function. |
| 92 | if test $HAVE_PTHREAD_SIGMASK = 1; then |
| 93 | AC_REQUIRE([AC_PROG_CC]) |
| 94 | AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 95 | |
| 96 | dnl On FreeBSD 6.4, HP-UX 11.31, Solaris 9, in programs that are not linked |
| 97 | dnl with -lpthread, the pthread_sigmask() function always returns 0 and has |
| 98 | dnl no effect. |
| 99 | if test -z "$LIB_PTHREAD_SIGMASK"; then |
| 100 | AC_CACHE_CHECK([whether pthread_sigmask works without -lpthread], |
| 101 | [gl_cv_func_pthread_sigmask_in_libc_works], |
| 102 | [ |
| 103 | AC_RUN_IFELSE( |
| 104 | [AC_LANG_SOURCE([[ |
| 105 | #include <pthread.h> |
| 106 | #include <signal.h> |
| 107 | #include <stddef.h> |
| 108 | int main () |
| 109 | { |
| 110 | sigset_t set; |
| 111 | sigemptyset (&set); |
| 112 | return pthread_sigmask (1729, &set, NULL) != 0; |
| 113 | }]])], |
| 114 | [gl_cv_func_pthread_sigmask_in_libc_works=no], |
| 115 | [gl_cv_func_pthread_sigmask_in_libc_works=yes], |
| 116 | [ |
| 117 | changequote(,)dnl |
| 118 | case "$host_os" in |
| 119 | freebsd* | hpux* | solaris | solaris2.[2-9]*) |
| 120 | gl_cv_func_pthread_sigmask_in_libc_works="guessing no";; |
| 121 | *) |
| 122 | gl_cv_func_pthread_sigmask_in_libc_works="guessing yes";; |
| 123 | esac |
| 124 | changequote([,])dnl |
| 125 | ]) |
| 126 | ]) |
| 127 | case "$gl_cv_func_pthread_sigmask_in_libc_works" in |
| 128 | *no) |
| 129 | REPLACE_PTHREAD_SIGMASK=1 |
| 130 | AC_DEFINE([PTHREAD_SIGMASK_INEFFECTIVE], [1], |
| 131 | [Define to 1 if pthread_sigmask() may returns 0 and have no effect.]) |
| 132 | ;; |
| 133 | esac |
| 134 | fi |
| 135 | |
| 136 | dnl On Cygwin 1.7.5, the pthread_sigmask() has a wrong return value |
| 137 | dnl convention: Upon failure, it returns -1 and sets errno. |
| 138 | AC_CACHE_CHECK([whether pthread_sigmask returns error numbers], |
| 139 | [gl_cv_func_pthread_sigmask_return_works], |
| 140 | [ |
| 141 | gl_save_LIBS="$LIBS" |
| 142 | LIBS="$LIBS $LIB_PTHREAD_SIGMASK" |
| 143 | AC_RUN_IFELSE( |
| 144 | [AC_LANG_SOURCE([[ |
| 145 | #include <pthread.h> |
| 146 | #include <signal.h> |
| 147 | #include <stddef.h> |
| 148 | int main () |
| 149 | { |
| 150 | sigset_t set; |
| 151 | sigemptyset (&set); |
| 152 | if (pthread_sigmask (1729, &set, NULL) == -1) |
| 153 | return 1; |
| 154 | return 0; |
| 155 | }]])], |
| 156 | [gl_cv_func_pthread_sigmask_return_works=yes], |
| 157 | [gl_cv_func_pthread_sigmask_return_works=no], |
| 158 | [case "$host_os" in |
| 159 | cygwin*) |
| 160 | gl_cv_func_pthread_sigmask_return_works="guessing no";; |
| 161 | *) |
| 162 | gl_cv_func_pthread_sigmask_return_works="guessing yes";; |
| 163 | esac |
| 164 | ]) |
| 165 | LIBS="$gl_save_LIBS" |
| 166 | ]) |
| 167 | case "$gl_cv_func_pthread_sigmask_return_works" in |
| 168 | *no) |
| 169 | REPLACE_PTHREAD_SIGMASK=1 |
| 170 | AC_DEFINE([PTHREAD_SIGMASK_FAILS_WITH_ERRNO], [1], |
| 171 | [Define to 1 if pthread_sigmask(), when it fails, returns -1 and sets errno.]) |
| 172 | ;; |
| 173 | esac |
| 174 | |
| 175 | dnl On IRIX 6.5, in a single-threaded program, pending signals are not |
| 176 | dnl immediately delivered when they are unblocked through pthread_sigmask, |
| 177 | dnl only a little while later. |
| 178 | AC_CACHE_CHECK([whether pthread_sigmask unblocks signals correctly], |
| 179 | [gl_cv_func_pthread_sigmask_unblock_works], |
| 180 | [ |
| 181 | case "$host_os" in |
| 182 | irix*) |
| 183 | gl_cv_func_pthread_sigmask_unblock_works="guessing no";; |
| 184 | *) |
| 185 | gl_cv_func_pthread_sigmask_unblock_works="guessing yes";; |
| 186 | esac |
| 187 | dnl Here we link against $LIBMULTITHREAD, not only $LIB_PTHREAD_SIGMASK, |
| 188 | dnl otherwise we get a false positive on those platforms where |
| 189 | dnl $gl_cv_func_pthread_sigmask_in_libc_works is "no". |
| 190 | gl_save_LIBS="$LIBS" |
| 191 | LIBS="$LIBS $LIBMULTITHREAD" |
| 192 | AC_RUN_IFELSE( |
| 193 | [AC_LANG_SOURCE([[ |
| 194 | #include <pthread.h> |
| 195 | #include <signal.h> |
| 196 | #include <stdio.h> |
| 197 | #include <stdlib.h> |
| 198 | #include <unistd.h> |
| 199 | static volatile int sigint_occurred; |
| 200 | static void |
| 201 | sigint_handler (int sig) |
| 202 | { |
| 203 | sigint_occurred++; |
| 204 | } |
| 205 | int main () |
| 206 | { |
| 207 | sigset_t set; |
| 208 | int pid = getpid (); |
| 209 | char command[80]; |
| 210 | signal (SIGINT, sigint_handler); |
| 211 | sigemptyset (&set); |
| 212 | sigaddset (&set, SIGINT); |
| 213 | if (!(pthread_sigmask (SIG_BLOCK, &set, NULL) == 0)) |
| 214 | return 1; |
| 215 | sprintf (command, "sh -c 'sleep 1; kill -%d %d' &", SIGINT, pid); |
| 216 | if (!(system (command) == 0)) |
| 217 | return 2; |
| 218 | sleep (2); |
| 219 | if (!(sigint_occurred == 0)) |
| 220 | return 3; |
| 221 | if (!(pthread_sigmask (SIG_UNBLOCK, &set, NULL) == 0)) |
| 222 | return 4; |
| 223 | if (!(sigint_occurred == 1)) /* This fails on IRIX. */ |
| 224 | return 5; |
| 225 | return 0; |
| 226 | }]])], |
| 227 | [:], |
| 228 | [gl_cv_func_pthread_sigmask_unblock_works=no], |
| 229 | [:]) |
| 230 | LIBS="$gl_save_LIBS" |
| 231 | ]) |
| 232 | case "$gl_cv_func_pthread_sigmask_unblock_works" in |
| 233 | *no) |
| 234 | REPLACE_PTHREAD_SIGMASK=1 |
| 235 | AC_DEFINE([PTHREAD_SIGMASK_UNBLOCK_BUG], [1], |
| 236 | [Define to 1 if pthread_sigmask() unblocks signals incorrectly.]) |
| 237 | ;; |
| 238 | esac |
| 239 | fi |
| 240 | ]) |
| 241 | |
| 242 | # Prerequisite of lib/pthread_sigmask.c. |
| 243 | AC_DEFUN([gl_PREREQ_PTHREAD_SIGMASK], |
| 244 | [ |
| 245 | if test $HAVE_PTHREAD_SIGMASK = 1; then |
| 246 | AC_DEFINE([HAVE_PTHREAD_SIGMASK], [1], |
| 247 | [Define to 1 if the pthread_sigmask function can be used (despite bugs).]) |
| 248 | fi |
| 249 | ]) |