aboutsummaryrefslogtreecommitdiff
path: root/testcases/open_posix_testsuite/conformance/interfaces/pthread_join/4-1.c
blob: a827f5b77bf0aa0876235f258cfee0de525f65ed (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (c) 2005, Bull S.A..  All rights reserved.
* Created by: Sebastien Decugis
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This sample test aims to check the following assertion:
*
* If the thread calling pthread_join is canceled, the joined thread
* remains joinable.
*
* The steps are:
* -> create a thread blocked on a mutex.
* -> create another thread which tries and join the first thread.
* -> cancel the 2nd thread.
* -> unblock the semaphore then join the 1st thread
* The test fails if the main thread is unable to join the 1st thread.
*/


#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <semaphore.h>
#include <errno.h>

#include "../testfrmw/testfrmw.h"
#include "../testfrmw/testfrmw.c"
#ifndef VERBOSE
#define VERBOSE 1
#endif

#include "../testfrmw/threads_scenarii.c"

pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

/* 1st thread function */
void *threaded(void *arg)
{
	int ret = 0;

	/* Try and lock the mutex, then exit */
	ret = pthread_mutex_lock(&mtx);
	if (ret != 0)
		UNRESOLVED(ret, "Failed to lock mutex");

	ret = pthread_mutex_unlock(&mtx);
	if (ret != 0)
		UNRESOLVED(ret, "Failed to unlock mutex");

	return NULL;
}

/* Canceled thread */
void *joiner_func(void *arg)
{
	(void)pthread_join(*(pthread_t *) arg, NULL);

	FAILED("The joiner thread was not canceled");

	/* please the compiler */
	return NULL;
}

/* The main test function. */
int main(void)
{
	int ret = 0;
	pthread_t child;
	pthread_t joiner;

	/* Initialize output routine */
	output_init();

	/* Initialize thread attribute objects */
	scenar_init();

	for (sc = 0; sc < NSCENAR; sc++) {
		if (scenarii[sc].detached == 1)
			continue;

#if VERBOSE > 0
		output("-----\n");
		output("Starting test with scenario (%i): %s\n",
		       sc, scenarii[sc].descr);
#endif

		/* Lock the mutex */
		ret = pthread_mutex_lock(&mtx);
		if (ret != 0)
			UNRESOLVED(ret, "failed to lock the mutex");

		ret = pthread_create(&child, &scenarii[sc].ta, threaded, NULL);

		switch (scenarii[sc].result) {
			/* Operation was expected to succeed */
		case 0:

			if (ret != 0)
				UNRESOLVED(ret, "Failed to create this thread");

			break;
			/* Operation was expected to fail */
		case 1:

			if (ret == 0)
				UNRESOLVED(-1, "An error was expected "
					   "but the thread creation succeeded");

			break;
			/* We did not know the expected result */
		case 2:
		default:
#if VERBOSE > 0

			if (ret == 0)
				output("Thread has been created "
				       "successfully for this scenario\n");
			else
				output("Thread creation failed with the error: "
				       "%s\n", strerror(ret));

#endif

		}
		/* The new thread is running */
		if (ret == 0) {
			/* Now create the joiner thread */
			ret = pthread_create(&joiner, NULL,
					     joiner_func, &child);

			if (ret != 0)
				UNRESOLVED(ret, "Failed to create the "
					   "joiner thread");

			/* Let it enter pthread_join */
			sched_yield();

			/* Cancel the joiner thread */
			ret = pthread_cancel(joiner);
			if (ret != 0)
				UNRESOLVED(ret, "Failed to cancel the thread");

			/* Join the canceled thread */
			ret = pthread_join(joiner, NULL);
			if (ret != 0)
				UNRESOLVED(ret, "Failed to join the "
					   "canceled thread");

			/* Unblock the child thread */
			ret = pthread_mutex_unlock(&mtx);
			if (ret != 0)
				UNRESOLVED(ret, "Failed to unlock the mutex");

			/* Check the first thread is still joinable */
			ret = pthread_join(child, NULL);
			if (ret != 0) {
				output("Error returned: %d\n");
				FAILED("The thread is no more joinable");
			}

		} else {
			ret = pthread_mutex_unlock(&mtx);
			if (ret != 0)
				UNRESOLVED(ret, "Failed to unlock the mutex");
		}
	}

	scenar_fini();
#if VERBOSE > 0
	output("-----\n");
	output("All test data destroyed\n");
	output("Test PASSED\n");
#endif

	PASSED;
}