1/*
2 * Copyright (c) 2004, 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., 59
15 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
16
17
18 * This file is a stress test for the function pthread_exit.
19 *
20 * It aims to check that:
21 *  -> when the threads are joinable, pthread_join always retrieve the
22 *     correct value.
23 *  -> pthread_exit() frees all the resources used by the threads.
24 *
25 * The second assertion is implicitly checked by monitoring the system
26 * while the stress test is running.
27 *
28 */
29
30 /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
31 #define _POSIX_C_SOURCE 200112L
32
33 /* We need the XSI extention for some routines */
34#ifndef WITHOUT_XOPEN
35 #define _XOPEN_SOURCE	600
36#endif
37/********************************************************************************************/
38/****************************** standard includes *****************************************/
39/********************************************************************************************/
40 #include <pthread.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45
46 #include <errno.h>
47 #include <signal.h>
48 #include <semaphore.h>
49
50/********************************************************************************************/
51/******************************   Test framework   *****************************************/
52/********************************************************************************************/
53 #include "testfrmw.h"
54 #include "testfrmw.c"
55 /* This header is responsible for defining the following macros:
56  * UNRESOLVED(ret, descr);
57  *    where descr is a description of the error and ret is an int (error code for example)
58  * FAILED(descr);
59  *    where descr is a short text saying why the test has failed.
60  * PASSED();
61  *    No parameter.
62  *
63  * Both three macros shall terminate the calling process.
64  * The testcase shall not terminate in any other maneer.
65  *
66  * The other file defines the functions
67  * void output_init()
68  * void output(char * string, ...)
69  *
70  * Those may be used to output information.
71  */
72
73/********************************************************************************************/
74/********************************** Configuration ******************************************/
75/********************************************************************************************/
76#ifndef SCALABILITY_FACTOR
77#define SCALABILITY_FACTOR 1
78#endif
79#ifndef VERBOSE
80#define VERBOSE 1
81#endif
82
83#define FACTOR 5
84
85/* This testcase needs the XSI features */
86#ifndef WITHOUT_XOPEN
87/********************************************************************************************/
88/***********************************    Test case   *****************************************/
89/********************************************************************************************/
90
91#include "threads_scenarii.c"
92
93/* This file will define the following objects:
94 * scenarii: array of struct __scenario type.
95 * NSCENAR : macro giving the total # of scenarii
96 * scenar_init(): function to call before use the scenarii array.
97 * scenar_fini(): function to call after end of use of the scenarii array.
98 */
99
100/********************************************************************************************/
101/***********************************    Real Test   *****************************************/
102/********************************************************************************************/
103
104char do_it=1;
105long long iterations=0;
106
107/* Handler for user request to terminate */
108void sighdl(int sig)
109{
110	/* do_it = 0 */
111	do { do_it = 0; }
112	while (do_it);
113}
114
115
116/* Cleanup handler to make sure the thread is exiting */
117void cleanup(void * arg)
118{
119	int ret = 0;
120	sem_t * sem = (sem_t *) arg;
121
122	/* Signal we're done (especially in case of a detached thread) */
123	do { ret = sem_post(sem); }
124	while ((ret == -1) && (errno == EINTR));
125	if (ret == -1)  {  UNRESOLVED(errno, "Failed to wait for the semaphore");  }
126}
127
128
129/* Thread routine */
130void * threaded(void * arg)
131{
132	pthread_cleanup_push(cleanup, &scenarii[sc].sem);
133
134	pthread_exit(arg);
135	FAILED("the pthread_exit routine returned");
136
137	pthread_cleanup_pop(1);
138
139	return NULL; /* For the sake of compiler */
140}
141
142/* main routine */
143int main(int argc, char * argv[])
144{
145	int ret, i;
146	void * rval;
147	struct sigaction sa;
148
149	pthread_t threads[NSCENAR * SCALABILITY_FACTOR * FACTOR];
150	int rets[NSCENAR * SCALABILITY_FACTOR * FACTOR];
151
152 	/* Initialize output */
153	output_init();
154
155	/* Initialize scenarii table */
156	scenar_init();
157
158	/* Register the signal handler for SIGUSR1 */
159	sigemptyset (&sa.sa_mask);
160	sa.sa_flags = 0;
161	sa.sa_handler = sighdl;
162	if ((ret = sigaction (SIGUSR1, &sa, NULL)))
163	{ UNRESOLVED(ret, "Unable to register signal handler"); }
164	if ((ret = sigaction (SIGALRM, &sa, NULL)))
165	{ UNRESOLVED(ret, "Unable to register signal handler"); }
166	#if VERBOSE > 1
167	output("[parent] Signal handler registered\n");
168	#endif
169
170	while (do_it)
171	{
172		/* Create all the threads */
173		for (i=0; i<SCALABILITY_FACTOR * FACTOR; i++)
174		{
175			for (sc=0; sc<NSCENAR; sc++)
176			{
177				/* Skip the alternative stack threads */
178				if (scenarii[sc].altstack != 0)
179					continue;
180
181				rets[i*NSCENAR + sc] = pthread_create(&threads[i*NSCENAR + sc], &scenarii[sc].ta, threaded, &threads[i*NSCENAR + sc]);
182				switch (scenarii[sc].result)
183				{
184					case 0: /* Operation was expected to succeed */
185						if (rets[i*NSCENAR + sc] != 0)  {  UNRESOLVED(rets[i*NSCENAR + sc], "Failed to create this thread");  }
186						break;
187
188					case 1: /* Operation was expected to fail */
189						if (rets[i*NSCENAR + sc] == 0)  {  UNRESOLVED(-1, "An error was expected but the thread creation succeeded");  }
190						break;
191
192					case 2: /* We did not know the expected result */
193					default:
194						#if VERBOSE > 5
195						if (rets[i*NSCENAR + sc] == 0)
196							{ output("Thread has been created successfully for this scenario\n"); }
197						else
198							{ output("Thread creation failed with the error: %s\n", strerror(rets[i*NSCENAR + sc])); }
199						#endif
200						;
201				}
202				if (rets[i*NSCENAR + sc] == 0)
203				{
204					/* Just wait for the thread to terminate */
205					do { ret = sem_wait(&scenarii[sc].sem); }
206					while ((ret == -1) && (errno == EINTR));
207					if (ret == -1)  {  UNRESOLVED(errno, "Failed to wait for the semaphore");  }
208				}
209			}
210		}
211
212		/* Join all the joinable threads and check the value */
213		for (i=0; i<SCALABILITY_FACTOR * FACTOR; i++)
214		{
215			for (sc=0; sc<NSCENAR; sc++)
216			{
217				if ((scenarii[sc].altstack == 0) && (scenarii[sc].detached == 0) && (rets[i*NSCENAR + sc] == 0))
218				{
219					ret = pthread_join(threads[i*NSCENAR + sc], &rval);
220					if (ret != 0)  {  UNRESOLVED(ret, "Unable to join a thread");  }
221
222					if (rval !=  (void *)&threads[i*NSCENAR + sc])
223					{
224						output("arg: %p -- got %p -- NULL=%p\n", &threads[i*NSCENAR + sc], rval, NULL);
225						FAILED("The retrieved error value is corrupted");
226					}
227				}
228			}
229		}
230
231		iterations++;
232	}
233
234	/* Destroy scenarii attributes */
235	scenar_fini();
236
237	/* Test passed */
238	output("pthread_exit stress test PASSED -- %llu iterations\n",iterations);
239	PASSED;
240}
241
242
243#else /* WITHOUT_XOPEN */
244int main(int argc, char * argv[])
245{
246	output_init();
247	UNTESTED("This test requires XSI features");
248}
249#endif
250