Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
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 "nsThreadUtils.h" |
michael@0 | 7 | #include <stdio.h> |
michael@0 | 8 | #include <stdlib.h> |
michael@0 | 9 | #include "nspr.h" |
michael@0 | 10 | #include "nsCOMPtr.h" |
michael@0 | 11 | #include "nsIServiceManager.h" |
michael@0 | 12 | #include "nsXPCOM.h" |
michael@0 | 13 | |
michael@0 | 14 | class nsRunner : public nsIRunnable { |
michael@0 | 15 | public: |
michael@0 | 16 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 17 | |
michael@0 | 18 | NS_IMETHOD Run() { |
michael@0 | 19 | nsCOMPtr<nsIThread> thread; |
michael@0 | 20 | nsresult rv = NS_GetCurrentThread(getter_AddRefs(thread)); |
michael@0 | 21 | if (NS_FAILED(rv)) { |
michael@0 | 22 | printf("failed to get current thread\n"); |
michael@0 | 23 | return rv; |
michael@0 | 24 | } |
michael@0 | 25 | printf("running %d on thread %p\n", mNum, (void *)thread.get()); |
michael@0 | 26 | |
michael@0 | 27 | // if we don't do something slow, we'll never see the other |
michael@0 | 28 | // worker threads run |
michael@0 | 29 | PR_Sleep(PR_MillisecondsToInterval(100)); |
michael@0 | 30 | |
michael@0 | 31 | return rv; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | nsRunner(int num) : mNum(num) { |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | protected: |
michael@0 | 38 | int mNum; |
michael@0 | 39 | }; |
michael@0 | 40 | |
michael@0 | 41 | NS_IMPL_ISUPPORTS(nsRunner, nsIRunnable) |
michael@0 | 42 | |
michael@0 | 43 | nsresult |
michael@0 | 44 | TestThreads() |
michael@0 | 45 | { |
michael@0 | 46 | nsresult rv; |
michael@0 | 47 | |
michael@0 | 48 | nsCOMPtr<nsIRunnable> event = new nsRunner(0); |
michael@0 | 49 | if (!event) |
michael@0 | 50 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 51 | |
michael@0 | 52 | nsCOMPtr<nsIThread> runner; |
michael@0 | 53 | rv = NS_NewThread(getter_AddRefs(runner), event); |
michael@0 | 54 | if (NS_FAILED(rv)) { |
michael@0 | 55 | printf("failed to create thread\n"); |
michael@0 | 56 | return rv; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | nsCOMPtr<nsIThread> thread; |
michael@0 | 60 | rv = NS_GetCurrentThread(getter_AddRefs(thread)); |
michael@0 | 61 | if (NS_FAILED(rv)) { |
michael@0 | 62 | printf("failed to get current thread\n"); |
michael@0 | 63 | return rv; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | rv = runner->Shutdown(); // wait for the runner to die before quitting |
michael@0 | 67 | if (NS_FAILED(rv)) { |
michael@0 | 68 | printf("join failed\n"); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | PR_Sleep(PR_MillisecondsToInterval(100)); // hopefully the runner will quit here |
michael@0 | 72 | |
michael@0 | 73 | return NS_OK; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | class nsStressRunner : public nsIRunnable { |
michael@0 | 77 | public: |
michael@0 | 78 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 79 | |
michael@0 | 80 | NS_IMETHOD Run() { |
michael@0 | 81 | NS_ASSERTION(!mWasRun, "run twice!"); |
michael@0 | 82 | mWasRun = true; |
michael@0 | 83 | PR_Sleep(1); |
michael@0 | 84 | if (!PR_AtomicDecrement(&gNum)) { |
michael@0 | 85 | printf(" last thread was %d\n", mNum); |
michael@0 | 86 | } |
michael@0 | 87 | return NS_OK; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | nsStressRunner(int num) : mNum(num), mWasRun(false) { |
michael@0 | 91 | PR_AtomicIncrement(&gNum); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | static int32_t GetGlobalCount() {return gNum;} |
michael@0 | 95 | |
michael@0 | 96 | private: |
michael@0 | 97 | ~nsStressRunner() { |
michael@0 | 98 | NS_ASSERTION(mWasRun, "never run!"); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | protected: |
michael@0 | 102 | static int32_t gNum; |
michael@0 | 103 | int32_t mNum; |
michael@0 | 104 | bool mWasRun; |
michael@0 | 105 | }; |
michael@0 | 106 | |
michael@0 | 107 | int32_t nsStressRunner::gNum = 0; |
michael@0 | 108 | |
michael@0 | 109 | NS_IMPL_ISUPPORTS(nsStressRunner, nsIRunnable) |
michael@0 | 110 | |
michael@0 | 111 | static int Stress(int loops, int threads) |
michael@0 | 112 | { |
michael@0 | 113 | |
michael@0 | 114 | for (int i = 0; i < loops; i++) { |
michael@0 | 115 | printf("Loop %d of %d\n", i+1, loops); |
michael@0 | 116 | |
michael@0 | 117 | int k; |
michael@0 | 118 | nsIThread** array = new nsIThread*[threads]; |
michael@0 | 119 | NS_ASSERTION(array, "out of memory"); |
michael@0 | 120 | |
michael@0 | 121 | NS_ASSERTION(!nsStressRunner::GetGlobalCount(), "bad count of runnables"); |
michael@0 | 122 | |
michael@0 | 123 | for (k = 0; k < threads; k++) { |
michael@0 | 124 | nsCOMPtr<nsIThread> t; |
michael@0 | 125 | nsresult rv = NS_NewThread(getter_AddRefs(t), new nsStressRunner(k)); |
michael@0 | 126 | if (NS_FAILED(rv)) { |
michael@0 | 127 | NS_ERROR("can't create thread"); |
michael@0 | 128 | return -1; |
michael@0 | 129 | } |
michael@0 | 130 | NS_ADDREF(array[k] = t); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | for (k = threads-1; k >= 0; k--) { |
michael@0 | 134 | array[k]->Shutdown(); |
michael@0 | 135 | NS_RELEASE(array[k]); |
michael@0 | 136 | } |
michael@0 | 137 | delete [] array; |
michael@0 | 138 | } |
michael@0 | 139 | return 0; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | static void threadProc(void *arg) |
michael@0 | 143 | { |
michael@0 | 144 | // printf(" running thread %d\n", (int) arg); |
michael@0 | 145 | PR_Sleep(1); |
michael@0 | 146 | PR_ASSERT(PR_JOINABLE_THREAD == PR_GetThreadState(PR_GetCurrentThread())); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | static int StressNSPR(int loops, int threads) |
michael@0 | 150 | { |
michael@0 | 151 | |
michael@0 | 152 | for (int i = 0; i < loops; i++) { |
michael@0 | 153 | printf("Loop %d of %d\n", i+1, loops); |
michael@0 | 154 | |
michael@0 | 155 | int k; |
michael@0 | 156 | PRThread** array = new PRThread*[threads]; |
michael@0 | 157 | PR_ASSERT(array); |
michael@0 | 158 | |
michael@0 | 159 | for (k = 0; k < threads; k++) { |
michael@0 | 160 | array[k] = PR_CreateThread(PR_USER_THREAD, |
michael@0 | 161 | threadProc, (void*) k, |
michael@0 | 162 | PR_PRIORITY_NORMAL, |
michael@0 | 163 | PR_GLOBAL_THREAD, |
michael@0 | 164 | PR_JOINABLE_THREAD, |
michael@0 | 165 | 0); |
michael@0 | 166 | PR_ASSERT(array[k]); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | for (k = 0; k < threads; k++) { |
michael@0 | 170 | PR_ASSERT(PR_JOINABLE_THREAD == PR_GetThreadState(array[k])); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | for (k = threads-1; k >= 0; k--) { |
michael@0 | 174 | PR_JoinThread(array[k]); |
michael@0 | 175 | } |
michael@0 | 176 | delete [] array; |
michael@0 | 177 | } |
michael@0 | 178 | return 0; |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | |
michael@0 | 182 | int |
michael@0 | 183 | main(int argc, char** argv) |
michael@0 | 184 | { |
michael@0 | 185 | int retval = 0; |
michael@0 | 186 | nsresult rv; |
michael@0 | 187 | |
michael@0 | 188 | rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); |
michael@0 | 189 | if (NS_FAILED(rv)) return -1; |
michael@0 | 190 | |
michael@0 | 191 | if (argc > 1 && !strcmp(argv[1], "-stress")) { |
michael@0 | 192 | int loops; |
michael@0 | 193 | int threads; |
michael@0 | 194 | if (argc != 4 || *argv[2] != '-' || *argv[3] != '-' || |
michael@0 | 195 | !(loops = atoi(argv[2]+1)) || !(threads = atoi(argv[3]+1))) { |
michael@0 | 196 | printf("To use -stress you must pass loop count and thread count...\n" |
michael@0 | 197 | " TestThreads -stress -1000 -50\n"); |
michael@0 | 198 | } else { |
michael@0 | 199 | printf("Running stress test with %d loops of %d threads each\n", |
michael@0 | 200 | loops, threads); |
michael@0 | 201 | retval = Stress(loops, threads); |
michael@0 | 202 | } |
michael@0 | 203 | } else if (argc > 1 && !strcmp(argv[1], "-stress-nspr")) { |
michael@0 | 204 | int loops; |
michael@0 | 205 | int threads; |
michael@0 | 206 | if (argc != 4 || *argv[2] != '-' || *argv[3] != '-' || |
michael@0 | 207 | !(loops = atoi(argv[2]+1)) || !(threads = atoi(argv[3]+1))) { |
michael@0 | 208 | printf("To use -stress-nspr you must pass loop count and thread count...\n" |
michael@0 | 209 | " TestThreads -stress -1000 -50\n"); |
michael@0 | 210 | } else { |
michael@0 | 211 | printf("Running stress test with %d loops of %d threads each\n", |
michael@0 | 212 | loops, threads); |
michael@0 | 213 | retval = StressNSPR(loops, threads); |
michael@0 | 214 | } |
michael@0 | 215 | } else { |
michael@0 | 216 | rv = TestThreads(); |
michael@0 | 217 | if (NS_FAILED(rv)) return -1; |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | rv = NS_ShutdownXPCOM(nullptr); |
michael@0 | 221 | if (NS_FAILED(rv)) return -1; |
michael@0 | 222 | return retval; |
michael@0 | 223 | } |