VirtualBox

Changeset 101895 in vbox


Ignore:
Timestamp:
Nov 6, 2023 7:11:38 PM (15 months ago)
Author:
vboxsync
Message:

libs/xpcom: Rmove unused code and convert other to use IPRT, bugref:10545

Location:
trunk/src/libs/xpcom18a4/xpcom/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/libs/xpcom18a4/xpcom/tests/TestAutoLock.cpp

    r1 r101895  
    4343
    4444#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>
    4651
    4752PRLock* gLock;
    4853int gCount;
    4954
    50 static void PR_CALLBACK run(void* arg)
     55static DECLCALLBACK(int) run(RTTHREAD hSelf, void* arg)
    5156{
     57    RT_NOREF(hSelf, arg);
     58
    5259    for (int i = 0; i < 1000000; ++i) {
    5360        nsAutoLock guard(gLock);
    5461        ++gCount;
    55         PR_ASSERT(gCount == 1);
     62        AssertRelease(gCount == 1);
    5663        --gCount;
    5764    }
     65
     66    return VINF_SUCCESS;
    5867}
    5968
     
    6372    gLock = PR_NewLock();
    6473    gCount = 0;
     74
     75    int vrc = RTR3InitExe(argc, &argv, 0);
     76    if (RT_FAILURE(vrc))
     77        return RTMsgInitFailure(vrc);
    6578
    6679    // This shouldn't compile
     
    7487
    7588    // 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");
    8492
    8593    // ...and now do the same thing ourselves
    86     run(nsnull);
     94    run(NIL_RTTHREAD, nsnull);
    8795
    8896    // 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
    90101    return 0;
    91102}
  • trunk/src/libs/xpcom18a4/xpcom/tests/TestThreads.cpp

    r49491 r101895  
    4141#include <stdio.h>
    4242#include <stdlib.h>
    43 #include "nspr.h"
    4443#include "nsCOMPtr.h"
    4544#include "nsIServiceManager.h"
     
    132131}
    133132
    134 class nsStressRunner : public nsIRunnable {
    135 public:
    136     NS_DECL_ISUPPORTS
    137 
    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 
    239133int
    240134main(int argc, char** argv)
     
    246140    if (NS_FAILED(rv)) return -1;
    247141
    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;
    276144
    277145    rv = NS_ShutdownXPCOM(nsnull);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette