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