nsprpub/pr/tests/suspend.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/suspend.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,187 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifdef XP_BEOS
    1.10 +#include <stdio.h>
    1.11 +int main()
    1.12 +{
    1.13 +    printf( "This test is not ported to the BeOS\n" );
    1.14 +    return 0;
    1.15 +}
    1.16 +#else
    1.17 +
    1.18 +#include "nspr.h"
    1.19 +#include "prpriv.h"
    1.20 +#include "prinrval.h"
    1.21 +
    1.22 +#include <stdio.h>
    1.23 +#include <stdlib.h>
    1.24 +#include <string.h>
    1.25 +
    1.26 +PRMonitor *mon;
    1.27 +PRInt32 count;
    1.28 +PRInt32 alive;
    1.29 +
    1.30 +#define SLEEP_TIME    4    /* secs */
    1.31 +
    1.32 +void PR_CALLBACK
    1.33 +Level_2_Thread(void *arg)
    1.34 +{
    1.35 +    PR_Sleep(PR_MillisecondsToInterval(4 * 1000));
    1.36 +    printf("Level_2_Thread[0x%lx] exiting\n",PR_GetCurrentThread());
    1.37 +    return;
    1.38 +}
    1.39 +
    1.40 +void PR_CALLBACK
    1.41 +Level_1_Thread(void *arg)
    1.42 +{
    1.43 +    PRUint32 tmp = (PRUint32)arg;
    1.44 +    PRThreadScope scope = (PRThreadScope) tmp;
    1.45 +    PRThread *thr;
    1.46 +
    1.47 +    thr = PR_CreateThreadGCAble(PR_USER_THREAD,
    1.48 +        Level_2_Thread,
    1.49 +        NULL,
    1.50 +        PR_PRIORITY_HIGH,
    1.51 +        scope,
    1.52 +        PR_JOINABLE_THREAD,
    1.53 +        0);
    1.54 +
    1.55 +    if (!thr) {
    1.56 +        printf("Could not create thread!\n");
    1.57 +    } else {
    1.58 +        printf("Level_1_Thread[0x%lx] created %15s thread 0x%lx\n",
    1.59 +            PR_GetCurrentThread(),
    1.60 +            (scope == PR_GLOBAL_THREAD) ?
    1.61 +            "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD",
    1.62 +            thr);
    1.63 +        PR_JoinThread(thr);
    1.64 +    }
    1.65 +    PR_EnterMonitor(mon);
    1.66 +    alive--;
    1.67 +    PR_Notify(mon);
    1.68 +    PR_ExitMonitor(mon);
    1.69 +    printf("Thread[0x%lx] exiting\n",PR_GetCurrentThread());
    1.70 +}
    1.71 +
    1.72 +static PRStatus PR_CALLBACK print_thread(PRThread *thread, int i, void *arg)
    1.73 +{
    1.74 +    PRInt32 words;
    1.75 +    PRWord *registers;
    1.76 +
    1.77 +    printf(
    1.78 +        "\nprint_thread[0x%lx]: %-20s - i = %ld\n",thread, 
    1.79 +        (PR_GLOBAL_THREAD == PR_GetThreadScope(thread)) ?
    1.80 +        "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD", i);
    1.81 +    registers = PR_GetGCRegisters(thread, 0, (int *)&words);
    1.82 +    if (registers)
    1.83 +        printf("Registers R0 = 0x%x R1 = 0x%x R2 = 0x%x R3 = 0x%x\n",
    1.84 +            registers[0],registers[1],registers[2],registers[3]);
    1.85 +    printf("Stack Pointer = 0x%lx\n", PR_GetSP(thread));
    1.86 +    return PR_SUCCESS;
    1.87 +}
    1.88 +
    1.89 +static void Level_0_Thread(PRThreadScope scope1, PRThreadScope scope2)
    1.90 +{
    1.91 +    PRThread *thr;
    1.92 +    PRThread *me = PR_GetCurrentThread();
    1.93 +    int n;
    1.94 +    PRInt32 words;
    1.95 +    PRWord *registers;
    1.96 +
    1.97 +    alive = 0;
    1.98 +    mon = PR_NewMonitor();
    1.99 +
   1.100 +    alive = count;
   1.101 +    for (n=0; n<count; n++) {
   1.102 +        thr = PR_CreateThreadGCAble(PR_USER_THREAD,
   1.103 +            Level_1_Thread, 
   1.104 +            (void *)scope2, 
   1.105 +            PR_PRIORITY_NORMAL,
   1.106 +            scope1,
   1.107 +            PR_UNJOINABLE_THREAD,
   1.108 +            0);
   1.109 +        if (!thr) {
   1.110 +            printf("Could not create thread!\n");
   1.111 +            alive--;
   1.112 +        }
   1.113 +        printf("Level_0_Thread[0x%lx] created %15s thread 0x%lx\n",
   1.114 +            PR_GetCurrentThread(),
   1.115 +            (scope1 == PR_GLOBAL_THREAD) ?
   1.116 +            "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD",
   1.117 +            thr);
   1.118 +
   1.119 +        PR_Sleep(0);
   1.120 +    }
   1.121 +    PR_SuspendAll();
   1.122 +    PR_EnumerateThreads(print_thread, NULL);
   1.123 +    registers = PR_GetGCRegisters(me, 1, (int *)&words);
   1.124 +    if (registers)
   1.125 +        printf("My Registers: R0 = 0x%x R1 = 0x%x R2 = 0x%x R3 = 0x%x\n",
   1.126 +            registers[0],registers[1],registers[2],registers[3]);
   1.127 +    printf("My Stack Pointer = 0x%lx\n", PR_GetSP(me));
   1.128 +    PR_ResumeAll();
   1.129 +
   1.130 +    /* Wait for all threads to exit */
   1.131 +    PR_EnterMonitor(mon);
   1.132 +    while (alive) {
   1.133 +        PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
   1.134 +    }
   1.135 +
   1.136 +    PR_ExitMonitor(mon);
   1.137 +    PR_DestroyMonitor(mon);
   1.138 +}
   1.139 +
   1.140 +static void CreateThreadsUU(void)
   1.141 +{
   1.142 +    Level_0_Thread(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
   1.143 +}
   1.144 +
   1.145 +static void CreateThreadsUK(void)
   1.146 +{
   1.147 +    Level_0_Thread(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
   1.148 +}
   1.149 +
   1.150 +static void CreateThreadsKU(void)
   1.151 +{
   1.152 +    Level_0_Thread(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
   1.153 +}
   1.154 +
   1.155 +static void CreateThreadsKK(void)
   1.156 +{
   1.157 +    Level_0_Thread(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
   1.158 +}
   1.159 +
   1.160 +
   1.161 +int main(int argc, char **argv)
   1.162 +{
   1.163 +    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   1.164 +    PR_STDIO_INIT();
   1.165 +
   1.166 +    if (argc > 1) {
   1.167 +        count = atoi(argv[1]);
   1.168 +    } else {
   1.169 +        count = 5;
   1.170 +    }
   1.171 +
   1.172 +    printf("\n\n%20s%30s\n\n"," ","Suspend_Resume Test");
   1.173 +    CreateThreadsUU();
   1.174 +    CreateThreadsUK();
   1.175 +    CreateThreadsKU();
   1.176 +    CreateThreadsKK();
   1.177 +    PR_SetConcurrency(2);
   1.178 +
   1.179 +    printf("\n%20s%30s\n\n"," ","Added 2nd CPU\n");
   1.180 +
   1.181 +    CreateThreadsUK();
   1.182 +    CreateThreadsKK();
   1.183 +    CreateThreadsUU();
   1.184 +    CreateThreadsKU();
   1.185 +    PR_Cleanup();
   1.186 +
   1.187 +    return 0;
   1.188 +}
   1.189 +
   1.190 +#endif /* XP_BEOS */

mercurial