Changeset 101895 in vbox
- Timestamp:
- Nov 6, 2023 7:11:38 PM (15 months ago)
- Location:
- trunk/src/libs/xpcom18a4/xpcom/tests
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/libs/xpcom18a4/xpcom/tests/TestAutoLock.cpp
r1 r101895 43 43 44 44 #include "nsAutoLock.h" 45 #include "prthread.h" 45 46 #include <iprt/assert.h> 47 #include <iprt/errcore.h> 48 #include <iprt/initterm.h> 49 #include <iprt/message.h> 50 #include <iprt/thread.h> 46 51 47 52 PRLock* gLock; 48 53 int gCount; 49 54 50 static void PR_CALLBACK run(void* arg)55 static DECLCALLBACK(int) run(RTTHREAD hSelf, void* arg) 51 56 { 57 RT_NOREF(hSelf, arg); 58 52 59 for (int i = 0; i < 1000000; ++i) { 53 60 nsAutoLock guard(gLock); 54 61 ++gCount; 55 PR_ASSERT(gCount == 1);62 AssertRelease(gCount == 1); 56 63 --gCount; 57 64 } 65 66 return VINF_SUCCESS; 58 67 } 59 68 … … 63 72 gLock = PR_NewLock(); 64 73 gCount = 0; 74 75 int vrc = RTR3InitExe(argc, &argv, 0); 76 if (RT_FAILURE(vrc)) 77 return RTMsgInitFailure(vrc); 65 78 66 79 // This shouldn't compile … … 74 87 75 88 // Fork a thread to access the shared variable in a tight loop 76 PRThread* t1 = 77 PR_CreateThread(PR_SYSTEM_THREAD, 78 run, 79 nsnull, 80 PR_PRIORITY_NORMAL, 81 PR_GLOBAL_THREAD, 82 PR_JOINABLE_THREAD, 83 0); 89 RTTHREAD hThread = NIL_RTTHREAD; 90 vrc = RTThreadCreate(&hThread, run, NULL, 0 /*cbStack*/, 91 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "AutoLockTest"); 84 92 85 93 // ...and now do the same thing ourselves 86 run( nsnull);94 run(NIL_RTTHREAD, nsnull); 87 95 88 96 // Wait for the background thread to finish, if necessary. 89 PR_JoinThread(t1); 97 int rcThread; 98 RTThreadWait(hThread, RT_INDEFINITE_WAIT, &rcThread); 99 AssertRC(rcThread); 100 90 101 return 0; 91 102 } -
trunk/src/libs/xpcom18a4/xpcom/tests/TestThreads.cpp
r49491 r101895 41 41 #include <stdio.h> 42 42 #include <stdlib.h> 43 #include "nspr.h"44 43 #include "nsCOMPtr.h" 45 44 #include "nsIServiceManager.h" … … 132 131 } 133 132 134 class nsStressRunner : public nsIRunnable {135 public:136 NS_DECL_ISUPPORTS137 138 NS_IMETHOD Run() {139 NS_ASSERTION(!mWasRun, "run twice!");140 mWasRun = PR_TRUE;141 PR_Sleep(1);142 if (!PR_AtomicDecrement(&gNum)) {143 printf(" last thread was %d\n", mNum);144 }145 return NS_OK;146 }147 148 nsStressRunner(int num) : mNum(num), mWasRun(PR_FALSE) {149 PR_AtomicIncrement(&gNum);150 }151 152 static PRInt32 GetGlobalCount() {return gNum;}153 154 private:155 ~nsStressRunner() {156 NS_ASSERTION(mWasRun, "never run!");157 }158 159 protected:160 static PRInt32 gNum;161 PRInt32 mNum;162 PRBool mWasRun;163 };164 165 PRInt32 nsStressRunner::gNum = 0;166 167 NS_IMPL_THREADSAFE_ISUPPORTS1(nsStressRunner, nsIRunnable)168 169 static int Stress(int loops, int threads)170 {171 172 for (int i = 0; i < loops; i++) {173 printf("Loop %d of %d\n", i+1, loops);174 175 int k;176 nsIThread** array = new nsIThread*[threads];177 NS_ASSERTION(array, "out of memory");178 179 NS_ASSERTION(!nsStressRunner::GetGlobalCount(), "bad count of runnables");180 181 for (k = 0; k < threads; k++) {182 nsCOMPtr<nsIThread> t;183 nsresult rv = NS_NewThread(getter_AddRefs(t),184 new nsStressRunner(k),185 0, PR_JOINABLE_THREAD);186 NS_ASSERTION(NS_SUCCEEDED(rv), "can't create thread");187 NS_ADDREF(array[k] = t);188 }189 190 for (k = threads-1; k >= 0; k--) {191 array[k]->Join();192 NS_RELEASE(array[k]);193 }194 delete [] array;195 }196 return 0;197 }198 199 PR_STATIC_CALLBACK(void) threadProc(void *arg)200 {201 // printf(" running thread %d\n", (int) arg);202 PR_Sleep(1);203 PR_ASSERT(PR_JOINABLE_THREAD == PR_GetThreadState(PR_GetCurrentThread()));204 }205 206 static int StressNSPR(int loops, int threads)207 {208 209 for (int i = 0; i < loops; i++) {210 printf("Loop %d of %d\n", i+1, loops);211 212 int k;213 PRThread** array = new PRThread*[threads];214 PR_ASSERT(array);215 216 for (k = 0; k < threads; k++) {217 array[k] = PR_CreateThread(PR_USER_THREAD,218 threadProc, (void*)(uintptr_t)k,219 PR_PRIORITY_NORMAL,220 PR_GLOBAL_THREAD,221 PR_JOINABLE_THREAD,222 0);223 PR_ASSERT(array[k]);224 }225 226 for (k = 0; k < threads; k++) {227 PR_ASSERT(PR_JOINABLE_THREAD == PR_GetThreadState(array[k]));228 }229 230 for (k = threads-1; k >= 0; k--) {231 PR_JoinThread(array[k]);232 }233 delete [] array;234 }235 return 0;236 }237 238 239 133 int 240 134 main(int argc, char** argv) … … 246 140 if (NS_FAILED(rv)) return -1; 247 141 248 if (argc > 1 && !strcmp(argv[1], "-stress")) { 249 int loops; 250 int threads; 251 if (argc != 4 || *argv[2] != '-' || *argv[3] != '-' || 252 !(loops = atoi(argv[2]+1)) || !(threads = atoi(argv[3]+1))) { 253 printf("To use -stress you must pass loop count and thread count...\n" 254 " TestThreads -stress -1000 -50\n"); 255 } else { 256 printf("Running stress test with %d loops of %d threads each\n", 257 loops, threads); 258 retval = Stress(loops, threads); 259 } 260 } else if (argc > 1 && !strcmp(argv[1], "-stress-nspr")) { 261 int loops; 262 int threads; 263 if (argc != 4 || *argv[2] != '-' || *argv[3] != '-' || 264 !(loops = atoi(argv[2]+1)) || !(threads = atoi(argv[3]+1))) { 265 printf("To use -stress-nspr you must pass loop count and thread count...\n" 266 " TestThreads -stress -1000 -50\n"); 267 } else { 268 printf("Running stress test with %d loops of %d threads each\n", 269 loops, threads); 270 retval = StressNSPR(loops, threads); 271 } 272 } else { 273 rv = TestThreads(); 274 if (NS_FAILED(rv)) return -1; 275 } 142 rv = TestThreads(); 143 if (NS_FAILED(rv)) return -1; 276 144 277 145 rv = NS_ShutdownXPCOM(nsnull);
Note:
See TracChangeset
for help on using the changeset viewer.