1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 ** Description: Exercising the thread private data bailywick.
53 #include "private/pprio.h"
58 static PRUintn key
[128];
59 static PRIntn debug
= 0;
60 static PRBool failed
= PR_FALSE
;
61 static PRBool should
= PR_TRUE
;
62 static PRBool did
= PR_TRUE
;
63 static PRFileDesc
*fout
= NULL
;
65 static void PrintProgress(PRIntn line
)
67 failed
= failed
|| (should
&& !did
);
68 failed
= failed
|| (!should
&& did
);
73 "@ line %d destructor should%s have been called and was%s\n",
74 line
, ((should
) ? "" : " NOT"), ((did
) ? "" : " NOT"));
77 fout
, "@ line %d destructor should%s have been called and was%s\n",
78 line
, ((should
) ? "" : " NOT"), ((did
) ? "" : " NOT"));
83 static void MyAssert(const char *expr
, const char *file
, PRIntn line
)
86 (void)PR_fprintf(fout
, "'%s' in file: %s: %d\n", expr
, file
, line
);
89 #define MY_ASSERT(_expr) \
90 ((_expr)?((void)0):MyAssert(# _expr,__FILE__,__LINE__))
93 static void PR_CALLBACK
Destructor(void *data
)
95 MY_ASSERT(NULL
!= data
);
96 if (should
) did
= PR_TRUE
;
97 else failed
= PR_TRUE
;
99 * We don't actually free the storage since it's actually allocated
100 * on the stack. Normally, this would not be the case and this is
101 * the opportunity to free whatever.
106 static void PR_CALLBACK
Thread(void *null
)
111 char *key_string
[] = {
112 "Key #0", "Key #1", "Key #2", "Key #3",
113 "Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"};
115 did
= should
= PR_FALSE
;
116 for (keys
= 0; keys
< 8; ++keys
)
118 pd
= PR_GetThreadPrivate(key
[keys
]);
119 MY_ASSERT(NULL
== pd
);
121 PrintProgress(__LINE__
);
123 did
= should
= PR_FALSE
;
124 for (keys
= 0; keys
< 4; ++keys
)
126 rv
= PR_SetThreadPrivate(key
[keys
], key_string
[keys
]);
127 MY_ASSERT(PR_SUCCESS
== rv
);
129 PrintProgress(__LINE__
);
131 did
= should
= PR_FALSE
;
132 for (keys
= 4; keys
< 8; ++keys
)
134 rv
= PR_SetThreadPrivate(key
[keys
], key_string
[keys
]);
135 MY_ASSERT(PR_FAILURE
== rv
);
137 PrintProgress(__LINE__
);
139 did
= PR_FALSE
; should
= PR_TRUE
;
140 for (keys
= 0; keys
< 4; ++keys
)
142 rv
= PR_SetThreadPrivate(key
[keys
], key_string
[keys
]);
143 MY_ASSERT(PR_SUCCESS
== rv
);
145 PrintProgress(__LINE__
);
147 did
= PR_FALSE
; should
= PR_TRUE
;
148 for (keys
= 0; keys
< 4; ++keys
)
150 rv
= PR_SetThreadPrivate(key
[keys
], NULL
);
151 MY_ASSERT(PR_SUCCESS
== rv
);
153 PrintProgress(__LINE__
);
155 did
= should
= PR_FALSE
;
156 for (keys
= 0; keys
< 4; ++keys
)
158 rv
= PR_SetThreadPrivate(key
[keys
], NULL
);
159 MY_ASSERT(PR_SUCCESS
== rv
);
161 PrintProgress(__LINE__
);
163 did
= should
= PR_FALSE
;
164 for (keys
= 8; keys
< 127; ++keys
)
166 rv
= PR_SetThreadPrivate(key
[keys
], "EXTENSION");
167 MY_ASSERT(PR_SUCCESS
== rv
);
169 PrintProgress(__LINE__
);
171 did
= PR_FALSE
; should
= PR_TRUE
;
172 for (keys
= 8; keys
< 127; ++keys
)
174 rv
= PR_SetThreadPrivate(key
[keys
], NULL
);
175 MY_ASSERT(PR_SUCCESS
== rv
);
177 PrintProgress(__LINE__
);
179 did
= should
= PR_FALSE
;
180 for (keys
= 8; keys
< 127; ++keys
)
182 rv
= PR_SetThreadPrivate(key
[keys
], NULL
);
183 MY_ASSERT(PR_SUCCESS
== rv
);
186 /* put in keys and leave them there for thread exit */
187 did
= should
= PR_FALSE
;
188 for (keys
= 0; keys
< 4; ++keys
)
190 rv
= PR_SetThreadPrivate(key
[keys
], key_string
[keys
]);
191 MY_ASSERT(PR_SUCCESS
== rv
);
193 PrintProgress(__LINE__
);
194 did
= PR_FALSE
; should
= PR_TRUE
;
198 static PRIntn PR_CALLBACK
Tpd(PRIntn argc
, char **argv
)
204 char *key_string
[] = {
205 "Key #0", "Key #1", "Key #2", "Key #3",
206 "Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"};
210 did
= should
= PR_FALSE
;
211 for (keys
= 0; keys
< 4; ++keys
)
213 rv
= PR_NewThreadPrivateIndex(&key
[keys
], Destructor
);
214 MY_ASSERT(PR_SUCCESS
== rv
);
216 PrintProgress(__LINE__
);
218 did
= should
= PR_FALSE
;
219 for (keys
= 0; keys
< 8; ++keys
)
221 pd
= PR_GetThreadPrivate(key
[keys
]);
222 MY_ASSERT(NULL
== pd
);
224 PrintProgress(__LINE__
);
226 did
= should
= PR_FALSE
;
227 for (keys
= 0; keys
< 4; ++keys
)
229 rv
= PR_SetThreadPrivate(key
[keys
], key_string
[keys
]);
230 MY_ASSERT(PR_SUCCESS
== rv
);
232 PrintProgress(__LINE__
);
234 for (keys
= 4; keys
< 8; ++keys
)
235 key
[keys
] = 4096; /* set to invalid value */
236 did
= should
= PR_FALSE
;
237 for (keys
= 4; keys
< 8; ++keys
)
239 rv
= PR_SetThreadPrivate(key
[keys
], key_string
[keys
]);
240 MY_ASSERT(PR_FAILURE
== rv
);
242 PrintProgress(__LINE__
);
244 did
= PR_FALSE
; should
= PR_TRUE
;
245 for (keys
= 0; keys
< 4; ++keys
)
247 rv
= PR_SetThreadPrivate(key
[keys
], key_string
[keys
]);
248 MY_ASSERT(PR_SUCCESS
== rv
);
250 PrintProgress(__LINE__
);
252 did
= PR_FALSE
; should
= PR_TRUE
;
253 for (keys
= 0; keys
< 4; ++keys
)
255 rv
= PR_SetThreadPrivate(key
[keys
], NULL
);
256 MY_ASSERT(PR_SUCCESS
== rv
);
258 PrintProgress(__LINE__
);
260 did
= should
= PR_FALSE
;
261 for (keys
= 0; keys
< 4; ++keys
)
263 rv
= PR_SetThreadPrivate(key
[keys
], NULL
);
264 MY_ASSERT(PR_SUCCESS
== rv
);
266 PrintProgress(__LINE__
);
268 did
= should
= PR_FALSE
;
269 for (keys
= 8; keys
< 127; ++keys
)
271 rv
= PR_NewThreadPrivateIndex(&key
[keys
], Destructor
);
272 MY_ASSERT(PR_SUCCESS
== rv
);
273 rv
= PR_SetThreadPrivate(key
[keys
], "EXTENSION");
274 MY_ASSERT(PR_SUCCESS
== rv
);
276 PrintProgress(__LINE__
);
278 did
= PR_FALSE
; should
= PR_TRUE
;
279 for (keys
= 8; keys
< 127; ++keys
)
281 rv
= PR_SetThreadPrivate(key
[keys
], NULL
);
282 MY_ASSERT(PR_SUCCESS
== rv
);
284 PrintProgress(__LINE__
);
286 did
= should
= PR_FALSE
;
287 for (keys
= 8; keys
< 127; ++keys
)
289 rv
= PR_SetThreadPrivate(key
[keys
], NULL
);
290 MY_ASSERT(PR_SUCCESS
== rv
);
293 thread
= PR_CreateThread(
294 PR_USER_THREAD
, Thread
, NULL
, PR_PRIORITY_NORMAL
,
295 PR_LOCAL_THREAD
, PR_JOINABLE_THREAD
, 0);
297 (void)PR_JoinThread(thread
);
299 PrintProgress(__LINE__
);
303 "%s\n",((PR_TRUE
== failed
) ? "FAILED" : "PASSED"));
306 fout
, "%s\n",((PR_TRUE
== failed
) ? "FAILED" : "PASSED"));
313 PRIntn
main(PRIntn argc
, char *argv
[])
316 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "dl:r:");
317 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
319 if (PL_OPT_BAD
== os
) continue;
322 case 'd': /* debug mode */
329 PL_DestroyOptState(opt
);
331 return PR_Initialize(Tpd
, argc
, argv
, 0);