nsprpub/pr/tests/threads.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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 #include "nspr.h"
michael@0 7 #include "prinrval.h"
michael@0 8 #include "plgetopt.h"
michael@0 9 #include "pprthred.h"
michael@0 10 #include <stdio.h>
michael@0 11 #include <stdlib.h>
michael@0 12 #include <string.h>
michael@0 13
michael@0 14 PRMonitor *mon;
michael@0 15 PRInt32 count, iterations, alive;
michael@0 16
michael@0 17 PRBool debug_mode = PR_FALSE, passed = PR_TRUE;
michael@0 18
michael@0 19 void
michael@0 20 PR_CALLBACK
michael@0 21 ReallyDumbThread(void *arg)
michael@0 22 {
michael@0 23 return;
michael@0 24 }
michael@0 25
michael@0 26 void
michael@0 27 PR_CALLBACK
michael@0 28 DumbThread(void *arg)
michael@0 29 {
michael@0 30 PRInt32 tmp = (PRInt32)arg;
michael@0 31 PRThreadScope scope = (PRThreadScope)tmp;
michael@0 32 PRThread *thr;
michael@0 33
michael@0 34 thr = PR_CreateThread(PR_USER_THREAD,
michael@0 35 ReallyDumbThread,
michael@0 36 NULL,
michael@0 37 PR_PRIORITY_NORMAL,
michael@0 38 scope,
michael@0 39 PR_JOINABLE_THREAD,
michael@0 40 0);
michael@0 41
michael@0 42 if (!thr) {
michael@0 43 if (debug_mode) {
michael@0 44 printf("Could not create really dumb thread (%d, %d)!\n",
michael@0 45 PR_GetError(), PR_GetOSError());
michael@0 46 }
michael@0 47 passed = PR_FALSE;
michael@0 48 } else {
michael@0 49 PR_JoinThread(thr);
michael@0 50 }
michael@0 51 PR_EnterMonitor(mon);
michael@0 52 alive--;
michael@0 53 PR_Notify(mon);
michael@0 54 PR_ExitMonitor(mon);
michael@0 55 }
michael@0 56
michael@0 57 static void CreateThreads(PRThreadScope scope1, PRThreadScope scope2)
michael@0 58 {
michael@0 59 PRThread *thr;
michael@0 60 int n;
michael@0 61
michael@0 62 alive = 0;
michael@0 63 mon = PR_NewMonitor();
michael@0 64
michael@0 65 alive = count;
michael@0 66 for (n=0; n<count; n++) {
michael@0 67 thr = PR_CreateThread(PR_USER_THREAD,
michael@0 68 DumbThread,
michael@0 69 (void *)scope2,
michael@0 70 PR_PRIORITY_NORMAL,
michael@0 71 scope1,
michael@0 72 PR_UNJOINABLE_THREAD,
michael@0 73 0);
michael@0 74 if (!thr) {
michael@0 75 if (debug_mode) {
michael@0 76 printf("Could not create dumb thread (%d, %d)!\n",
michael@0 77 PR_GetError(), PR_GetOSError());
michael@0 78 }
michael@0 79 passed = PR_FALSE;
michael@0 80 alive--;
michael@0 81 }
michael@0 82
michael@0 83 PR_Sleep(0);
michael@0 84 }
michael@0 85
michael@0 86 /* Wait for all threads to exit */
michael@0 87 PR_EnterMonitor(mon);
michael@0 88 while (alive) {
michael@0 89 PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
michael@0 90 }
michael@0 91
michael@0 92 PR_ExitMonitor(mon);
michael@0 93 PR_DestroyMonitor(mon);
michael@0 94 }
michael@0 95
michael@0 96 static void CreateThreadsUU(void)
michael@0 97 {
michael@0 98 CreateThreads(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
michael@0 99 }
michael@0 100
michael@0 101 static void CreateThreadsUK(void)
michael@0 102 {
michael@0 103 CreateThreads(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
michael@0 104 }
michael@0 105
michael@0 106 static void CreateThreadsKU(void)
michael@0 107 {
michael@0 108 CreateThreads(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
michael@0 109 }
michael@0 110
michael@0 111 static void CreateThreadsKK(void)
michael@0 112 {
michael@0 113 CreateThreads(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
michael@0 114 }
michael@0 115
michael@0 116 /************************************************************************/
michael@0 117
michael@0 118 static void Measure(void (*func)(void), const char *msg)
michael@0 119 {
michael@0 120 PRIntervalTime start, stop;
michael@0 121 double d;
michael@0 122
michael@0 123 start = PR_IntervalNow();
michael@0 124 (*func)();
michael@0 125 stop = PR_IntervalNow();
michael@0 126
michael@0 127 if (debug_mode)
michael@0 128 {
michael@0 129 d = (double)PR_IntervalToMicroseconds(stop - start);
michael@0 130 printf("%40s: %6.2f usec\n", msg, d / count);
michael@0 131 }
michael@0 132 }
michael@0 133
michael@0 134 int main(int argc, char **argv)
michael@0 135 {
michael@0 136 int index;
michael@0 137
michael@0 138 PR_STDIO_INIT();
michael@0 139 PR_Init(PR_USER_THREAD, PR_PRIORITY_HIGH, 0);
michael@0 140
michael@0 141 {
michael@0 142 PLOptStatus os;
michael@0 143 PLOptState *opt = PL_CreateOptState(argc, argv, "dc:i:");
michael@0 144 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
michael@0 145 {
michael@0 146 if (PL_OPT_BAD == os) continue;
michael@0 147 switch (opt->option)
michael@0 148 {
michael@0 149 case 'd': /* debug mode */
michael@0 150 debug_mode = PR_TRUE;
michael@0 151 break;
michael@0 152 case 'c': /* loop counter */
michael@0 153 count = atoi(opt->value);
michael@0 154 break;
michael@0 155 case 'i': /* loop counter */
michael@0 156 iterations = atoi(opt->value);
michael@0 157 break;
michael@0 158 default:
michael@0 159 break;
michael@0 160 }
michael@0 161 }
michael@0 162 PL_DestroyOptState(opt);
michael@0 163 }
michael@0 164
michael@0 165 if (0 == count) count = 50;
michael@0 166 if (0 == iterations) iterations = 10;
michael@0 167
michael@0 168 if (debug_mode)
michael@0 169 {
michael@0 170 printf("\
michael@0 171 ** Tests lots of thread creations. \n\
michael@0 172 ** Create %ld native threads %ld times. \n\
michael@0 173 ** Create %ld user threads %ld times \n", iterations,count,iterations,count);
michael@0 174 }
michael@0 175
michael@0 176 for (index=0; index<iterations; index++) {
michael@0 177 Measure(CreateThreadsUU, "Create user/user threads");
michael@0 178 Measure(CreateThreadsUK, "Create user/native threads");
michael@0 179 Measure(CreateThreadsKU, "Create native/user threads");
michael@0 180 Measure(CreateThreadsKK, "Create native/native threads");
michael@0 181 }
michael@0 182
michael@0 183 if (debug_mode) printf("\nNow switch to recycling threads \n\n");
michael@0 184 PR_SetThreadRecycleMode(1);
michael@0 185
michael@0 186 for (index=0; index<iterations; index++) {
michael@0 187 Measure(CreateThreadsUU, "Create user/user threads");
michael@0 188 Measure(CreateThreadsUK, "Create user/native threads");
michael@0 189 Measure(CreateThreadsKU, "Create native/user threads");
michael@0 190 Measure(CreateThreadsKK, "Create native/native threads");
michael@0 191 }
michael@0 192
michael@0 193
michael@0 194 printf("%s\n", ((passed) ? "PASS" : "FAIL"));
michael@0 195
michael@0 196 PR_Cleanup();
michael@0 197
michael@0 198 if (passed) {
michael@0 199 return 0;
michael@0 200 } else {
michael@0 201 return 1;
michael@0 202 }
michael@0 203 }

mercurial