nsprpub/pr/tests/tpd.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial