xpcom/tests/TestThreadPoolListener.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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: 8; 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 "TestHarness.h"
michael@0 7
michael@0 8 #include "nsIThread.h"
michael@0 9 #include "nsIThreadPool.h"
michael@0 10
michael@0 11 #include "nsThreadUtils.h"
michael@0 12 #include "nsXPCOMCIDInternal.h"
michael@0 13 #include "pratom.h"
michael@0 14 #include "prinrval.h"
michael@0 15 #include "prmon.h"
michael@0 16 #include "prthread.h"
michael@0 17 #include "mozilla/Attributes.h"
michael@0 18
michael@0 19 #include "mozilla/ReentrantMonitor.h"
michael@0 20 using namespace mozilla;
michael@0 21
michael@0 22 #define NUMBER_OF_THREADS 4
michael@0 23
michael@0 24 // One hour... because test boxes can be slow!
michael@0 25 #define IDLE_THREAD_TIMEOUT 3600000
michael@0 26
michael@0 27 static nsIThread** gCreatedThreadList = nullptr;
michael@0 28 static nsIThread** gShutDownThreadList = nullptr;
michael@0 29
michael@0 30 static ReentrantMonitor* gReentrantMonitor = nullptr;
michael@0 31
michael@0 32 static bool gAllRunnablesPosted = false;
michael@0 33 static bool gAllThreadsCreated = false;
michael@0 34 static bool gAllThreadsShutDown = false;
michael@0 35
michael@0 36 #ifdef DEBUG
michael@0 37 #define TEST_ASSERTION(_test, _msg) \
michael@0 38 NS_ASSERTION(_test, _msg);
michael@0 39 #else
michael@0 40 #define TEST_ASSERTION(_test, _msg) \
michael@0 41 PR_BEGIN_MACRO \
michael@0 42 if (!(_test)) { \
michael@0 43 NS_DebugBreak(NS_DEBUG_ABORT, _msg, #_test, __FILE__, __LINE__); \
michael@0 44 } \
michael@0 45 PR_END_MACRO
michael@0 46 #endif
michael@0 47
michael@0 48 class Listener MOZ_FINAL : public nsIThreadPoolListener
michael@0 49 {
michael@0 50 public:
michael@0 51 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 52 NS_DECL_NSITHREADPOOLLISTENER
michael@0 53 };
michael@0 54
michael@0 55 NS_IMPL_ISUPPORTS(Listener, nsIThreadPoolListener)
michael@0 56
michael@0 57 NS_IMETHODIMP
michael@0 58 Listener::OnThreadCreated()
michael@0 59 {
michael@0 60 nsCOMPtr<nsIThread> current(do_GetCurrentThread());
michael@0 61 TEST_ASSERTION(current, "Couldn't get current thread!");
michael@0 62
michael@0 63 ReentrantMonitorAutoEnter mon(*gReentrantMonitor);
michael@0 64
michael@0 65 while (!gAllRunnablesPosted) {
michael@0 66 mon.Wait();
michael@0 67 }
michael@0 68
michael@0 69 for (uint32_t i = 0; i < NUMBER_OF_THREADS; i++) {
michael@0 70 nsIThread* thread = gCreatedThreadList[i];
michael@0 71 TEST_ASSERTION(thread != current, "Saw the same thread twice!");
michael@0 72
michael@0 73 if (!thread) {
michael@0 74 gCreatedThreadList[i] = current;
michael@0 75 if (i == (NUMBER_OF_THREADS - 1)) {
michael@0 76 gAllThreadsCreated = true;
michael@0 77 mon.NotifyAll();
michael@0 78 }
michael@0 79 return NS_OK;
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83 TEST_ASSERTION(false, "Too many threads!");
michael@0 84 return NS_ERROR_FAILURE;
michael@0 85 }
michael@0 86
michael@0 87 NS_IMETHODIMP
michael@0 88 Listener::OnThreadShuttingDown()
michael@0 89 {
michael@0 90 nsCOMPtr<nsIThread> current(do_GetCurrentThread());
michael@0 91 TEST_ASSERTION(current, "Couldn't get current thread!");
michael@0 92
michael@0 93 ReentrantMonitorAutoEnter mon(*gReentrantMonitor);
michael@0 94
michael@0 95 for (uint32_t i = 0; i < NUMBER_OF_THREADS; i++) {
michael@0 96 nsIThread* thread = gShutDownThreadList[i];
michael@0 97 TEST_ASSERTION(thread != current, "Saw the same thread twice!");
michael@0 98
michael@0 99 if (!thread) {
michael@0 100 gShutDownThreadList[i] = current;
michael@0 101 if (i == (NUMBER_OF_THREADS - 1)) {
michael@0 102 gAllThreadsShutDown = true;
michael@0 103 mon.NotifyAll();
michael@0 104 }
michael@0 105 return NS_OK;
michael@0 106 }
michael@0 107 }
michael@0 108
michael@0 109 TEST_ASSERTION(false, "Too many threads!");
michael@0 110 return NS_ERROR_FAILURE;
michael@0 111 }
michael@0 112
michael@0 113 class AutoCreateAndDestroyReentrantMonitor
michael@0 114 {
michael@0 115 public:
michael@0 116 AutoCreateAndDestroyReentrantMonitor(ReentrantMonitor** aReentrantMonitorPtr)
michael@0 117 : mReentrantMonitorPtr(aReentrantMonitorPtr) {
michael@0 118 *aReentrantMonitorPtr = new ReentrantMonitor("TestThreadPoolListener::AutoMon");
michael@0 119 TEST_ASSERTION(*aReentrantMonitorPtr, "Out of memory!");
michael@0 120 }
michael@0 121
michael@0 122 ~AutoCreateAndDestroyReentrantMonitor() {
michael@0 123 if (*mReentrantMonitorPtr) {
michael@0 124 delete *mReentrantMonitorPtr;
michael@0 125 *mReentrantMonitorPtr = nullptr;
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 private:
michael@0 130 ReentrantMonitor** mReentrantMonitorPtr;
michael@0 131 };
michael@0 132
michael@0 133 int main(int argc, char** argv)
michael@0 134 {
michael@0 135 ScopedXPCOM xpcom("ThreadPoolListener");
michael@0 136 NS_ENSURE_FALSE(xpcom.failed(), 1);
michael@0 137
michael@0 138 nsIThread* createdThreadList[NUMBER_OF_THREADS] = { nullptr };
michael@0 139 gCreatedThreadList = createdThreadList;
michael@0 140
michael@0 141 nsIThread* shutDownThreadList[NUMBER_OF_THREADS] = { nullptr };
michael@0 142 gShutDownThreadList = shutDownThreadList;
michael@0 143
michael@0 144 AutoCreateAndDestroyReentrantMonitor newMon(&gReentrantMonitor);
michael@0 145 NS_ENSURE_TRUE(gReentrantMonitor, 1);
michael@0 146
michael@0 147 nsresult rv;
michael@0 148
michael@0 149 nsCOMPtr<nsIThreadPool> pool =
michael@0 150 do_CreateInstance(NS_THREADPOOL_CONTRACTID, &rv);
michael@0 151 NS_ENSURE_SUCCESS(rv, 1);
michael@0 152
michael@0 153 rv = pool->SetThreadLimit(NUMBER_OF_THREADS);
michael@0 154 NS_ENSURE_SUCCESS(rv, 1);
michael@0 155
michael@0 156 rv = pool->SetIdleThreadLimit(NUMBER_OF_THREADS);
michael@0 157 NS_ENSURE_SUCCESS(rv, 1);
michael@0 158
michael@0 159 rv = pool->SetIdleThreadTimeout(IDLE_THREAD_TIMEOUT);
michael@0 160 NS_ENSURE_SUCCESS(rv, 1);
michael@0 161
michael@0 162 nsCOMPtr<nsIThreadPoolListener> listener = new Listener();
michael@0 163 NS_ENSURE_TRUE(listener, 1);
michael@0 164
michael@0 165 rv = pool->SetListener(listener);
michael@0 166 NS_ENSURE_SUCCESS(rv, 1);
michael@0 167
michael@0 168 {
michael@0 169 ReentrantMonitorAutoEnter mon(*gReentrantMonitor);
michael@0 170
michael@0 171 for (uint32_t i = 0; i < NUMBER_OF_THREADS; i++) {
michael@0 172 nsCOMPtr<nsIRunnable> runnable = new nsRunnable();
michael@0 173 NS_ENSURE_TRUE(runnable, 1);
michael@0 174
michael@0 175 rv = pool->Dispatch(runnable, NS_DISPATCH_NORMAL);
michael@0 176 NS_ENSURE_SUCCESS(rv, 1);
michael@0 177 }
michael@0 178
michael@0 179 gAllRunnablesPosted = true;
michael@0 180 mon.NotifyAll();
michael@0 181 }
michael@0 182
michael@0 183 {
michael@0 184 ReentrantMonitorAutoEnter mon(*gReentrantMonitor);
michael@0 185 while (!gAllThreadsCreated) {
michael@0 186 mon.Wait();
michael@0 187 }
michael@0 188 }
michael@0 189
michael@0 190 rv = pool->Shutdown();
michael@0 191 NS_ENSURE_SUCCESS(rv, 1);
michael@0 192
michael@0 193 {
michael@0 194 ReentrantMonitorAutoEnter mon(*gReentrantMonitor);
michael@0 195 while (!gAllThreadsShutDown) {
michael@0 196 mon.Wait();
michael@0 197 }
michael@0 198 }
michael@0 199
michael@0 200 for (uint32_t i = 0; i < NUMBER_OF_THREADS; i++) {
michael@0 201 nsIThread* created = gCreatedThreadList[i];
michael@0 202 NS_ENSURE_TRUE(created, 1);
michael@0 203
michael@0 204 bool match = false;
michael@0 205 for (uint32_t j = 0; j < NUMBER_OF_THREADS; j++) {
michael@0 206 nsIThread* destroyed = gShutDownThreadList[j];
michael@0 207 NS_ENSURE_TRUE(destroyed, 1);
michael@0 208
michael@0 209 if (destroyed == created) {
michael@0 210 match = true;
michael@0 211 break;
michael@0 212 }
michael@0 213 }
michael@0 214
michael@0 215 NS_ENSURE_TRUE(match, 1);
michael@0 216 }
michael@0 217
michael@0 218 return 0;
michael@0 219 }

mercurial