truncate: New module.
[gnulib/ericb.git] / tests / test-pthread_sigmask2.c
blobca744313e001e1a9e92e7b1ddf0595f2931cac04
1 /* Test of pthread_sigmask in a multi-threaded program.
2 Copyright (C) 2011-2017 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 <http://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 <stdio.h>
25 #include <unistd.h>
27 #include "glthread/thread.h"
29 #include "macros.h"
31 #if USE_POSIX_THREADS
33 static gl_thread_t main_thread;
34 static gl_thread_t killer_thread;
36 static void *
37 killer_thread_func (void *arg)
39 sleep (1);
40 pthread_kill (main_thread, SIGINT);
41 return NULL;
44 static volatile int sigint_occurred;
46 static void
47 sigint_handler (int sig)
49 sigint_occurred++;
52 int
53 main (int argc, char *argv[])
55 sigset_t set;
57 signal (SIGINT, sigint_handler);
59 sigemptyset (&set);
60 sigaddset (&set, SIGINT);
62 /* Check error handling. */
63 ASSERT (pthread_sigmask (1729, &set, NULL) == EINVAL);
65 /* Block SIGINT. */
66 ASSERT (pthread_sigmask (SIG_BLOCK, &set, NULL) == 0);
68 /* Request a SIGINT signal from another thread. */
69 main_thread = gl_thread_self ();
70 ASSERT (glthread_create (&killer_thread, killer_thread_func, NULL) == 0);
72 /* Wait. */
73 sleep (2);
75 /* The signal should not have arrived yet, because it is blocked. */
76 ASSERT (sigint_occurred == 0);
78 /* Unblock SIGINT. */
79 ASSERT (pthread_sigmask (SIG_UNBLOCK, &set, NULL) == 0);
81 /* The signal should have arrived now, because POSIX says
82 "If there are any pending unblocked signals after the call to
83 pthread_sigmask(), at least one of those signals shall be delivered
84 before the call to pthread_sigmask() returns." */
85 ASSERT (sigint_occurred == 1);
87 return 0;
90 #else
92 int
93 main ()
95 fputs ("Skipping test: POSIX threads not enabled\n", stderr);
96 return 77;
99 #endif