|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 ** File: tpd.c |
|
8 ** Description: Exercising the thread private data bailywick. |
|
9 */ |
|
10 |
|
11 #include "prmem.h" |
|
12 #include "prinit.h" |
|
13 #include "prlog.h" |
|
14 #include "prprf.h" |
|
15 #include "prthread.h" |
|
16 #include "prtypes.h" |
|
17 |
|
18 #include "private/pprio.h" |
|
19 |
|
20 #include "plgetopt.h" |
|
21 |
|
22 static PRUintn key[128]; |
|
23 static PRIntn debug = 0; |
|
24 static PRBool failed = PR_FALSE; |
|
25 static PRBool should = PR_TRUE; |
|
26 static PRBool did = PR_TRUE; |
|
27 static PRFileDesc *fout = NULL; |
|
28 |
|
29 static void PrintProgress(PRIntn line) |
|
30 { |
|
31 failed = failed || (should && !did); |
|
32 failed = failed || (!should && did); |
|
33 if (debug > 0) |
|
34 { |
|
35 #if defined(WIN16) |
|
36 printf( |
|
37 "@ line %d destructor should%s have been called and was%s\n", |
|
38 line, ((should) ? "" : " NOT"), ((did) ? "" : " NOT")); |
|
39 #else |
|
40 PR_fprintf( |
|
41 fout, "@ line %d destructor should%s have been called and was%s\n", |
|
42 line, ((should) ? "" : " NOT"), ((did) ? "" : " NOT")); |
|
43 #endif |
|
44 } |
|
45 } /* PrintProgress */ |
|
46 |
|
47 static void MyAssert(const char *expr, const char *file, PRIntn line) |
|
48 { |
|
49 if (debug > 0) |
|
50 (void)PR_fprintf(fout, "'%s' in file: %s: %d\n", expr, file, line); |
|
51 } /* MyAssert */ |
|
52 |
|
53 #define MY_ASSERT(_expr) \ |
|
54 ((_expr)?((void)0):MyAssert(# _expr,__FILE__,__LINE__)) |
|
55 |
|
56 |
|
57 static void PR_CALLBACK Destructor(void *data) |
|
58 { |
|
59 MY_ASSERT(NULL != data); |
|
60 if (should) did = PR_TRUE; |
|
61 else failed = PR_TRUE; |
|
62 /* |
|
63 * We don't actually free the storage since it's actually allocated |
|
64 * on the stack. Normally, this would not be the case and this is |
|
65 * the opportunity to free whatever. |
|
66 PR_Free(data); |
|
67 */ |
|
68 } /* Destructor */ |
|
69 |
|
70 static void PR_CALLBACK Thread(void *null) |
|
71 { |
|
72 void *pd; |
|
73 PRStatus rv; |
|
74 PRUintn keys; |
|
75 char *key_string[] = { |
|
76 "Key #0", "Key #1", "Key #2", "Key #3", |
|
77 "Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"}; |
|
78 |
|
79 did = should = PR_FALSE; |
|
80 for (keys = 0; keys < 8; ++keys) |
|
81 { |
|
82 pd = PR_GetThreadPrivate(key[keys]); |
|
83 MY_ASSERT(NULL == pd); |
|
84 } |
|
85 PrintProgress(__LINE__); |
|
86 |
|
87 did = should = PR_FALSE; |
|
88 for (keys = 0; keys < 4; ++keys) |
|
89 { |
|
90 rv = PR_SetThreadPrivate(key[keys], key_string[keys]); |
|
91 MY_ASSERT(PR_SUCCESS == rv); |
|
92 } |
|
93 PrintProgress(__LINE__); |
|
94 |
|
95 did = should = PR_FALSE; |
|
96 for (keys = 4; keys < 8; ++keys) |
|
97 { |
|
98 rv = PR_SetThreadPrivate(key[keys], key_string[keys]); |
|
99 MY_ASSERT(PR_FAILURE == rv); |
|
100 } |
|
101 PrintProgress(__LINE__); |
|
102 |
|
103 did = PR_FALSE; should = PR_TRUE; |
|
104 for (keys = 0; keys < 4; ++keys) |
|
105 { |
|
106 rv = PR_SetThreadPrivate(key[keys], key_string[keys]); |
|
107 MY_ASSERT(PR_SUCCESS == rv); |
|
108 } |
|
109 PrintProgress(__LINE__); |
|
110 |
|
111 did = PR_FALSE; should = PR_TRUE; |
|
112 for (keys = 0; keys < 4; ++keys) |
|
113 { |
|
114 rv = PR_SetThreadPrivate(key[keys], NULL); |
|
115 MY_ASSERT(PR_SUCCESS == rv); |
|
116 } |
|
117 PrintProgress(__LINE__); |
|
118 |
|
119 did = should = PR_FALSE; |
|
120 for (keys = 0; keys < 4; ++keys) |
|
121 { |
|
122 rv = PR_SetThreadPrivate(key[keys], NULL); |
|
123 MY_ASSERT(PR_SUCCESS == rv); |
|
124 } |
|
125 PrintProgress(__LINE__); |
|
126 |
|
127 did = should = PR_FALSE; |
|
128 for (keys = 8; keys < 127; ++keys) |
|
129 { |
|
130 rv = PR_SetThreadPrivate(key[keys], "EXTENSION"); |
|
131 MY_ASSERT(PR_SUCCESS == rv); |
|
132 } |
|
133 PrintProgress(__LINE__); |
|
134 |
|
135 did = PR_FALSE; should = PR_TRUE; |
|
136 for (keys = 8; keys < 127; ++keys) |
|
137 { |
|
138 rv = PR_SetThreadPrivate(key[keys], NULL); |
|
139 MY_ASSERT(PR_SUCCESS == rv); |
|
140 } |
|
141 PrintProgress(__LINE__); |
|
142 |
|
143 did = should = PR_FALSE; |
|
144 for (keys = 8; keys < 127; ++keys) |
|
145 { |
|
146 rv = PR_SetThreadPrivate(key[keys], NULL); |
|
147 MY_ASSERT(PR_SUCCESS == rv); |
|
148 } |
|
149 |
|
150 /* put in keys and leave them there for thread exit */ |
|
151 did = should = PR_FALSE; |
|
152 for (keys = 0; keys < 4; ++keys) |
|
153 { |
|
154 rv = PR_SetThreadPrivate(key[keys], key_string[keys]); |
|
155 MY_ASSERT(PR_SUCCESS == rv); |
|
156 } |
|
157 PrintProgress(__LINE__); |
|
158 did = PR_FALSE; should = PR_TRUE; |
|
159 |
|
160 } /* Thread */ |
|
161 |
|
162 static PRIntn PR_CALLBACK Tpd(PRIntn argc, char **argv) |
|
163 { |
|
164 void *pd; |
|
165 PRStatus rv; |
|
166 PRUintn keys; |
|
167 PRThread *thread; |
|
168 char *key_string[] = { |
|
169 "Key #0", "Key #1", "Key #2", "Key #3", |
|
170 "Bogus #5", "Bogus #6", "Bogus #7", "Bogus #8"}; |
|
171 |
|
172 fout = PR_STDOUT; |
|
173 |
|
174 did = should = PR_FALSE; |
|
175 for (keys = 0; keys < 4; ++keys) |
|
176 { |
|
177 rv = PR_NewThreadPrivateIndex(&key[keys], Destructor); |
|
178 MY_ASSERT(PR_SUCCESS == rv); |
|
179 } |
|
180 PrintProgress(__LINE__); |
|
181 |
|
182 did = should = PR_FALSE; |
|
183 for (keys = 0; keys < 8; ++keys) |
|
184 { |
|
185 pd = PR_GetThreadPrivate(key[keys]); |
|
186 MY_ASSERT(NULL == pd); |
|
187 } |
|
188 PrintProgress(__LINE__); |
|
189 |
|
190 did = should = PR_FALSE; |
|
191 for (keys = 0; keys < 4; ++keys) |
|
192 { |
|
193 rv = PR_SetThreadPrivate(key[keys], key_string[keys]); |
|
194 MY_ASSERT(PR_SUCCESS == rv); |
|
195 } |
|
196 PrintProgress(__LINE__); |
|
197 |
|
198 for (keys = 4; keys < 8; ++keys) |
|
199 key[keys] = 4096; /* set to invalid value */ |
|
200 did = should = PR_FALSE; |
|
201 for (keys = 4; keys < 8; ++keys) |
|
202 { |
|
203 rv = PR_SetThreadPrivate(key[keys], key_string[keys]); |
|
204 MY_ASSERT(PR_FAILURE == rv); |
|
205 } |
|
206 PrintProgress(__LINE__); |
|
207 |
|
208 did = PR_FALSE; should = PR_TRUE; |
|
209 for (keys = 0; keys < 4; ++keys) |
|
210 { |
|
211 rv = PR_SetThreadPrivate(key[keys], key_string[keys]); |
|
212 MY_ASSERT(PR_SUCCESS == rv); |
|
213 } |
|
214 PrintProgress(__LINE__); |
|
215 |
|
216 did = PR_FALSE; should = PR_TRUE; |
|
217 for (keys = 0; keys < 4; ++keys) |
|
218 { |
|
219 rv = PR_SetThreadPrivate(key[keys], NULL); |
|
220 MY_ASSERT(PR_SUCCESS == rv); |
|
221 } |
|
222 PrintProgress(__LINE__); |
|
223 |
|
224 did = should = PR_FALSE; |
|
225 for (keys = 0; keys < 4; ++keys) |
|
226 { |
|
227 rv = PR_SetThreadPrivate(key[keys], NULL); |
|
228 MY_ASSERT(PR_SUCCESS == rv); |
|
229 } |
|
230 PrintProgress(__LINE__); |
|
231 |
|
232 did = should = PR_FALSE; |
|
233 for (keys = 8; keys < 127; ++keys) |
|
234 { |
|
235 rv = PR_NewThreadPrivateIndex(&key[keys], Destructor); |
|
236 MY_ASSERT(PR_SUCCESS == rv); |
|
237 rv = PR_SetThreadPrivate(key[keys], "EXTENSION"); |
|
238 MY_ASSERT(PR_SUCCESS == rv); |
|
239 } |
|
240 PrintProgress(__LINE__); |
|
241 |
|
242 did = PR_FALSE; should = PR_TRUE; |
|
243 for (keys = 8; keys < 127; ++keys) |
|
244 { |
|
245 rv = PR_SetThreadPrivate(key[keys], NULL); |
|
246 MY_ASSERT(PR_SUCCESS == rv); |
|
247 } |
|
248 PrintProgress(__LINE__); |
|
249 |
|
250 did = should = PR_FALSE; |
|
251 for (keys = 8; keys < 127; ++keys) |
|
252 { |
|
253 rv = PR_SetThreadPrivate(key[keys], NULL); |
|
254 MY_ASSERT(PR_SUCCESS == rv); |
|
255 } |
|
256 |
|
257 thread = PR_CreateThread( |
|
258 PR_USER_THREAD, Thread, NULL, PR_PRIORITY_NORMAL, |
|
259 PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0); |
|
260 |
|
261 (void)PR_JoinThread(thread); |
|
262 |
|
263 PrintProgress(__LINE__); |
|
264 |
|
265 #if defined(WIN16) |
|
266 printf( |
|
267 "%s\n",((PR_TRUE == failed) ? "FAILED" : "PASSED")); |
|
268 #else |
|
269 (void)PR_fprintf( |
|
270 fout, "%s\n",((PR_TRUE == failed) ? "FAILED" : "PASSED")); |
|
271 #endif |
|
272 |
|
273 return 0; |
|
274 |
|
275 } /* Tpd */ |
|
276 |
|
277 int main(int argc, char **argv) |
|
278 { |
|
279 PLOptStatus os; |
|
280 PLOptState *opt = PL_CreateOptState(argc, argv, "dl:r:"); |
|
281 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
|
282 { |
|
283 if (PL_OPT_BAD == os) continue; |
|
284 switch (opt->option) |
|
285 { |
|
286 case 'd': /* debug mode */ |
|
287 debug = PR_TRUE; |
|
288 break; |
|
289 default: |
|
290 break; |
|
291 } |
|
292 } |
|
293 PL_DestroyOptState(opt); |
|
294 PR_STDIO_INIT(); |
|
295 return PR_Initialize(Tpd, argc, argv, 0); |
|
296 } /* main */ |
|
297 |
|
298 /* tpd.c */ |