• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 blocked in a call to sigwait,
20 * only one is unblocked when the signal becomes pending.
21 
22 * The steps are:
23 * -> mask SIGUSR1
24 * -> create several threads which sigwait for SIGUSR1
25 * -> raise SIGUSR1
26 * -> Check than only 1 thread has been awaken.
27 
28 * The test fails if less or more than 1 thread returns from sigwait when the
29 * signal is raised.
30 
31 */
32 
33 
34 /******************************************************************************/
35 /*************************** standard includes ********************************/
36 /******************************************************************************/
37 #include <pthread.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include <signal.h>
45 #include <errno.h>
46 
47 /******************************************************************************/
48 /***************************   Test framework   *******************************/
49 /******************************************************************************/
50 #include "../testfrmw/testfrmw.h"
51 #include "../testfrmw/testfrmw.c"
52 /* This header is responsible for defining the following macros:
53  * UNRESOLVED(ret, descr);
54  *    where descr is a description of the error and ret is an int
55  *   (error code for example)
56  * FAILED(descr);
57  *    where descr is a short text saying why the test has failed.
58  * PASSED();
59  *    No parameter.
60  *
61  * Both three macros shall terminate the calling process.
62  * The testcase shall not terminate in any other maneer.
63  *
64  * The other file defines the functions
65  * void output_init()
66  * void output(char * string, ...)
67  *
68  * Those may be used to output information.
69  */
70 
71 /******************************************************************************/
72 /**************************** Configuration ***********************************/
73 /******************************************************************************/
74 #ifndef VERBOSE
75 #define VERBOSE 1
76 #endif
77 
78 #define NTHREADS 5
79 
80 /******************************************************************************/
81 /***************************    Test case   ***********************************/
82 /******************************************************************************/
83 
84 int n_awaken = 0;
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 	/* quit */
105 	return NULL;
106 }
107 
108 /* The main test function. */
main(void)109 int main(void)
110 {
111 	int ret, i;
112 	pthread_t ch[NTHREADS];
113 
114 	/* Initialize output */
115 	output_init();
116 
117 	/* Set the signal mask */
118 	ret = sigemptyset(&setusr);
119 
120 	if (ret != 0) {
121 		UNRESOLVED(ret, "Failed to empty signal set");
122 	}
123 
124 	ret = sigaddset(&setusr, SIGUSR1);
125 
126 	if (ret != 0) {
127 		UNRESOLVED(ret, "failed to add SIGUSR1 to signal set");
128 	}
129 
130 	ret = pthread_sigmask(SIG_BLOCK, &setusr, NULL);
131 
132 	if (ret != 0) {
133 		UNRESOLVED(ret, "Failed to block SIGUSR1");
134 	}
135 
136 	/* Create the children */
137 
138 	for (i = 0; i < NTHREADS; i++) {
139 		ret = pthread_create(&ch[i], NULL, threaded, NULL);
140 
141 		if (ret != 0) {
142 			UNRESOLVED(ret, "Failed to create a thread");
143 		}
144 	}
145 
146 	/* raise the signal */
147 	ret = kill(getpid(), SIGUSR1);
148 
149 	if (ret != 0) {
150 		UNRESOLVED(ret, "Failed to raise the signal");
151 	}
152 
153 	sleep(1);
154 
155 	if (n_awaken != 1) {
156 		output("%d threads were awaken\n", n_awaken);
157 		FAILED("Unexpected number of threads awaken");
158 	}
159 
160 	/* Wake other threads */
161 	for (; n_awaken < NTHREADS; sched_yield()) {
162 		ret = kill(getpid(), SIGUSR1);
163 
164 		if (ret != 0) {
165 			UNRESOLVED(ret, "Failed to raise the signal");
166 		}
167 	}
168 
169 	/* Wait for child thread termination */
170 	for (i = 0; i < NTHREADS; i++) {
171 		ret = pthread_join(ch[i], NULL);
172 
173 		if (ret != 0) {
174 			UNRESOLVED(ret, "Failed to join the thread");
175 		}
176 	}
177 
178 	/* Test passed */
179 #if VERBOSE > 0
180 
181 	output("Test passed\n");
182 
183 #endif
184 
185 	PASSED;
186 }
187