aboutsummaryrefslogtreecommitdiff
path: root/testcases/open_posix_testsuite/conformance/interfaces/pthread_mutex_unlock/5-1.c
blob: 89e89d1a6be489a4bf9f0d1fb47da26580ae0fdf (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
/*
 * 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 type is PTHREAD_MUTEX_RECURSIVE,
 * and a thread attempts to unlock a mutex that it does not own,
 * an error is returned.

 * The steps are:
 *  -> Initialize and lock a recursive mutex
 *  -> create a child thread which tries to unlock this mutex. *
 */

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

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

#include <errno.h>		/* needed for EPERM test */

/********************************************************************************************/
/******************************   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   *****************************************/
/********************************************************************************************/

pthread_mutex_t m;

/** child thread function **/
void *threaded(void *arg)
{
	int ret;
	ret = pthread_mutex_unlock(&m);
	if (ret == 0) {
		UNRESOLVED(ret,
			   "Unlocking a not owned recursive mutex succeeded");
	}

	if (ret != EPERM)	/* This is a "may" assertion */
		output
		    ("Unlocking a not owned recursive mutex did not return EPERM\n");

	return NULL;
}

/** parent thread function **/
int main(void)
{
	int ret;
	pthread_mutexattr_t ma;
	pthread_t th;

	output_init();

#if VERBOSE >1
	output("Initialize the PTHREAD_MUTEX_RECURSIVE mutex\n");
#endif

	ret = pthread_mutexattr_init(&ma);
	if (ret != 0) {
		UNRESOLVED(ret, "Mutex attribute init failed");
	}

	ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE);
	if (ret != 0) {
		UNRESOLVED(ret, "Set type recursive failed");
	}

	ret = pthread_mutex_init(&m, &ma);
	if (ret != 0) {
		UNRESOLVED(ret, "Mutex init failed");
	}
#if VERBOSE >1
	output("Lock the mutex\n");
#endif

	ret = pthread_mutex_lock(&m);
	if (ret != 0) {
		UNRESOLVED(ret, "Mutex lock failed");
	}

	/* destroy the mutex attribute object */
	ret = pthread_mutexattr_destroy(&ma);
	if (ret != 0) {
		UNRESOLVED(ret, "Mutex attribute destroy failed");
	}
#if VERBOSE >1
	output("Create the thread\n");
#endif

	ret = pthread_create(&th, NULL, threaded, NULL);
	if (ret != 0) {
		UNRESOLVED(ret, "Thread creation failed");
	}

	/* Let the thread terminate */
	ret = pthread_join(th, NULL);
	if (ret != 0) {
		UNRESOLVED(ret, "Thread join failed");
	}
#if VERBOSE >1
	output("Joined the thread\n");
#endif

	/* We can clean everything and exit */
	ret = pthread_mutex_unlock(&m);
	if (ret != 0) {
		UNRESOLVED(ret, "Mutex unlock failed. Mutex got corrupted?");
	}

	PASSED;
}