1 /*
2 * Copyright (c) 2005, Bull S.A.. All rights reserved.
3 * Created by: Sebastien Decugis
4
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16
17 * This sample test aims to check the following assertion:
18 *
19 * If several threads are waiting for a signal and this signal is generated
20 * for a specific thread, only this thread is unblocked.
21
22 * The steps are:
23 * -> mask SIGUSR1
24 * -> create several threads which sigwait for SIGUSR1
25 * -> pthread_kill one of the threads
26 * -> Check than this thread has been awaken.
27
28 * The test fails if the thread is not awaken.
29
30 */
31
32
33 /******************************************************************************/
34 /*************************** standard includes ********************************/
35 /******************************************************************************/
36 #include <pthread.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <signal.h>
44 #include <errno.h>
45
46 /******************************************************************************/
47 /*************************** Test framework *******************************/
48 /******************************************************************************/
49 #include "../testfrmw/testfrmw.h"
50 #include "../testfrmw/testfrmw.c"
51 /* This header is responsible for defining the following macros:
52 * UNRESOLVED(ret, descr);
53 * where descr is a description of the error and ret is an int
54 * (error code for example)
55 * FAILED(descr);
56 * where descr is a short text saying why the test has failed.
57 * PASSED();
58 * No parameter.
59 *
60 * Both three macros shall terminate the calling process.
61 * The testcase shall not terminate in any other maneer.
62 *
63 * The other file defines the functions
64 * void output_init()
65 * void output(char * string, ...)
66 *
67 * Those may be used to output information.
68 */
69
70 /******************************************************************************/
71 /**************************** Configuration ***********************************/
72 /******************************************************************************/
73 #ifndef VERBOSE
74 #define VERBOSE 1
75 #endif
76
77 #define NTHREADS 5
78
79 /******************************************************************************/
80 /*************************** Test case ***********************************/
81 /******************************************************************************/
82
83 int n_awaken = 0;
84 pthread_t last_awaken;
85 sigset_t setusr;
86
87 /* Thread function */
threaded(void * arg)88 void *threaded(void *arg)
89 {
90 int ret;
91 int sig;
92
93 /* The signal is already masked, because inherited from the parent */
94
95 /* wait for the signal */
96 ret = sigwait(&setusr, &sig);
97
98 if (ret != 0) {
99 UNRESOLVED(ret, "failed to wait for signal in thread");
100 }
101
102 n_awaken++;
103
104 last_awaken = pthread_self();
105
106 /* quit */
107 return NULL;
108 }
109
110 /* The main test function. */
main(void)111 int main(void)
112 {
113 int ret, i;
114 pthread_t ch[NTHREADS];
115
116 /* Initialize output */
117 output_init();
118
119 /* Set the signal mask */
120 ret = sigemptyset(&setusr);
121
122 if (ret != 0) {
123 UNRESOLVED(ret, "Failed to empty signal set");
124 }
125
126 ret = sigaddset(&setusr, SIGUSR1);
127
128 if (ret != 0) {
129 UNRESOLVED(ret, "failed to add SIGUSR1 to signal set");
130 }
131
132 ret = pthread_sigmask(SIG_BLOCK, &setusr, NULL);
133
134 if (ret != 0) {
135 UNRESOLVED(ret, "Failed to block SIGUSR1");
136 }
137
138 /* Create the children */
139
140 for (i = 0; i < NTHREADS; i++) {
141 ret = pthread_create(&ch[i], NULL, threaded, NULL);
142
143 if (ret != 0) {
144 UNRESOLVED(ret, "Failed to create a thread");
145 }
146 }
147
148 /* raise the signal */
149 ret = pthread_kill(ch[0], SIGUSR1);
150
151 if (ret != 0) {
152 UNRESOLVED(ret, "Failed to raise the signal");
153 }
154
155 sleep(1);
156
157 if (n_awaken != 1) {
158 output("%d threads were awaken\n", n_awaken);
159 FAILED("Unexpected number of threads awaken");
160 }
161
162 if (!pthread_equal(last_awaken, ch[0])) {
163 FAILED("The awaken thread is not the signal target one.");
164 }
165
166 /* Wake other threads */
167 for (i = 1; i < NTHREADS; i++) {
168 ret = pthread_kill(ch[i], SIGUSR1);
169
170 if (ret != 0) {
171 UNRESOLVED(ret, "Failed to raise the signal");
172 }
173 }
174
175 /* Wait for child thread termination */
176 for (i = 0; i < NTHREADS; i++) {
177 ret = pthread_join(ch[i], NULL);
178
179 if (ret != 0) {
180 UNRESOLVED(ret, "Failed to join the thread");
181 }
182 }
183
184 /* Test passed */
185 #if VERBOSE > 0
186
187 output("Test passed\n");
188
189 #endif
190
191 PASSED;
192 }
193