Mercurial > gnulib
annotate tests/test-pthread_sigmask2.c @ 40220:2796695e9216
get-rusage-as, pthread_sigmask tests: Fix -fsanitize=thread findings.
* lib/get-rusage-as.c (get_rusage_as): When compiled by
"gcc -fsanitize=thread", don't try get_rusage_as_via_setrlimit.
* tests/test-pthread_sigmask2.c (main): Clean up the killer_thread
before exiting.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sun, 10 Mar 2019 01:28:42 +0100 |
parents | b06060465f09 |
children |
rev | line source |
---|---|
15379
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* Test of pthread_sigmask in a multi-threaded program. |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
2 Copyright (C) 2011-2019 Free Software Foundation, Inc. |
15379
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
7 (at your option) any later version. |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
8 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
12 GNU General Public License for more details. |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
13 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
14 You should have received a copy of the GNU General Public License |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
15379
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
16 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 /* Written by Bruno Haible <bruno@clisp.org>, 2011. */ |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
18 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
19 #include <config.h> |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
21 #include <signal.h> |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
22 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
23 #include <errno.h> |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
24 #include <stdio.h> |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
25 #include <unistd.h> |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
26 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
27 #include "glthread/thread.h" |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
28 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 #include "macros.h" |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
30 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
31 #if USE_POSIX_THREADS |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
32 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
33 static gl_thread_t main_thread; |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
34 static gl_thread_t killer_thread; |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
35 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
36 static void * |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
37 killer_thread_func (void *arg) |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
38 { |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
39 sleep (1); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
40 pthread_kill (main_thread, SIGINT); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
41 return NULL; |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
42 } |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
43 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
44 static volatile int sigint_occurred; |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
45 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
46 static void |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
47 sigint_handler (int sig) |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
48 { |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
49 sigint_occurred++; |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
50 } |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
51 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
52 int |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
53 main (int argc, char *argv[]) |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
54 { |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
55 sigset_t set; |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
56 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
57 signal (SIGINT, sigint_handler); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
58 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
59 sigemptyset (&set); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
60 sigaddset (&set, SIGINT); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
61 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
62 /* Check error handling. */ |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
63 ASSERT (pthread_sigmask (1729, &set, NULL) == EINVAL); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
64 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
65 /* Block SIGINT. */ |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
66 ASSERT (pthread_sigmask (SIG_BLOCK, &set, NULL) == 0); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
67 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
68 /* Request a SIGINT signal from another thread. */ |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
69 main_thread = gl_thread_self (); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
70 ASSERT (glthread_create (&killer_thread, killer_thread_func, NULL) == 0); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
71 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
72 /* Wait. */ |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
73 sleep (2); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
74 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
75 /* The signal should not have arrived yet, because it is blocked. */ |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
76 ASSERT (sigint_occurred == 0); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
77 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
78 /* Unblock SIGINT. */ |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
79 ASSERT (pthread_sigmask (SIG_UNBLOCK, &set, NULL) == 0); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
80 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
81 /* The signal should have arrived now, because POSIX says |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
82 "If there are any pending unblocked signals after the call to |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
83 pthread_sigmask(), at least one of those signals shall be delivered |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
84 before the call to pthread_sigmask() returns." */ |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
85 ASSERT (sigint_occurred == 1); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
86 |
40220
2796695e9216
get-rusage-as, pthread_sigmask tests: Fix -fsanitize=thread findings.
Bruno Haible <bruno@clisp.org>
parents:
40057
diff
changeset
|
87 /* Clean up the thread. This avoid a "ThreadSanitizer: thread leak" warning |
2796695e9216
get-rusage-as, pthread_sigmask tests: Fix -fsanitize=thread findings.
Bruno Haible <bruno@clisp.org>
parents:
40057
diff
changeset
|
88 from "gcc -fsanitize=thread". */ |
2796695e9216
get-rusage-as, pthread_sigmask tests: Fix -fsanitize=thread findings.
Bruno Haible <bruno@clisp.org>
parents:
40057
diff
changeset
|
89 gl_thread_join (killer_thread, NULL); |
2796695e9216
get-rusage-as, pthread_sigmask tests: Fix -fsanitize=thread findings.
Bruno Haible <bruno@clisp.org>
parents:
40057
diff
changeset
|
90 |
15379
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
91 return 0; |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
92 } |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
93 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
94 #else |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
95 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
96 int |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
97 main () |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
98 { |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
99 fputs ("Skipping test: POSIX threads not enabled\n", stderr); |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
100 return 77; |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
101 } |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
102 |
ca34a467b2b5
Tests for module 'pthread_sigmask'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
103 #endif |