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 sample test aims to check the following assertion:
19 *
20 * pthread_mutex_init() can be used to re-initialize a destroyed mutex.
21
22 * The steps are:
23 * -> Initialize a mutex with a given attribute.
24 * -> Destroy the mutex
25 * -> Initialize again the mutex with another attribute.
26
27 */
28
29 /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
30 #define _POSIX_C_SOURCE 200112L
31
32 /* We need the XSI extention for the mutex attributes */
33#ifndef WITHOUT_XOPEN
34 #define _XOPEN_SOURCE	600
35#endif
36 /********************************************************************************************/
37/****************************** standard includes *****************************************/
38/********************************************************************************************/
39 #include <pthread.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44
45/********************************************************************************************/
46/******************************   Test framework   *****************************************/
47/********************************************************************************************/
48 #include "testfrmw.h"
49 #include "testfrmw.c"
50 /* This header is responsible for defining the following macros:
51  * UNRESOLVED(ret, descr);
52  *    where descr is a description of the error and ret is an int (error code for example)
53  * FAILED(descr);
54  *    where descr is a short text saying why the test has failed.
55  * PASSED();
56  *    No parameter.
57  *
58  * Both three macros shall terminate the calling process.
59  * The testcase shall not terminate in any other maneer.
60  *
61  * The other file defines the functions
62  * void output_init()
63  * void output(char * string, ...)
64  *
65  * Those may be used to output information.
66  */
67
68/********************************************************************************************/
69/********************************** Configuration ******************************************/
70/********************************************************************************************/
71#ifndef VERBOSE
72#define VERBOSE 1
73#endif
74
75/********************************************************************************************/
76/***********************************    Test case   *****************************************/
77/********************************************************************************************/
78#ifndef WITHOUT_XOPEN
79
80struct _scenar
81{
82	int m_type; /* Mutex type to use */
83	int m_pshared; /* 0: mutex is process-private (default) ~ !0: mutex is process-shared, if supported */
84	char * descr; /* Case description */
85}
86scenarii[] =
87{
88	 {PTHREAD_MUTEX_DEFAULT,    0, "Default mutex"}
89	,{PTHREAD_MUTEX_NORMAL,     0, "Normal mutex"}
90	,{PTHREAD_MUTEX_ERRORCHECK, 0, "Errorcheck mutex"}
91	,{PTHREAD_MUTEX_RECURSIVE,  0, "Recursive mutex"}
92
93	,{PTHREAD_MUTEX_DEFAULT,    1, "Pshared mutex"}
94	,{PTHREAD_MUTEX_NORMAL,     1, "Pshared Normal mutex"}
95	,{PTHREAD_MUTEX_ERRORCHECK, 1, "Pshared Errorcheck mutex"}
96	,{PTHREAD_MUTEX_RECURSIVE,  1, "Pshared Recursive mutex"}
97};
98#define NSCENAR (sizeof(scenarii)/sizeof(scenarii[0]))
99
100
101/* Main function */
102int main (int argc, char * argv[])
103{
104	int ret;
105	int i,j;
106	pthread_mutex_t mtx;
107	pthread_mutexattr_t ma[NSCENAR + 1];
108	pthread_mutexattr_t *pma[NSCENAR + 2];
109	long pshared;
110
111	/* Initialize output routine */
112	output_init();
113
114	/* System abilities */
115	pshared = sysconf(_SC_THREAD_PROCESS_SHARED);
116
117	/* Initialize the mutex attributes objects */
118	for (i=0; i<NSCENAR; i++)
119	{
120		ret = pthread_mutexattr_init(&ma[i]);
121		if (ret != 0)  {  UNRESOLVED(ret, "[parent] Unable to initialize the mutex attribute object");  }
122
123		/* Set the mutex type */
124		ret = pthread_mutexattr_settype(&ma[i], scenarii[i].m_type);
125		if (ret != 0)  {  UNRESOLVED(ret, "[parent] Unable to set mutex type");  }
126
127		/* Set the pshared attributes, if supported */
128		if ((pshared > 0) && (scenarii[i].m_pshared != 0))
129		{
130			ret = pthread_mutexattr_setpshared(&ma[i], PTHREAD_PROCESS_SHARED);
131			if (ret != 0)  {  UNRESOLVED(ret, "[parent] Unable to set the mutex process-shared");  }
132		}
133	}
134	 /* Default mutexattr object */
135	ret = pthread_mutexattr_init(&ma[i]);
136	if (ret != 0)  {  UNRESOLVED(ret, "[parent] Unable to initialize the mutex attribute object");  }
137
138	/* Initialize the pointer array */
139	for (i=0; i<NSCENAR+1; i++)
140		pma[i]=&ma[i];
141
142	/* NULL pointer */
143	pma[i] = NULL;
144
145	/* Ok, we can now proceed to the test */
146	#if VERBOSE > 0
147	output("Attributes are ready, proceed to the test\n");
148	#endif
149
150	for (i=0; i<NSCENAR + 2; i++)
151	{
152		for (j=0; j<NSCENAR + 2; j++)
153		{
154			#if VERBOSE > 1
155			char * nul="NULL";
156			char * def ="Default";
157			char * stri;
158			char * strj;
159			if (i<NSCENAR)
160				stri = scenarii[i].descr;
161			if (i==NSCENAR)
162				stri = def;
163			if (i==NSCENAR+1)
164				stri = nul;
165			if (j<NSCENAR)
166				strj = scenarii[j].descr;
167			if (j==NSCENAR)
168				strj = def;
169			if (j==NSCENAR+1)
170				strj = nul;
171
172			output("Init with: %s, \nreinit with: %s\n", stri, strj);
173			#endif
174
175			ret = pthread_mutex_init(&mtx, pma[i]);
176			if (ret != 0)  {   UNRESOLVED(ret, "Failed to init the mutex");  }
177
178			ret = pthread_mutex_destroy(&mtx);
179			if (ret != 0)  {  FAILED("Failed to destroy an initialized unlocked mutex");  }
180
181			ret = pthread_mutex_init(&mtx, pma[j]);
182			if (ret != 0)  {   FAILED("Failed to re-init the mutex");  }
183
184			ret = pthread_mutex_destroy(&mtx);
185			if (ret != 0)  {  FAILED("Failed to destroy an initialized unlocked mutex");  }
186
187		}
188	}
189
190	#if VERBOSE > 0
191	output("Test passed; destroying the test data\n");
192	#endif
193
194	for (i=0; i<NSCENAR + 1; i++)
195	{
196		ret = pthread_mutexattr_destroy(&ma[i]);
197		if (ret != 0)  {  UNRESOLVED(ret, "Failed to destroy a mutex attribute object");  }
198	}
199
200	PASSED;
201}
202
203
204#else /* WITHOUT_XOPEN */
205int main(int argc, char * argv[])
206{
207	output_init();
208	UNTESTED("This test requires XSI features");
209}
210#endif
211