nsprpub/pr/tests/suspend.c

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.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #ifdef XP_BEOS
     7 #include <stdio.h>
     8 int main()
     9 {
    10     printf( "This test is not ported to the BeOS\n" );
    11     return 0;
    12 }
    13 #else
    15 #include "nspr.h"
    16 #include "prpriv.h"
    17 #include "prinrval.h"
    19 #include <stdio.h>
    20 #include <stdlib.h>
    21 #include <string.h>
    23 PRMonitor *mon;
    24 PRInt32 count;
    25 PRInt32 alive;
    27 #define SLEEP_TIME    4    /* secs */
    29 void PR_CALLBACK
    30 Level_2_Thread(void *arg)
    31 {
    32     PR_Sleep(PR_MillisecondsToInterval(4 * 1000));
    33     printf("Level_2_Thread[0x%lx] exiting\n",PR_GetCurrentThread());
    34     return;
    35 }
    37 void PR_CALLBACK
    38 Level_1_Thread(void *arg)
    39 {
    40     PRUint32 tmp = (PRUint32)arg;
    41     PRThreadScope scope = (PRThreadScope) tmp;
    42     PRThread *thr;
    44     thr = PR_CreateThreadGCAble(PR_USER_THREAD,
    45         Level_2_Thread,
    46         NULL,
    47         PR_PRIORITY_HIGH,
    48         scope,
    49         PR_JOINABLE_THREAD,
    50         0);
    52     if (!thr) {
    53         printf("Could not create thread!\n");
    54     } else {
    55         printf("Level_1_Thread[0x%lx] created %15s thread 0x%lx\n",
    56             PR_GetCurrentThread(),
    57             (scope == PR_GLOBAL_THREAD) ?
    58             "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD",
    59             thr);
    60         PR_JoinThread(thr);
    61     }
    62     PR_EnterMonitor(mon);
    63     alive--;
    64     PR_Notify(mon);
    65     PR_ExitMonitor(mon);
    66     printf("Thread[0x%lx] exiting\n",PR_GetCurrentThread());
    67 }
    69 static PRStatus PR_CALLBACK print_thread(PRThread *thread, int i, void *arg)
    70 {
    71     PRInt32 words;
    72     PRWord *registers;
    74     printf(
    75         "\nprint_thread[0x%lx]: %-20s - i = %ld\n",thread, 
    76         (PR_GLOBAL_THREAD == PR_GetThreadScope(thread)) ?
    77         "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD", i);
    78     registers = PR_GetGCRegisters(thread, 0, (int *)&words);
    79     if (registers)
    80         printf("Registers R0 = 0x%x R1 = 0x%x R2 = 0x%x R3 = 0x%x\n",
    81             registers[0],registers[1],registers[2],registers[3]);
    82     printf("Stack Pointer = 0x%lx\n", PR_GetSP(thread));
    83     return PR_SUCCESS;
    84 }
    86 static void Level_0_Thread(PRThreadScope scope1, PRThreadScope scope2)
    87 {
    88     PRThread *thr;
    89     PRThread *me = PR_GetCurrentThread();
    90     int n;
    91     PRInt32 words;
    92     PRWord *registers;
    94     alive = 0;
    95     mon = PR_NewMonitor();
    97     alive = count;
    98     for (n=0; n<count; n++) {
    99         thr = PR_CreateThreadGCAble(PR_USER_THREAD,
   100             Level_1_Thread, 
   101             (void *)scope2, 
   102             PR_PRIORITY_NORMAL,
   103             scope1,
   104             PR_UNJOINABLE_THREAD,
   105             0);
   106         if (!thr) {
   107             printf("Could not create thread!\n");
   108             alive--;
   109         }
   110         printf("Level_0_Thread[0x%lx] created %15s thread 0x%lx\n",
   111             PR_GetCurrentThread(),
   112             (scope1 == PR_GLOBAL_THREAD) ?
   113             "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD",
   114             thr);
   116         PR_Sleep(0);
   117     }
   118     PR_SuspendAll();
   119     PR_EnumerateThreads(print_thread, NULL);
   120     registers = PR_GetGCRegisters(me, 1, (int *)&words);
   121     if (registers)
   122         printf("My Registers: R0 = 0x%x R1 = 0x%x R2 = 0x%x R3 = 0x%x\n",
   123             registers[0],registers[1],registers[2],registers[3]);
   124     printf("My Stack Pointer = 0x%lx\n", PR_GetSP(me));
   125     PR_ResumeAll();
   127     /* Wait for all threads to exit */
   128     PR_EnterMonitor(mon);
   129     while (alive) {
   130         PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
   131     }
   133     PR_ExitMonitor(mon);
   134     PR_DestroyMonitor(mon);
   135 }
   137 static void CreateThreadsUU(void)
   138 {
   139     Level_0_Thread(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
   140 }
   142 static void CreateThreadsUK(void)
   143 {
   144     Level_0_Thread(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
   145 }
   147 static void CreateThreadsKU(void)
   148 {
   149     Level_0_Thread(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
   150 }
   152 static void CreateThreadsKK(void)
   153 {
   154     Level_0_Thread(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
   155 }
   158 int main(int argc, char **argv)
   159 {
   160     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   161     PR_STDIO_INIT();
   163     if (argc > 1) {
   164         count = atoi(argv[1]);
   165     } else {
   166         count = 5;
   167     }
   169     printf("\n\n%20s%30s\n\n"," ","Suspend_Resume Test");
   170     CreateThreadsUU();
   171     CreateThreadsUK();
   172     CreateThreadsKU();
   173     CreateThreadsKK();
   174     PR_SetConcurrency(2);
   176     printf("\n%20s%30s\n\n"," ","Added 2nd CPU\n");
   178     CreateThreadsUK();
   179     CreateThreadsKK();
   180     CreateThreadsUU();
   181     CreateThreadsKU();
   182     PR_Cleanup();
   184     return 0;
   185 }
   187 #endif /* XP_BEOS */

mercurial