usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-pthread_sigmask2.c
blobfa3c1d8d91870edb00a8fd903f378e5a2a1f2fce
1 /* Test of pthread_sigmask in a multi-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 <errno.h>
24 #include <pthread.h>
25 #include <stdio.h>
26 #include <unistd.h>
28 #include "macros.h"
30 #if USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS
32 static pthread_t main_thread;
33 static pthread_t killer_thread;
35 static void *
36 killer_thread_func (_GL_UNUSED void *arg)
38 sleep (1);
39 pthread_kill (main_thread, SIGINT);
40 return NULL;
43 static volatile int sigint_occurred;
45 static void
46 sigint_handler (_GL_UNUSED int sig)
48 sigint_occurred++;
51 int
52 main ()
54 sigset_t set;
56 signal (SIGINT, sigint_handler);
58 sigemptyset (&set);
59 sigaddset (&set, SIGINT);
61 /* Check error handling. */
62 /* This call returns 0 on NetBSD 8.0. */
63 #if !defined __NetBSD__
64 ASSERT (pthread_sigmask (1729, &set, NULL) == EINVAL);
65 #endif
67 /* Block SIGINT. */
68 ASSERT (pthread_sigmask (SIG_BLOCK, &set, NULL) == 0);
70 /* Request a SIGINT signal from another thread. */
71 main_thread = pthread_self ();
72 ASSERT (pthread_create (&killer_thread, NULL, killer_thread_func, NULL) == 0);
74 /* Wait. */
75 sleep (2);
77 /* The signal should not have arrived yet, because it is blocked. */
78 ASSERT (sigint_occurred == 0);
80 /* Unblock SIGINT. */
81 ASSERT (pthread_sigmask (SIG_UNBLOCK, &set, NULL) == 0);
83 /* The signal should have arrived now, because POSIX says
84 "If there are any pending unblocked signals after the call to
85 pthread_sigmask(), at least one of those signals shall be delivered
86 before the call to pthread_sigmask() returns." */
87 ASSERT (sigint_occurred == 1);
89 /* Clean up the thread. This avoid a "ThreadSanitizer: thread leak" warning
90 from "gcc -fsanitize=thread". */
91 ASSERT (pthread_join (killer_thread, NULL) == 0);
93 return test_exit_status;
96 #else
98 int
99 main ()
101 fputs ("Skipping test: POSIX threads not enabled\n", stderr);
102 return 77;
105 #endif