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.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16
17 * This sample test aims to check the following assertion:
18 *
19 * pthread_exit() terminates the current thread and
20 * allows a call to pthread_join() on this thread retrieve
21 * the argument (if the thread is not detached).
22
23 * The steps are:
24 *
25 *  -> Create threads with different attributes (but all must be joinable)
26 *  -> inside the thread, a call to pthread_exit() is made with a certain value.
27 *  -> In the main thread, we join the thread and check the value is as expected.
28
29 * The test fails if the values are different.
30
31 */
32
33 /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
34#define _POSIX_C_SOURCE 200112L
35
36 /* Some routines are part of the XSI Extensions */
37#ifndef WITHOUT_XOPEN
38#define _XOPEN_SOURCE	600
39#endif
40
41/********************************************************************************************/
42/****************************** standard includes *****************************************/
43/********************************************************************************************/
44#include <pthread.h>
45#include <stdarg.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <unistd.h>
50
51#include <sched.h>
52#include <semaphore.h>
53#include <errno.h>
54#include <assert.h>
55/********************************************************************************************/
56/******************************   Test framework   *****************************************/
57/********************************************************************************************/
58#include "../testfrmw/testfrmw.h"
59#include "../testfrmw/testfrmw.c"
60 /* This header is responsible for defining the following macros:
61  * UNRESOLVED(ret, descr);
62  *    where descr is a description of the error and ret is an int (error code for example)
63  * FAILED(descr);
64  *    where descr is a short text saying why the test has failed.
65  * PASSED();
66  *    No parameter.
67  *
68  * Both three macros shall terminate the calling process.
69  * The testcase shall not terminate in any other maneer.
70  *
71  * The other file defines the functions
72  * void output_init()
73  * void output(char * string, ...)
74  *
75  * Those may be used to output information.
76  */
77
78/********************************************************************************************/
79/********************************** Configuration ******************************************/
80/********************************************************************************************/
81#ifndef VERBOSE
82#define VERBOSE 1
83#endif
84
85/********************************************************************************************/
86/***********************************    Test cases  *****************************************/
87/********************************************************************************************/
88
89#include "../testfrmw/threads_scenarii.c"
90
91/* This file will define the following objects:
92 * scenarii: array of struct __scenario type.
93 * NSCENAR : macro giving the total # of scenarii
94 * scenar_init(): function to call before use the scenarii array.
95 * scenar_fini(): function to call after end of use of the scenarii array.
96 */
97
98/********************************************************************************************/
99/***********************************    Real Test   *****************************************/
100/********************************************************************************************/
101
102void *threaded(void *arg)
103{
104	pthread_exit(NULL + 1);
105
106	FAILED("pthread_exit() did not terminate the thread");
107
108	return NULL;
109}
110
111int main(void)
112{
113	int ret = 0;
114	void *rval;
115	pthread_t child;
116	int i;
117
118	output_init();
119
120	scenar_init();
121
122	for (i = 0; i < NSCENAR; i++) {
123		if (scenarii[i].detached == 0) {
124#if VERBOSE > 0
125			output("-----\n");
126			output("Starting test with scenario (%i): %s\n", i,
127			       scenarii[i].descr);
128#endif
129
130			ret =
131			    pthread_create(&child, &scenarii[i].ta, threaded,
132					   NULL);
133			switch (scenarii[i].result) {
134			case 0:	/* Operation was expected to succeed */
135				if (ret != 0) {
136					UNRESOLVED(ret,
137						   "Failed to create this thread");
138				}
139				break;
140
141			case 1:	/* Operation was expected to fail */
142				if (ret == 0) {
143					UNRESOLVED(-1,
144						   "An error was expected but the thread creation succeeded");
145				}
146				break;
147
148			case 2:	/* We did not know the expected result */
149			default:
150#if VERBOSE > 0
151				if (ret == 0) {
152					output
153					    ("Thread has been created successfully for this scenario\n");
154				} else {
155					output
156					    ("Thread creation failed with the error: %s\n",
157					     strerror(ret));
158				}
159#endif
160			}
161			if (ret == 0) {	/* The new thread is running */
162				ret = pthread_join(child, &rval);
163				if (ret != 0) {
164					UNRESOLVED(ret,
165						   "Unable to join a thread");
166				}
167
168				if (rval != (NULL + 1)) {
169					FAILED
170					    ("pthread_join() did not retrieve the pthread_exit() param");
171				}
172			}
173		}
174	}
175
176	scenar_fini();
177#if VERBOSE > 0
178	output("-----\n");
179	output("All test data destroyed\n");
180	output("Test PASSED\n");
181#endif
182
183	PASSED;
184}
185