1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/* Test that pthread_kill succeeds during thread exit.
Copyright (C) 2021-2023 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
/* This test verifies that pthread_kill for a thread that is exiting
succeeds (with or without actually delivering the signal). */
#include <array_length.h>
#include <stdbool.h>
#include <stddef.h>
#include <support/xsignal.h>
#include <support/xthread.h>
#include <unistd.h>
/* Set to true by timeout_thread_function when the test should
terminate. */
static bool timeout;
static void *
timeout_thread_function (void *unused)
{
usleep (1000 * 1000);
__atomic_store_n (&timeout, true, __ATOMIC_RELAXED);
return NULL;
}
/* Used to synchronize the sending threads with the target thread and
main thread. */
static pthread_barrier_t barrier_1;
static pthread_barrier_t barrier_2;
/* The target thread to which signals are to be sent. */
static pthread_t target_thread;
/* Set by the main thread to true after timeout has been set to
true. */
static bool exiting;
static void *
sender_thread_function (void *unused)
{
while (true)
{
/* Wait until target_thread has been initialized. The target
thread and main thread participate in this barrier. */
xpthread_barrier_wait (&barrier_1);
if (exiting)
break;
xpthread_kill (target_thread, SIGUSR1);
/* Communicate that the signal has been sent. The main thread
participates in this barrier. */
xpthread_barrier_wait (&barrier_2);
}
return NULL;
}
static void *
target_thread_function (void *unused)
{
target_thread = pthread_self ();
xpthread_barrier_wait (&barrier_1);
return NULL;
}
static int
do_test (void)
{
xsignal (SIGUSR1, SIG_IGN);
pthread_t thr_timeout = xpthread_create (NULL, timeout_thread_function, NULL);
pthread_t threads[4];
xpthread_barrier_init (&barrier_1, NULL, array_length (threads) + 2);
xpthread_barrier_init (&barrier_2, NULL, array_length (threads) + 1);
for (int i = 0; i < array_length (threads); ++i)
threads[i] = xpthread_create (NULL, sender_thread_function, NULL);
while (!__atomic_load_n (&timeout, __ATOMIC_RELAXED))
{
xpthread_create (NULL, target_thread_function, NULL);
/* Wait for the target thread to be set up and signal sending to
start. */
xpthread_barrier_wait (&barrier_1);
/* Wait for signal sending to complete. */
xpthread_barrier_wait (&barrier_2);
xpthread_join (target_thread);
}
exiting = true;
/* Signal the sending threads to exit. */
xpthread_create (NULL, target_thread_function, NULL);
xpthread_barrier_wait (&barrier_1);
for (int i = 0; i < array_length (threads); ++i)
xpthread_join (threads[i]);
xpthread_join (thr_timeout);
return 0;
}
#include <support/test-driver.c>
|