Test bustage fix by using specific event listener.
[wine-gecko.git] / nsprpub / lib / msgc / tests / thrashgc.c
blob9d49ec19f308fc8e1d7b11e2e0807f35e78bea3f
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
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/
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.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
22 * Contributor(s):
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.
36 * ***** END LICENSE BLOCK ***** */
38 /***********************************************************************
39 ** Name: thrashgc
41 ** Description: test garbace collection functions.
43 ** Modification History:
44 ** 19-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
45 ** The debug mode will print all of the printfs associated with this test.
46 ** The regress mode will be the default mode. Since the regress tool limits
47 ** the output to a one line status:PASS or FAIL,all of the printf statements
48 ** have been handled with an if (debug_mode) statement.
49 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
50 ** recognize the return code from tha main program.
51 ***********************************************************************/
52 /***********************************************************************
53 ** Includes
54 ***********************************************************************/
55 #include "prthread.h"
56 #include "prgc.h"
57 #include "prprf.h"
58 #include "prinrval.h"
59 #include "prlock.h"
60 #include "prinit.h"
61 #include "prcvar.h"
63 #ifndef XP_MAC
64 #include "private/pprthred.h"
65 #else
66 #include "pprthred.h"
67 #endif
69 #include <stdio.h>
70 #include <memory.h>
71 #include <string.h>
74 #ifdef XP_MAC
75 #include "prlog.h"
76 #define printf PR_LogPrint
77 extern void SetupMacPrintfLog(char *logFile);
78 #endif
80 PRIntn failed_already=0;
81 PRIntn debug_mode;
83 static char* progname;
84 static PRInt32 loops = 1000;
85 static int tix1, tix2, tix3;
86 static GCInfo* gcInfo;
87 static PRLock* stderrLock;
89 typedef struct Type1 Type1;
90 typedef struct Type2 Type2;
92 struct Type1 {
93 Type2* atwo;
94 Type1* next;
97 struct Type2 {
98 void* buf;
101 static void PR_CALLBACK ScanType1(void *obj) {
102 gcInfo->livePointer(((Type1 *)obj)->atwo);
103 gcInfo->livePointer(((Type1 *)obj)->next);
106 static void PR_CALLBACK ScanType2(void *obj) {
107 gcInfo->livePointer(((Type2 *)obj)->buf);
110 static GCType type1 = {
111 ScanType1
114 static GCType type2 = {
115 ScanType2
116 /* (void (*)(void*)) ScanType2 */
119 static GCType type3 = {
123 Type1* NewType1(void) {
124 Type1* p = (Type1*) PR_AllocMemory(sizeof(Type1), tix1, PR_ALLOC_DOUBLE);
125 PR_ASSERT(p != NULL);
126 return p;
129 Type2* NewType2(void) {
130 Type2* p = (Type2*) PR_AllocMemory(sizeof(Type2), tix2, PR_ALLOC_DOUBLE);
131 PR_ASSERT(p != NULL);
132 return p;
135 void* NewBuffer(PRInt32 size) {
136 void* p = PR_AllocMemory(size, tix3, PR_ALLOC_DOUBLE);
137 PR_ASSERT(p != NULL);
138 return p;
141 /* Allocate alot of garbage */
142 static void PR_CALLBACK AllocStuff(void *unused) {
143 PRInt32 i;
144 void* danglingRefs[50];
145 PRIntervalTime start, end;
146 char msg[100];
148 start = PR_IntervalNow();
149 for (i = 0; i < loops; i++) {
150 void* p;
151 if (i & 1) {
152 Type1* t1 = NewType1();
153 t1->atwo = NewType2();
154 t1->next = NewType1();
155 t1->atwo->buf = NewBuffer(100);
156 p = t1;
157 } else {
158 Type2* t2 = NewType2();
159 t2->buf = NewBuffer(i & 16383);
160 p = t2;
162 if ((i % 10) == 0) {
163 memmove(&danglingRefs[0], &danglingRefs[1], 49*sizeof(void*));
164 danglingRefs[49] = p;
167 end = PR_IntervalNow();
168 if (debug_mode) PR_snprintf(msg, sizeof(msg), "Thread %p: %ld allocations took %ld ms",
169 PR_GetCurrentThread(), loops,
170 PR_IntervalToMilliseconds((PRIntervalTime) (end - start)));
171 PR_Lock(stderrLock);
172 #ifndef XP_MAC
173 fprintf(stderr, "%s\n", msg);
174 #else
175 if (debug_mode) printf("%s\n", msg);
176 #endif
177 PR_Unlock(stderrLock);
180 static void usage(char *progname) {
181 #ifndef XP_MAC
182 fprintf(stderr, "Usage: %s [-t threads] [-l loops]\n", progname);
183 #else
184 printf("Usage: %s [-t threads] [-l loops]\n", progname);
185 #endif
186 exit(-1);
189 static int realMain(int argc, char **argv, char *notused) {
190 int i;
191 int threads = 0;
193 #ifndef XP_MAC
194 progname = strrchr(argv[0], '/');
195 if (progname == 0) progname = argv[0];
196 for (i = 1; i < argc; i++) {
197 if (strcmp(argv[i], "-t") == 0) {
198 if (i == argc - 1) {
199 usage(progname);
201 threads = atoi(argv[++i]);
202 if (threads < 0) threads = 0;
203 if (threads > 10000) threads = 10000;
204 continue;
206 if (strcmp(argv[i], "-l") == 0) {
207 if (i == argc - 1) {
208 usage(progname);
210 loops = atoi(argv[++i]);
211 continue;
213 usage(progname);
215 #else
216 threads = 50;
217 #endif
219 for (i = 0; i < threads; i++) {
220 PRThread* thread;
222 /* XXXXX */
223 thread = PR_CreateThreadGCAble(PR_USER_THREAD, /* thread type */
224 AllocStuff, /* start function */
225 NULL, /* arg */
226 PR_PRIORITY_NORMAL, /* priority */
227 PR_LOCAL_THREAD, /* thread scope */
228 PR_UNJOINABLE_THREAD, /* thread state */
229 0); /* stack size */
230 if (thread == 0) {
231 #ifndef XP_MAC
232 fprintf(stderr, "%s: no more threads (only %d were created)\n",
233 progname, i);
234 #else
235 printf("%s: no more threads (only %d were created)\n",
236 progname, i);
237 #endif
238 break;
241 AllocStuff(NULL);
242 return 0;
245 static int padMain(int argc, char **argv) {
246 char pad[512];
247 return realMain(argc, argv, pad);
250 int main(int argc, char **argv) {
251 int rv;
253 debug_mode = 1;
255 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
256 PR_SetThreadGCAble();
258 #ifdef XP_MAC
259 SetupMacPrintfLog("thrashgc.log");
260 debug_mode = 1;
261 #endif
263 PR_InitGC(0, 0, 0, PR_GLOBAL_THREAD);
264 PR_STDIO_INIT();
265 stderrLock = PR_NewLock();
266 tix1 = PR_RegisterType(&type1);
267 tix2 = PR_RegisterType(&type2);
268 tix3 = PR_RegisterType(&type3);
269 gcInfo = PR_GetGCInfo();
270 rv = padMain(argc, argv);
271 printf("PASS\n");
272 PR_Cleanup();
273 return rv;