michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsThreadUtils.h" michael@0: #include michael@0: #include michael@0: #include "nspr.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsXPCOM.h" michael@0: michael@0: class nsRunner : public nsIRunnable { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: michael@0: NS_IMETHOD Run() { michael@0: nsCOMPtr thread; michael@0: nsresult rv = NS_GetCurrentThread(getter_AddRefs(thread)); michael@0: if (NS_FAILED(rv)) { michael@0: printf("failed to get current thread\n"); michael@0: return rv; michael@0: } michael@0: printf("running %d on thread %p\n", mNum, (void *)thread.get()); michael@0: michael@0: // if we don't do something slow, we'll never see the other michael@0: // worker threads run michael@0: PR_Sleep(PR_MillisecondsToInterval(100)); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: nsRunner(int num) : mNum(num) { michael@0: } michael@0: michael@0: protected: michael@0: int mNum; michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(nsRunner, nsIRunnable) michael@0: michael@0: nsresult michael@0: TestThreads() michael@0: { michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr event = new nsRunner(0); michael@0: if (!event) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: nsCOMPtr runner; michael@0: rv = NS_NewThread(getter_AddRefs(runner), event); michael@0: if (NS_FAILED(rv)) { michael@0: printf("failed to create thread\n"); michael@0: return rv; michael@0: } michael@0: michael@0: nsCOMPtr thread; michael@0: rv = NS_GetCurrentThread(getter_AddRefs(thread)); michael@0: if (NS_FAILED(rv)) { michael@0: printf("failed to get current thread\n"); michael@0: return rv; michael@0: } michael@0: michael@0: rv = runner->Shutdown(); // wait for the runner to die before quitting michael@0: if (NS_FAILED(rv)) { michael@0: printf("join failed\n"); michael@0: } michael@0: michael@0: PR_Sleep(PR_MillisecondsToInterval(100)); // hopefully the runner will quit here michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: class nsStressRunner : public nsIRunnable { michael@0: public: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: michael@0: NS_IMETHOD Run() { michael@0: NS_ASSERTION(!mWasRun, "run twice!"); michael@0: mWasRun = true; michael@0: PR_Sleep(1); michael@0: if (!PR_AtomicDecrement(&gNum)) { michael@0: printf(" last thread was %d\n", mNum); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsStressRunner(int num) : mNum(num), mWasRun(false) { michael@0: PR_AtomicIncrement(&gNum); michael@0: } michael@0: michael@0: static int32_t GetGlobalCount() {return gNum;} michael@0: michael@0: private: michael@0: ~nsStressRunner() { michael@0: NS_ASSERTION(mWasRun, "never run!"); michael@0: } michael@0: michael@0: protected: michael@0: static int32_t gNum; michael@0: int32_t mNum; michael@0: bool mWasRun; michael@0: }; michael@0: michael@0: int32_t nsStressRunner::gNum = 0; michael@0: michael@0: NS_IMPL_ISUPPORTS(nsStressRunner, nsIRunnable) michael@0: michael@0: static int Stress(int loops, int threads) michael@0: { michael@0: michael@0: for (int i = 0; i < loops; i++) { michael@0: printf("Loop %d of %d\n", i+1, loops); michael@0: michael@0: int k; michael@0: nsIThread** array = new nsIThread*[threads]; michael@0: NS_ASSERTION(array, "out of memory"); michael@0: michael@0: NS_ASSERTION(!nsStressRunner::GetGlobalCount(), "bad count of runnables"); michael@0: michael@0: for (k = 0; k < threads; k++) { michael@0: nsCOMPtr t; michael@0: nsresult rv = NS_NewThread(getter_AddRefs(t), new nsStressRunner(k)); michael@0: if (NS_FAILED(rv)) { michael@0: NS_ERROR("can't create thread"); michael@0: return -1; michael@0: } michael@0: NS_ADDREF(array[k] = t); michael@0: } michael@0: michael@0: for (k = threads-1; k >= 0; k--) { michael@0: array[k]->Shutdown(); michael@0: NS_RELEASE(array[k]); michael@0: } michael@0: delete [] array; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static void threadProc(void *arg) michael@0: { michael@0: // printf(" running thread %d\n", (int) arg); michael@0: PR_Sleep(1); michael@0: PR_ASSERT(PR_JOINABLE_THREAD == PR_GetThreadState(PR_GetCurrentThread())); michael@0: } michael@0: michael@0: static int StressNSPR(int loops, int threads) michael@0: { michael@0: michael@0: for (int i = 0; i < loops; i++) { michael@0: printf("Loop %d of %d\n", i+1, loops); michael@0: michael@0: int k; michael@0: PRThread** array = new PRThread*[threads]; michael@0: PR_ASSERT(array); michael@0: michael@0: for (k = 0; k < threads; k++) { michael@0: array[k] = PR_CreateThread(PR_USER_THREAD, michael@0: threadProc, (void*) k, michael@0: PR_PRIORITY_NORMAL, michael@0: PR_GLOBAL_THREAD, michael@0: PR_JOINABLE_THREAD, michael@0: 0); michael@0: PR_ASSERT(array[k]); michael@0: } michael@0: michael@0: for (k = 0; k < threads; k++) { michael@0: PR_ASSERT(PR_JOINABLE_THREAD == PR_GetThreadState(array[k])); michael@0: } michael@0: michael@0: for (k = threads-1; k >= 0; k--) { michael@0: PR_JoinThread(array[k]); michael@0: } michael@0: delete [] array; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: int michael@0: main(int argc, char** argv) michael@0: { michael@0: int retval = 0; michael@0: nsresult rv; michael@0: michael@0: rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) return -1; michael@0: michael@0: if (argc > 1 && !strcmp(argv[1], "-stress")) { michael@0: int loops; michael@0: int threads; michael@0: if (argc != 4 || *argv[2] != '-' || *argv[3] != '-' || michael@0: !(loops = atoi(argv[2]+1)) || !(threads = atoi(argv[3]+1))) { michael@0: printf("To use -stress you must pass loop count and thread count...\n" michael@0: " TestThreads -stress -1000 -50\n"); michael@0: } else { michael@0: printf("Running stress test with %d loops of %d threads each\n", michael@0: loops, threads); michael@0: retval = Stress(loops, threads); michael@0: } michael@0: } else if (argc > 1 && !strcmp(argv[1], "-stress-nspr")) { michael@0: int loops; michael@0: int threads; michael@0: if (argc != 4 || *argv[2] != '-' || *argv[3] != '-' || michael@0: !(loops = atoi(argv[2]+1)) || !(threads = atoi(argv[3]+1))) { michael@0: printf("To use -stress-nspr you must pass loop count and thread count...\n" michael@0: " TestThreads -stress -1000 -50\n"); michael@0: } else { michael@0: printf("Running stress test with %d loops of %d threads each\n", michael@0: loops, threads); michael@0: retval = StressNSPR(loops, threads); michael@0: } michael@0: } else { michael@0: rv = TestThreads(); michael@0: if (NS_FAILED(rv)) return -1; michael@0: } michael@0: michael@0: rv = NS_ShutdownXPCOM(nullptr); michael@0: if (NS_FAILED(rv)) return -1; michael@0: return retval; michael@0: }