1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3
* The contents of this file are subject to the Mozilla Public
4
* License Version 1.1 (the "License"); you may not use this file
5
* except in compliance with the License. You may obtain a copy of
6
* the License at http://www.mozilla.org/MPL/
8
* Software distributed under the License is distributed on an "AS
9
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
* implied. See the License for the specific language governing
11
* rights and limitations under the License.
13
* The Original Code is the Netscape Portable Runtime (NSPR).
15
* The Initial Developer of the Original Code is Netscape
16
* Communications Corporation. Portions created by Netscape are
17
* Copyright (C) 1998-2000 Netscape Communications Corporation. All
22
* Alternatively, the contents of this file may be used under the
23
* terms of the GNU General Public License Version 2 or later (the
24
* "GPL"), in which case the provisions of the GPL are applicable
25
* instead of those above. If you wish to allow use of your
26
* version of this file only under the terms of the GPL and not to
27
* allow others to use your version of this file under the MPL,
28
* indicate your decision by deleting the provisions above and
29
* replace them with the notice and other provisions required by
30
* the GPL. If you do not delete the provisions above, a recipient
31
* may use your version of this file under either the MPL or the
37
** Description: Exercising the thread private data bailywick.
50
#include "private/pprio.h"
55
static PRUintn key[128];
56
static PRIntn debug = 0;
57
static PRBool failed = PR_FALSE;
58
static PRBool should = PR_TRUE;
59
static PRBool did = PR_TRUE;
60
static PRFileDesc *fout = NULL;
62
static void PrintProgress(PRIntn line)
64
failed = failed || (should && !did);
65
failed = failed || (!should && did);
70
"@ line %d destructor should%s have been called and was%s\n",
71
line, ((should) ? "" : " NOT"), ((did) ? "" : " NOT"));
74
fout, "@ line %d destructor should%s have been called and was%s\n",
75
line, ((should) ? "" : " NOT"), ((did) ? "" : " NOT"));
80
static void MyAssert(const char *expr, const char *file, PRIntn line)
83
(void)PR_fprintf(fout, "'%s' in file: %s: %d\n", expr, file, line);
86
#define MY_ASSERT(_expr) \
87
((_expr)?((void)0):MyAssert(# _expr,__FILE__,__LINE__))
90
static void PR_CALLBACK Destructor(void *data)
92
MY_ASSERT(NULL != data);
93
if (should) did = PR_TRUE;
94
else failed = PR_TRUE;
96
* We don't actually free the storage since it's actually allocated
97
* on the stack. Normally, this would not be the case and this is
98
* the opportunity to free whatever.
103
static void PR_CALLBACK Thread(void *null)
108
char *key_string[] = {
109
"Key #0", "Key #1", "Key #2", "Key #3",
110
"Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"};
112
did = should = PR_FALSE;
113
for (keys = 0; keys < 8; ++keys)
115
pd = PR_GetThreadPrivate(key[keys]);
116
MY_ASSERT(NULL == pd);
118
PrintProgress(__LINE__);
120
did = should = PR_FALSE;
121
for (keys = 0; keys < 4; ++keys)
123
rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
124
MY_ASSERT(PR_SUCCESS == rv);
126
PrintProgress(__LINE__);
128
did = should = PR_FALSE;
129
for (keys = 4; keys < 8; ++keys)
131
rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
132
MY_ASSERT(PR_FAILURE == rv);
134
PrintProgress(__LINE__);
136
did = PR_FALSE; should = PR_TRUE;
137
for (keys = 0; keys < 4; ++keys)
139
rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
140
MY_ASSERT(PR_SUCCESS == rv);
142
PrintProgress(__LINE__);
144
did = PR_FALSE; should = PR_TRUE;
145
for (keys = 0; keys < 4; ++keys)
147
rv = PR_SetThreadPrivate(key[keys], NULL);
148
MY_ASSERT(PR_SUCCESS == rv);
150
PrintProgress(__LINE__);
152
did = should = PR_FALSE;
153
for (keys = 0; keys < 4; ++keys)
155
rv = PR_SetThreadPrivate(key[keys], NULL);
156
MY_ASSERT(PR_SUCCESS == rv);
158
PrintProgress(__LINE__);
160
did = should = PR_FALSE;
161
for (keys = 8; keys < 127; ++keys)
163
rv = PR_SetThreadPrivate(key[keys], "EXTENSION");
164
MY_ASSERT(PR_SUCCESS == rv);
166
PrintProgress(__LINE__);
168
did = PR_FALSE; should = PR_TRUE;
169
for (keys = 8; keys < 127; ++keys)
171
rv = PR_SetThreadPrivate(key[keys], NULL);
172
MY_ASSERT(PR_SUCCESS == rv);
174
PrintProgress(__LINE__);
176
did = should = PR_FALSE;
177
for (keys = 8; keys < 127; ++keys)
179
rv = PR_SetThreadPrivate(key[keys], NULL);
180
MY_ASSERT(PR_SUCCESS == rv);
183
/* put in keys and leave them there for thread exit */
184
did = should = PR_FALSE;
185
for (keys = 0; keys < 4; ++keys)
187
rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
188
MY_ASSERT(PR_SUCCESS == rv);
190
PrintProgress(__LINE__);
191
did = PR_FALSE; should = PR_TRUE;
195
static PRIntn PR_CALLBACK Tpd(PRIntn argc, char **argv)
201
char *key_string[] = {
202
"Key #0", "Key #1", "Key #2", "Key #3",
203
"Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"};
207
did = should = PR_FALSE;
208
for (keys = 0; keys < 4; ++keys)
210
rv = PR_NewThreadPrivateIndex(&key[keys], Destructor);
211
MY_ASSERT(PR_SUCCESS == rv);
213
PrintProgress(__LINE__);
215
did = should = PR_FALSE;
216
for (keys = 0; keys < 8; ++keys)
218
pd = PR_GetThreadPrivate(key[keys]);
219
MY_ASSERT(NULL == pd);
221
PrintProgress(__LINE__);
223
did = should = PR_FALSE;
224
for (keys = 0; keys < 4; ++keys)
226
rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
227
MY_ASSERT(PR_SUCCESS == rv);
229
PrintProgress(__LINE__);
231
for (keys = 4; keys < 8; ++keys)
232
key[keys] = 4096; /* set to invalid value */
233
did = should = PR_FALSE;
234
for (keys = 4; keys < 8; ++keys)
236
rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
237
MY_ASSERT(PR_FAILURE == rv);
239
PrintProgress(__LINE__);
241
did = PR_FALSE; should = PR_TRUE;
242
for (keys = 0; keys < 4; ++keys)
244
rv = PR_SetThreadPrivate(key[keys], key_string[keys]);
245
MY_ASSERT(PR_SUCCESS == rv);
247
PrintProgress(__LINE__);
249
did = PR_FALSE; should = PR_TRUE;
250
for (keys = 0; keys < 4; ++keys)
252
rv = PR_SetThreadPrivate(key[keys], NULL);
253
MY_ASSERT(PR_SUCCESS == rv);
255
PrintProgress(__LINE__);
257
did = should = PR_FALSE;
258
for (keys = 0; keys < 4; ++keys)
260
rv = PR_SetThreadPrivate(key[keys], NULL);
261
MY_ASSERT(PR_SUCCESS == rv);
263
PrintProgress(__LINE__);
265
did = should = PR_FALSE;
266
for (keys = 8; keys < 127; ++keys)
268
rv = PR_NewThreadPrivateIndex(&key[keys], Destructor);
269
MY_ASSERT(PR_SUCCESS == rv);
270
rv = PR_SetThreadPrivate(key[keys], "EXTENSION");
271
MY_ASSERT(PR_SUCCESS == rv);
273
PrintProgress(__LINE__);
275
did = PR_FALSE; should = PR_TRUE;
276
for (keys = 8; keys < 127; ++keys)
278
rv = PR_SetThreadPrivate(key[keys], NULL);
279
MY_ASSERT(PR_SUCCESS == rv);
281
PrintProgress(__LINE__);
283
did = should = PR_FALSE;
284
for (keys = 8; keys < 127; ++keys)
286
rv = PR_SetThreadPrivate(key[keys], NULL);
287
MY_ASSERT(PR_SUCCESS == rv);
290
thread = PR_CreateThread(
291
PR_USER_THREAD, Thread, NULL, PR_PRIORITY_NORMAL,
292
PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
294
(void)PR_JoinThread(thread);
296
PrintProgress(__LINE__);
300
"%s\n",((PR_TRUE == failed) ? "FAILED" : "PASSED"));
303
fout, "%s\n",((PR_TRUE == failed) ? "FAILED" : "PASSED"));
310
PRIntn main(PRIntn argc, char *argv[])
313
PLOptState *opt = PL_CreateOptState(argc, argv, "dl:r:");
314
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
316
if (PL_OPT_BAD == os) continue;
319
case 'd': /* debug mode */
326
PL_DestroyOptState(opt);
328
return PR_Initialize(Tpd, argc, argv, 0);