Back to index

lightning-sunbird  0.9+nobinonly
suspend.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifdef XP_BEOS
00039 #include <stdio.h>
00040 int main()
00041 {
00042     printf( "This test is not ported to the BeOS\n" );
00043     return 0;
00044 }
00045 #else
00046 
00047 #include "nspr.h"
00048 #include "prpriv.h"
00049 #include "prinrval.h"
00050 
00051 #if defined(XP_MAC)
00052 #include "gcint.h"
00053 #endif
00054 
00055 #include <stdio.h>
00056 #include <stdlib.h>
00057 #include <string.h>
00058 
00059 #ifdef XP_MAC
00060 #include "gcint.h"
00061 #include "prlog.h"
00062 #define printf PR_LogPrint
00063 extern void SetupMacPrintfLog(char *logFile);
00064 #endif
00065 
00066 PRMonitor *mon;
00067 PRInt32 count;
00068 PRInt32 alive;
00069 
00070 #define SLEEP_TIME    4    /* secs */
00071 
00072 void PR_CALLBACK
00073 Level_2_Thread(void *arg)
00074 {
00075     PR_Sleep(PR_MillisecondsToInterval(4 * 1000));
00076     printf("Level_2_Thread[0x%lx] exiting\n",PR_GetCurrentThread());
00077     return;
00078 }
00079 
00080 void PR_CALLBACK
00081 Level_1_Thread(void *arg)
00082 {
00083     PRUint32 tmp = (PRUint32)arg;
00084     PRThreadScope scope = (PRThreadScope) tmp;
00085     PRThread *thr;
00086 
00087     thr = PR_CreateThreadGCAble(PR_USER_THREAD,
00088         Level_2_Thread,
00089         NULL,
00090         PR_PRIORITY_HIGH,
00091         scope,
00092         PR_JOINABLE_THREAD,
00093         0);
00094 
00095     if (!thr) {
00096         printf("Could not create thread!\n");
00097     } else {
00098         printf("Level_1_Thread[0x%lx] created %15s thread 0x%lx\n",
00099             PR_GetCurrentThread(),
00100             (scope == PR_GLOBAL_THREAD) ?
00101             "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD",
00102             thr);
00103         PR_JoinThread(thr);
00104     }
00105     PR_EnterMonitor(mon);
00106     alive--;
00107     PR_Notify(mon);
00108     PR_ExitMonitor(mon);
00109     printf("Thread[0x%lx] exiting\n",PR_GetCurrentThread());
00110 }
00111 
00112 static PRStatus PR_CALLBACK print_thread(PRThread *thread, int i, void *arg)
00113 {
00114     PRInt32 words;
00115     PRWord *registers;
00116 
00117     printf(
00118         "\nprint_thread[0x%lx]: %-20s - i = %ld\n",thread, 
00119         (PR_GLOBAL_THREAD == PR_GetThreadScope(thread)) ?
00120         "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD", i);
00121     registers = PR_GetGCRegisters(thread, 0, (int *)&words);
00122     printf("Regsters R0 = 0x%x R1 = 0x%x R2 = 0x%x R3 = 0x%x\n",
00123         registers[0],registers[1],registers[2],registers[3]);
00124     printf("Stack Pointer = 0x%lx\n", PR_GetSP(thread));
00125     return PR_SUCCESS;
00126 }
00127 
00128 static void Level_0_Thread(PRThreadScope scope1, PRThreadScope scope2)
00129 {
00130     PRThread *thr;
00131     PRThread *me = PR_GetCurrentThread();
00132     int n;
00133     PRInt32 words;
00134     PRWord *registers;
00135 
00136     alive = 0;
00137     mon = PR_NewMonitor();
00138 
00139     alive = count;
00140     for (n=0; n<count; n++) {
00141         thr = PR_CreateThreadGCAble(PR_USER_THREAD,
00142             Level_1_Thread, 
00143             (void *)scope2, 
00144             PR_PRIORITY_NORMAL,
00145             scope1,
00146             PR_UNJOINABLE_THREAD,
00147             0);
00148         if (!thr) {
00149             printf("Could not create thread!\n");
00150             alive--;
00151         }
00152         printf("Level_0_Thread[0x%lx] created %15s thread 0x%lx\n",
00153             PR_GetCurrentThread(),
00154             (scope1 == PR_GLOBAL_THREAD) ?
00155             "PR_GLOBAL_THREAD" : "PR_LOCAL_THREAD",
00156             thr);
00157 
00158         PR_Sleep(0);
00159     }
00160     PR_SuspendAll();
00161     PR_EnumerateThreads(print_thread, NULL);
00162     registers = PR_GetGCRegisters(me, 1, (int *)&words);
00163     printf("My Registers: R0 = 0x%x R1 = 0x%x R2 = 0x%x R3 = 0x%x\n",
00164         registers[0],registers[1],registers[2],registers[3]);
00165     printf("My Stack Pointer = 0x%lx\n", PR_GetSP(me));
00166     PR_ResumeAll();
00167 
00168     /* Wait for all threads to exit */
00169     PR_EnterMonitor(mon);
00170     while (alive) {
00171         PR_Wait(mon, PR_INTERVAL_NO_TIMEOUT);
00172     }
00173 
00174     PR_ExitMonitor(mon);
00175     PR_DestroyMonitor(mon);
00176 }
00177 
00178 static void CreateThreadsUU(void)
00179 {
00180     Level_0_Thread(PR_LOCAL_THREAD, PR_LOCAL_THREAD);
00181 }
00182 
00183 static void CreateThreadsUK(void)
00184 {
00185     Level_0_Thread(PR_LOCAL_THREAD, PR_GLOBAL_THREAD);
00186 }
00187 
00188 static void CreateThreadsKU(void)
00189 {
00190     Level_0_Thread(PR_GLOBAL_THREAD, PR_LOCAL_THREAD);
00191 }
00192 
00193 static void CreateThreadsKK(void)
00194 {
00195     Level_0_Thread(PR_GLOBAL_THREAD, PR_GLOBAL_THREAD);
00196 }
00197 
00198 
00199 void
00200 main(int argc, char **argv)
00201 {
00202     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00203     PR_STDIO_INIT();
00204 
00205 #ifdef XP_MAC
00206     SetupMacPrintfLog("suspend.log");
00207 #endif
00208 
00209     if (argc > 1) {
00210         count = atoi(argv[1]);
00211     } else {
00212         count = 5;
00213     }
00214 
00215     printf("\n\n%20s%30s\n\n"," ","Suspend_Resume Test");
00216     CreateThreadsUU();
00217     CreateThreadsUK();
00218     CreateThreadsKU();
00219     CreateThreadsKK();
00220     PR_SetConcurrency(2);
00221 
00222     printf("\n%20s%30s\n\n"," ","Added 2nd CPU\n");
00223 
00224     CreateThreadsUK();
00225     CreateThreadsKK();
00226     CreateThreadsUU();
00227     CreateThreadsKU();
00228     PR_Cleanup();
00229 }
00230 
00231 #endif /* XP_BEOS */