home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / suspend.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.2 KB  |  202 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "nspr.h"
  20. #include "prpriv.h"
  21. #include "prinrval.h"
  22.  
  23. #if defined(XP_MAC)
  24. #include "gcint.h"
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30.  
  31. #ifdef XP_MAC
  32. #include "gcint.h"
  33. #include "prlog.h"
  34. #define printf PR_LogPrint
  35. extern void SetupMacPrintfLog(char *logFile);
  36. #endif
  37.  
  38. PRMonitor *mon;
  39. PRInt32 count;
  40. PRInt32 alive;
  41.  
  42. #define SLEEP_TIME    4    /* secs */
  43.  
  44. void PR_CALLBACK
  45. Level_2_Thread(void *arg)
  46. {
  47.     PR_Sleep(PR_MillisecondsToInterval(4 * 1000));
  48.     printf("Level_2_Thread[0x%lx] exiting\n",PR_GetCurrentThread());
  49.     return;
  50. }
  51.  
  52. void PR_CALLBACK
  53. Level_1_Thread(void *arg)
  54. {
  55.     PRUint32 tmp = (PRUint32)arg;
  56.     PRThreadScope scope = (PRThreadScope) tmp;
  57.     PRThread *thr;
  58.  
  59.     thr = PR_CreateThreadGCAble(PR_USER_THREAD,
  60.                           Level_2_Thread,
  61.                           NULL,
  62.                           PR_PRIORITY_HIGH,
  63.                           scope,
  64.                           PR_JOINABLE_THREAD,
  65.                           0);
  66.  
  67.     if (!thr) {
  68.         printf("Could not create thread!\n");
  69.     } else {
  70.     printf("Level_1_Thread[0x%lx] created %15s thread 0x%lx\n",
  71.                 PR_GetCurrentThread(),
  72.                 (scope == PR_GLOBAL_THREAD) ?
  73.                 "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD",
  74.                 thr);
  75.         PR_JoinThread(thr);
  76.     }
  77.     PR_EnterMonitor(mon);
  78.     alive--;
  79.     PR_Notify(mon);
  80.     PR_ExitMonitor(mon);
  81.     printf("Thread[0x%lx] exiting\n",PR_GetCurrentThread());
  82. }
  83.  
  84. static PRStatus PR_CALLBACK print_thread(PRThread *thread, int i, void *arg)
  85. {
  86. PRInt32 words;
  87. PRWord *registers;
  88.  
  89.     printf(
  90.         "\nprint_thread[0x%lx]: %-20s - i = %ld\n",thread, 
  91.         (PR_GLOBAL_THREAD == PR_GetThreadScope(thread)) ?
  92.         "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD", i);
  93.     registers = PR_GetGCRegisters(thread, 0, (int *)&words);
  94.     printf("Regsters R0 = 0x%x R1 = 0x%x R2 = 0x%x R3 = 0x%x\n",
  95.                         registers[0],registers[1],registers[2],registers[3]);
  96.     printf("Stack Pointer = 0x%lx\n", PR_GetSP(thread));
  97.     return PR_SUCCESS;
  98. }
  99.  
  100. static void Level_0_Thread(PRThreadScope scope1, PRThreadScope scope2)
  101. {
  102.     PRThread *thr;
  103.     PRThread *me = PR_GetCurrentThread();
  104.     int n;
  105.     PRInt32 words;
  106.     PRWord *registers;
  107.  
  108.     alive = 0;
  109.     mon = PR_NewMonitor();
  110.  
  111.     alive = count;
  112.     for (n=0; n<count; n++) {
  113.         thr = PR_CreateThreadGCAble(PR_USER_THREAD,
  114.                               Level_1_Thread, 
  115.                               (void *)scope2, 
  116.                               PR_PRIORITY_NORMAL,
  117.                               scope1,
  118.                               PR_UNJOINABLE_THREAD,
  119.                               0);
  120.         if (!thr) {
  121.             printf("Could not create thread!\n");
  122.             alive--;
  123.         }
  124.     printf("Level_0_Thread[0x%lx] created %15s thread 0x%lx\n",
  125.                 PR_GetCurrentThread(),
  126.                 (scope1 == PR_GLOBAL_THREAD) ?
  127.                 "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD",
  128.                 thr);
  129.          
  130.         PR_Sleep(0);
  131.     }
  132.     PR_SuspendAll();
  133.     PR_EnumerateThreads(print_thread, NULL);
  134.     registers = PR_GetGCRegisters(me, 1, (int *)&words);
  135.     printf("My Registers: R0 = 0x%x R1 = 0x%x R2 = 0x%x R3 = 0x%x\n",
  136.                         registers[0],registers[1],registers[2],registers[3]);
  137.     printf("My Stack Pointer = 0x%lx\n", PR_GetSP(me));
  138.     PR_ResumeAll();
  139.  
  140.     /* Wait for all threads to exit */
  141.     PR_EnterMonitor(mon);
  142.     while (alive) {
  143.         PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
  144.     }
  145.  
  146.     PR_ExitMonitor(mon);
  147.     PR_DestroyMonitor(mon);
  148. }
  149.  
  150. static void CreateThreadsUU(void)
  151. {
  152.     Level_0_Thread(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
  153. }
  154.  
  155. static void CreateThreadsUK(void)
  156. {
  157.     Level_0_Thread(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
  158. }
  159.  
  160. static void CreateThreadsKU(void)
  161. {
  162.     Level_0_Thread(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
  163. }
  164.  
  165. static void CreateThreadsKK(void)
  166. {
  167.     Level_0_Thread(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
  168. }
  169.  
  170.  
  171. void
  172. main(int argc, char **argv)
  173. {
  174.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  175.     PR_STDIO_INIT();
  176.  
  177. #ifdef XP_MAC
  178.     SetupMacPrintfLog("suspend.log");
  179. #endif
  180.  
  181.     if (argc > 1) {
  182.         count = atoi(argv[1]);
  183.     } else {
  184.         count = 5;
  185.     }
  186.  
  187.     printf("\n\n%20s%30s\n\n"," ","Suspend_Resume Test");
  188.     CreateThreadsUU();
  189.     CreateThreadsUK();
  190.     CreateThreadsKU();
  191.     CreateThreadsKK();
  192.     PR_SetConcurrency(2);
  193.  
  194.     printf("\n%20s%30s\n\n"," ","Added 2nd CPU\n");
  195.  
  196.     CreateThreadsUK();
  197.     CreateThreadsKK();
  198.     CreateThreadsUU();
  199.     CreateThreadsKU();
  200.     PR_Cleanup();
  201. }
  202.