VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/tpd.c@ 1

Last change on this file since 1 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
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
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
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.
21 *
22 * Contributor(s):
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/*
39** File: tpd.c
40** Description: Exercising the thread private data bailywick.
41*/
42
43#include "prmem.h"
44#include "prinit.h"
45#include "prlog.h"
46#include "prprf.h"
47#include "prthread.h"
48#include "prtypes.h"
49
50#if defined(XP_MAC)
51#include "pprio.h"
52#else
53#include "private/pprio.h"
54#endif
55
56#include "plgetopt.h"
57
58static PRUintn key[128];
59static PRIntn debug = 0;
60static PRBool failed = PR_FALSE;
61static PRBool should = PR_TRUE;
62static PRBool did = PR_TRUE;
63static PRFileDesc *fout = NULL;
64
65static void PrintProgress(PRIntn line)
66{
67 failed = failed || (should && !did);
68 failed = failed || (!should && did);
69 if (debug > 0)
70 {
71#if defined(WIN16)
72 printf(
73 "@ line %d destructor should%s have been called and was%s\n",
74 line, ((should) ? "" : " NOT"), ((did) ? "" : " NOT"));
75#else
76 PR_fprintf(
77 fout, "@ line %d destructor should%s have been called and was%s\n",
78 line, ((should) ? "" : " NOT"), ((did) ? "" : " NOT"));
79#endif
80 }
81} /* PrintProgress */
82
83static void MyAssert(const char *expr, const char *file, PRIntn line)
84{
85 if (debug > 0)
86 (void)PR_fprintf(fout, "'%s' in file: %s: %d\n", expr, file, line);
87} /* MyAssert */
88
89#define MY_ASSERT(_expr) \
90 ((_expr)?((void)0):MyAssert(# _expr,__FILE__,__LINE__))
91
92
93static void PR_CALLBACK Destructor(void *data)
94{
95 MY_ASSERT(NULL != data);
96 if (should) did = PR_TRUE;
97 else failed = PR_TRUE;
98 /*
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.
102 PR_Free(data);
103 */
104} /* Destructor */
105
106static void PR_CALLBACK Thread(void *null)
107{
108 void *pd;
109 PRStatus rv;
110 PRUintn keys;
111 char *key_string[] = {
112 "Key #0", "Key #1", "Key #2", "Key #3",
113 "Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"};
114
115 did = should = PR_FALSE;
116 for (keys = 0; keys < 8; ++keys)
117 {
118 pd = PR_GetThreadPrivate(key[keys]);
119 MY_ASSERT(NULL == pd);
120 }
121 PrintProgress(__LINE__);
122
123 did = should = PR_FALSE;
124 for (keys = 0; keys < 4; ++keys)
125 {
126 rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
127 MY_ASSERT(PR_SUCCESS == rv);
128 }
129 PrintProgress(__LINE__);
130
131 did = should = PR_FALSE;
132 for (keys = 4; keys < 8; ++keys)
133 {
134 rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
135 MY_ASSERT(PR_FAILURE == rv);
136 }
137 PrintProgress(__LINE__);
138
139 did = PR_FALSE; should = PR_TRUE;
140 for (keys = 0; keys < 4; ++keys)
141 {
142 rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
143 MY_ASSERT(PR_SUCCESS == rv);
144 }
145 PrintProgress(__LINE__);
146
147 did = PR_FALSE; should = PR_TRUE;
148 for (keys = 0; keys < 4; ++keys)
149 {
150 rv = PR_SetThreadPrivate(key[keys], NULL);
151 MY_ASSERT(PR_SUCCESS == rv);
152 }
153 PrintProgress(__LINE__);
154
155 did = should = PR_FALSE;
156 for (keys = 0; keys < 4; ++keys)
157 {
158 rv = PR_SetThreadPrivate(key[keys], NULL);
159 MY_ASSERT(PR_SUCCESS == rv);
160 }
161 PrintProgress(__LINE__);
162
163 did = should = PR_FALSE;
164 for (keys = 8; keys < 127; ++keys)
165 {
166 rv = PR_SetThreadPrivate(key[keys], "EXTENSION");
167 MY_ASSERT(PR_SUCCESS == rv);
168 }
169 PrintProgress(__LINE__);
170
171 did = PR_FALSE; should = PR_TRUE;
172 for (keys = 8; keys < 127; ++keys)
173 {
174 rv = PR_SetThreadPrivate(key[keys], NULL);
175 MY_ASSERT(PR_SUCCESS == rv);
176 }
177 PrintProgress(__LINE__);
178
179 did = should = PR_FALSE;
180 for (keys = 8; keys < 127; ++keys)
181 {
182 rv = PR_SetThreadPrivate(key[keys], NULL);
183 MY_ASSERT(PR_SUCCESS == rv);
184 }
185
186 /* put in keys and leave them there for thread exit */
187 did = should = PR_FALSE;
188 for (keys = 0; keys < 4; ++keys)
189 {
190 rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
191 MY_ASSERT(PR_SUCCESS == rv);
192 }
193 PrintProgress(__LINE__);
194 did = PR_FALSE; should = PR_TRUE;
195
196} /* Thread */
197
198static PRIntn PR_CALLBACK Tpd(PRIntn argc, char **argv)
199{
200 void *pd;
201 PRStatus rv;
202 PRUintn keys;
203 PRThread *thread;
204 char *key_string[] = {
205 "Key #0", "Key #1", "Key #2", "Key #3",
206 "Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"};
207
208 fout = PR_STDOUT;
209
210 did = should = PR_FALSE;
211 for (keys = 0; keys < 4; ++keys)
212 {
213 rv = PR_NewThreadPrivateIndex(&key[keys], Destructor);
214 MY_ASSERT(PR_SUCCESS == rv);
215 }
216 PrintProgress(__LINE__);
217
218 did = should = PR_FALSE;
219 for (keys = 0; keys < 8; ++keys)
220 {
221 pd = PR_GetThreadPrivate(key[keys]);
222 MY_ASSERT(NULL == pd);
223 }
224 PrintProgress(__LINE__);
225
226 did = should = PR_FALSE;
227 for (keys = 0; keys < 4; ++keys)
228 {
229 rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
230 MY_ASSERT(PR_SUCCESS == rv);
231 }
232 PrintProgress(__LINE__);
233
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)
238 {
239 rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
240 MY_ASSERT(PR_FAILURE == rv);
241 }
242 PrintProgress(__LINE__);
243
244 did = PR_FALSE; should = PR_TRUE;
245 for (keys = 0; keys < 4; ++keys)
246 {
247 rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
248 MY_ASSERT(PR_SUCCESS == rv);
249 }
250 PrintProgress(__LINE__);
251
252 did = PR_FALSE; should = PR_TRUE;
253 for (keys = 0; keys < 4; ++keys)
254 {
255 rv = PR_SetThreadPrivate(key[keys], NULL);
256 MY_ASSERT(PR_SUCCESS == rv);
257 }
258 PrintProgress(__LINE__);
259
260 did = should = PR_FALSE;
261 for (keys = 0; keys < 4; ++keys)
262 {
263 rv = PR_SetThreadPrivate(key[keys], NULL);
264 MY_ASSERT(PR_SUCCESS == rv);
265 }
266 PrintProgress(__LINE__);
267
268 did = should = PR_FALSE;
269 for (keys = 8; keys < 127; ++keys)
270 {
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);
275 }
276 PrintProgress(__LINE__);
277
278 did = PR_FALSE; should = PR_TRUE;
279 for (keys = 8; keys < 127; ++keys)
280 {
281 rv = PR_SetThreadPrivate(key[keys], NULL);
282 MY_ASSERT(PR_SUCCESS == rv);
283 }
284 PrintProgress(__LINE__);
285
286 did = should = PR_FALSE;
287 for (keys = 8; keys < 127; ++keys)
288 {
289 rv = PR_SetThreadPrivate(key[keys], NULL);
290 MY_ASSERT(PR_SUCCESS == rv);
291 }
292
293 thread = PR_CreateThread(
294 PR_USER_THREAD, Thread, NULL, PR_PRIORITY_NORMAL,
295 PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
296
297 (void)PR_JoinThread(thread);
298
299 PrintProgress(__LINE__);
300
301#if defined(WIN16)
302 printf(
303 "%s\n",((PR_TRUE == failed) ? "FAILED" : "PASSED"));
304#else
305 (void)PR_fprintf(
306 fout, "%s\n",((PR_TRUE == failed) ? "FAILED" : "PASSED"));
307#endif
308
309 return 0;
310
311} /* Tpd */
312
313PRIntn main(PRIntn argc, char *argv[])
314{
315 PLOptStatus os;
316 PLOptState *opt = PL_CreateOptState(argc, argv, "dl:r:");
317 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
318 {
319 if (PL_OPT_BAD == os) continue;
320 switch (opt->option)
321 {
322 case 'd': /* debug mode */
323 debug = PR_TRUE;
324 break;
325 default:
326 break;
327 }
328 }
329 PL_DestroyOptState(opt);
330 PR_STDIO_INIT();
331 return PR_Initialize(Tpd, argc, argv, 0);
332} /* main */
333
334/* tpd.c */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette