usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-pthread_sigmask1.c
blob93ed7eb1723ec54df198cf52666d2f8699644e13
1 /* Test of pthread_sigmask in a single-threaded program.
2 Copyright (C) 2011-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2011. */
19 #include <config.h>
21 #include <signal.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (pthread_sigmask, int, (int, const sigset_t *, sigset_t *));
26 #include <errno.h>
27 #include <inttypes.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
32 #include "macros.h"
34 #if !(defined _WIN32 && !defined __CYGWIN__)
36 static volatile int sigint_occurred;
38 static void
39 sigint_handler (_GL_UNUSED int sig)
41 sigint_occurred++;
44 int
45 main ()
47 sigset_t set;
48 intmax_t pid = getpid ();
49 char command[80];
51 signal (SIGINT, sigint_handler);
53 sigemptyset (&set);
54 sigaddset (&set, SIGINT);
56 /* Check error handling. */
57 ASSERT (pthread_sigmask (1729, &set, NULL) == EINVAL);
59 /* Block SIGINT. */
60 ASSERT (pthread_sigmask (SIG_BLOCK, &set, NULL) == 0);
62 /* Request a SIGINT signal from outside. */
63 sprintf (command, "sh -c 'sleep 1; kill -INT %"PRIdMAX"' &", pid);
64 ASSERT (system (command) == 0);
66 /* Wait. */
67 sleep (2);
69 /* The signal should not have arrived yet, because it is blocked. */
70 ASSERT (sigint_occurred == 0);
72 /* Unblock SIGINT. */
73 ASSERT (pthread_sigmask (SIG_UNBLOCK, &set, NULL) == 0);
75 /* The signal should have arrived now, because POSIX says
76 "If there are any pending unblocked signals after the call to
77 pthread_sigmask(), at least one of those signals shall be delivered
78 before the call to pthread_sigmask() returns." */
79 ASSERT (sigint_occurred == 1);
81 return test_exit_status;
84 #else
86 /* On native Windows, getpid() values and the arguments that are passed to
87 the (Cygwin?) 'kill' program are not necessarily related. */
89 int
90 main ()
92 fputs ("Skipping test: native Windows platform\n", stderr);
93 return 77;
96 #endif