aboutsummaryrefslogtreecommitdiff
path: root/testcases/open_posix_testsuite/conformance/interfaces/pthread_mutex_init/1-2.c
blob: ff6177ed07b36fd4ab3d15ba26a239faa7f5f41d (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * Copyright (c) 2004, 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 mutex attribute pointer passed to pthread_mutex_init is NULL,
 * the effects on the mutex are the same as if a default mutex attribute object had been passed.
 *
 * The steps are:
 *  * create two mutexes. One is initialized with NULL attribute, the other with a default attribute object.
 *  * Compare the following features between the two mutexes:
 *      -> Can it cause / detect a deadlock? (attempt to lock a mutex the thread already owns).
 *            If detected, do both mutexes cause the same error code?
 *      -> Is an error returned when unlocking the mutex in unlocked state?
 *            When unlocking the mutex owned by another thread?
 *
 * The test will pass if the results of each feature are the same for the two mutexes
 * (making no assumption on what is the default behavior).
 * The test will be unresolved if any initialization fails.
 * The test will fail if a feature differs between the two mutex objects.
 */

 /*
  * - adam.li@intel.com 2004-05-09
  *   Add to PTS. Please refer to http://nptl.bullopensource.org/phpBB/
  *   for general information
  */

/********************************************************************************************/
/****************************** standard includes *****************************************/
/********************************************************************************************/
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>

/********************************************************************************************/
/******************************   Test framework   *****************************************/
/********************************************************************************************/
#include "../testfrmw/testfrmw.h"
#include "../testfrmw/testfrmw.c"
 /* This header is responsible for defining the following macros:
  * UNRESOLVED(ret, descr);
  *    where descr is a description of the error and ret is an int (error code for example)
  * FAILED(descr);
  *    where descr is a short text saying why the test has failed.
  * PASSED();
  *    No parameter.
  *
  * Both three macros shall terminate the calling process.
  * The testcase shall not terminate in any other maneer.
  *
  * The other file defines the functions
  * void output_init()
  * void output(char * string, ...)
  *
  * Those may be used to output information.
  */

/********************************************************************************************/
/********************************** Configuration ******************************************/
/********************************************************************************************/
#ifndef VERBOSE
#define VERBOSE 1
#endif

/********************************************************************************************/
/***********************************    Test case   *****************************************/
/********************************************************************************************/

/**** global variables ****/
pthread_mutex_t *p_mtx;
int retval = 0;
int returned = 0;
int canceled = 0;
sem_t semA, semB;

/***** Cancelation handlers  *****/
void cleanup_deadlk(void *arg LTP_ATTRIBUTE_UNUSED)
{
	canceled = 1;
	pthread_mutex_unlock(p_mtx);
}

/***** Threads functions *****/
void *deadlk_issue(void *arg LTP_ATTRIBUTE_UNUSED)
{
	int ret, tmp;

	if ((ret = pthread_mutex_lock(p_mtx))) {
		UNRESOLVED(ret, "First mutex lock in deadlk_issue");
	}
	pthread_cleanup_push(cleanup_deadlk, NULL);
	if ((ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &tmp))) {
		UNRESOLVED(ret, "Set cancel type in deadlk_issue");
	}
	if ((ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &tmp))) {
		UNRESOLVED(ret, "Set cancel state in deadlk_issue");
	}
#if VERBOSE >1
	output("Thread releases the semaphore...\n");
#endif
	if ((ret = sem_post(&semA))) {
		UNRESOLVED(errno, "Sem_post in deadlk_issue");
	}

	returned = 0;
	retval = pthread_mutex_lock(p_mtx);
	returned = 1;

	if ((ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &tmp))) {
		UNRESOLVED(ret, "Set cancel state in deadlk_issue");
	}
	pthread_cleanup_pop(0);
	return NULL;
}

void *unlock_issue(void *arg LTP_ATTRIBUTE_UNUSED)
{
	int ret;

#if VERBOSE >1
	output("Locking in child...\n");
#endif
	if ((ret = pthread_mutex_lock(p_mtx))) {
		UNRESOLVED(ret, "First mutex lock in unlock_issue");
	}

	if ((ret = sem_post(&semA))) {
		UNRESOLVED(errno, "Sem_post in unlock_issue");
	}

	if ((ret = sem_wait(&semB))) {
		UNRESOLVED(errno, "Sem_wait in unlock_issue");
	}

	if (retval != 0) {	/* parent thread failed to unlock the mutex) */
#if VERBOSE >1
		output("Unlocking in child...\n");
#endif
		if ((ret = pthread_mutex_unlock(p_mtx))) {
			FAILED
			    ("Mutex unlock returned an error but mutex is unlocked.");
		}
	}

	return NULL;
}

/***** main program *****/
int main(void)
{
	pthread_mutex_t mtx_null, mtx_def;
	pthread_mutexattr_t mattr;
	pthread_t thr;

	pthread_mutex_t *tab_mutex[2] = { &mtx_null, &mtx_def };
	int tab_res[2][3] = { {0, 0, 0}, {0, 0, 0} };

	int ret;
	void *th_ret;

	int i;

	output_init();

#if VERBOSE >1
	output("Test starting...\n");
#endif

	/* We first initialize the two mutexes. */
	if ((ret = pthread_mutex_init(&mtx_null, NULL))) {
		UNRESOLVED(ret, "NULL mutex init");
	}

	if ((ret = pthread_mutexattr_init(&mattr))) {
		UNRESOLVED(ret, "Mutex attribute init");
	}
	if ((ret = pthread_mutex_init(&mtx_def, &mattr))) {
		UNRESOLVED(ret, "Default attribute mutex init");
	}

	if ((ret = pthread_mutexattr_destroy(&mattr))) {
		UNRESOLVED(ret, "Mutex attribute destroy");
	}

	if ((ret = sem_init(&semA, 0, 0))) {
		UNRESOLVED(errno, "Sem A init");
	}
	if ((ret = sem_init(&semB, 0, 0))) {
		UNRESOLVED(errno, "Sem B init");
	}
#if VERBOSE >1
	output("Data initialized...\n");
#endif

	/* OK let's go for the first part of the test : abnormals unlocking */

	/* We first check if unlocking an unlocked mutex returns an error. */
	retval = pthread_mutex_unlock(tab_mutex[0]);
	ret = pthread_mutex_unlock(tab_mutex[1]);
#if VERBOSE >0
	output
	    ("Results for unlock issue #1:\n mutex 1 unlocking returned %i\n mutex 2 unlocking returned %i\n",
	     retval, ret);
#endif
	if (ret != retval) {
		FAILED("Unlocking an unlocked mutex behaves differently.");
	}

	/* Now we focus on unlocking a mutex lock by another thread */
	for (i = 0; i < 2; i++) {
		p_mtx = tab_mutex[i];
		tab_res[i][0] = 0;
		tab_res[i][1] = 0;
		tab_res[i][2] = 0;

#if VERBOSE >1
		output("Creating thread (unlock)...\n");
#endif

		if ((ret = pthread_create(&thr, NULL, unlock_issue, NULL))) {
			UNRESOLVED(ret, "Unlock issue thread create");
		}

		if ((ret = sem_wait(&semA))) {
			UNRESOLVED(errno,
				   "Sem A wait failed for unlock issue.");
		}
#if VERBOSE >1
		output("Unlocking in parent...\n");
#endif
		retval = pthread_mutex_unlock(p_mtx);

		if ((ret = sem_post(&semB))) {
			UNRESOLVED(errno,
				   "Sem B post failed for unlock issue.");
		}

		if ((ret = pthread_join(thr, &th_ret))) {
			UNRESOLVED(ret, "Join thread");
		}
#if VERBOSE >1
		output("Thread joined successfully...\n");
#endif

		tab_res[i][0] = retval;
	}
#if VERBOSE >0
	output
	    ("Results for unlock issue #2:\n mutex 1 returned %i\n mutex 2 returned %i\n",
	     tab_res[0][0], tab_res[1][0]);
#endif

	if (tab_res[0][0] != tab_res[1][0]) {
		FAILED("Unlocking an unowned mutex behaves differently.");
	}

	/* We now are going to test the deadlock issue
	 */

	/* We start with testing the NULL mutex features */
	for (i = 0; i < 2; i++) {
		p_mtx = tab_mutex[i];
		tab_res[i][0] = 0;
		tab_res[i][1] = 0;
		tab_res[i][2] = 0;

#if VERBOSE >1
		output("Creating thread (deadlk)...\n");
#endif

		if ((ret = pthread_create(&thr, NULL, deadlk_issue, NULL))) {
			UNRESOLVED(ret, "Deadlk_issue thread create");
		}

		/* Now we are waiting the thread is ready to relock the mutex. */
		if ((ret = sem_wait(&semA))) {
			UNRESOLVED(errno, "Sem wait");
		}

		/* To ensure thread runs until second lock, we yield here */
		sched_yield();

		/* OK, now we cancel the thread */
		canceled = 0;
#if VERBOSE >1
		output("Cancel thread...\n");
#endif
		if (returned == 0)
			if ((ret = pthread_cancel(thr))) {
				UNRESOLVED(ret, "Cancel thread (deadlk_issue)");
			}
#if VERBOSE >1
		output("Thread canceled...\n");
#endif

		if ((ret = pthread_join(thr, &th_ret))) {
			UNRESOLVED(ret, "Join thread");
		}
#if VERBOSE >1
		output("Thread joined successfully...\n");
#endif

		tab_res[i][2] = retval;
		tab_res[i][1] = returned;
		tab_res[i][0] = canceled;
	}

	/* Now we parse the results */
#if VERBOSE >0
	output
	    ("Results for deadlock issue:\n mutex 1 \t%s\t%s%i\n mutex 2 \t%s\t%s%i\n",
	     tab_res[0][0] ? "deadlock" : "no deadlock",
	     tab_res[0][1] ? "returned " : "did not return ", tab_res[0][2],
	     tab_res[1][0] ? "deadlock" : "no deadlock",
	     tab_res[1][1] ? "returned " : "did not return ", tab_res[1][2]);
#endif

	if (tab_res[0][0] != tab_res[1][0]) {
		FAILED("One mutex deadlocks, not the other");
	}

	if (tab_res[0][1] != tab_res[1][1]) {
		UNRESOLVED(tab_res[0][1], "Abnormal situation!");
	}

	if ((tab_res[0][1] == 1) && (tab_res[0][2] != tab_res[1][2])) {
		FAILED("The locks returned different error codes.");
	}

	PASSED;
}