Bug 462294 - Add "View Video" to context menu for <video> elements. r=gavin, ui...
[wine-gecko.git] / nsprpub / pr / tests / priotest.c
blob16def03d15a12f54a688fe04c3c3e294ee6f0f25
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 ***** */
39 * File: priotest.c
40 * Purpose: testing priorities
43 #ifdef XP_MAC
44 #error "This test does not run on Macintosh"
45 #else
48 #include "prcmon.h"
49 #include "prinit.h"
50 #include "prinrval.h"
51 #include "prlock.h"
52 #include "prlog.h"
53 #include "prmon.h"
54 #include "prprf.h"
55 #include "prthread.h"
56 #include "prtypes.h"
58 #include "plerror.h"
59 #include "plgetopt.h"
61 #include <stdio.h>
62 #include <stdlib.h>
64 #define DEFAULT_DURATION 5
66 static PRBool failed = PR_FALSE;
67 static PRIntervalTime oneSecond;
68 static PRFileDesc *debug_out = NULL;
69 static PRBool debug_mode = PR_FALSE;
71 static PRUint32 PerSecond(PRIntervalTime timein)
73 PRUint32 loop = 0;
74 while (((PRIntervalTime)(PR_IntervalNow()) - timein) < oneSecond)
75 loop += 1;
76 return loop;
77 } /* PerSecond */
79 static void PR_CALLBACK Low(void *arg)
81 PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg;
82 while (1)
84 t0 = PerSecond(PR_IntervalNow());
85 *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8;
86 t3 = t2; t2 = t1; t1 = t0;
88 } /* Low */
90 static void PR_CALLBACK High(void *arg)
92 PRUint32 t3 = 0, t2 = 0, t1 = 0, t0, *tn = (PRUint32*)arg;
93 while (1)
95 PRIntervalTime timein = PR_IntervalNow();
96 PR_Sleep(oneSecond >> 2); /* 0.25 seconds */
97 t0 = PerSecond(timein);
98 *tn = (t3 + 3 * t2 + 3 * t1 + t0) / 8;
99 t3 = t2; t2 = t1; t1 = t0;
101 } /* High */
103 static void Help(void)
105 PR_fprintf(
106 debug_out, "Usage: priotest [-d] [-c n]\n");
107 PR_fprintf(
108 debug_out, "-c n\tduration of test in seconds (default: %d)\n", DEFAULT_DURATION);
109 PR_fprintf(
110 debug_out, "-d\tturn on debugging output (default: FALSE)\n");
111 } /* Help */
113 static void RudimentaryTests(void)
116 ** Try some rudimentary tests like setting valid priority and
117 ** getting it back, or setting invalid priorities and getting
118 ** back a valid answer.
120 PRThreadPriority priority;
121 PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT);
122 priority = PR_GetThreadPriority(PR_GetCurrentThread());
123 failed = ((PR_TRUE == failed) || (PR_PRIORITY_URGENT != priority))
124 ? PR_TRUE : PR_FALSE;
125 if (debug_mode && (PR_PRIORITY_URGENT != priority))
127 PR_fprintf(debug_out, "PR_[S/G]etThreadPriority() failed\n");
131 PR_SetThreadPriority(
132 PR_GetCurrentThread(), (PRThreadPriority)(PR_PRIORITY_FIRST - 1));
133 priority = PR_GetThreadPriority(PR_GetCurrentThread());
134 failed = ((PR_TRUE == failed) || (PR_PRIORITY_FIRST != priority))
135 ? PR_TRUE : PR_FALSE;
136 if (debug_mode && (PR_PRIORITY_FIRST != priority))
138 PR_fprintf(debug_out, "PR_SetThreadPriority(-1) failed\n");
141 PR_SetThreadPriority(
142 PR_GetCurrentThread(), (PRThreadPriority)(PR_PRIORITY_LAST + 1));
143 priority = PR_GetThreadPriority(PR_GetCurrentThread());
144 failed = ((PR_TRUE == failed) || (PR_PRIORITY_LAST != priority))
145 ? PR_TRUE : PR_FALSE;
146 if (debug_mode && (PR_PRIORITY_LAST != priority))
148 PR_fprintf(debug_out, "PR_SetThreadPriority(+1) failed\n");
151 } /* RudimentataryTests */
153 static void CreateThreads(PRUint32 *lowCount, PRUint32 *highCount)
155 (void)PR_CreateThread(
156 PR_USER_THREAD, Low, lowCount, PR_PRIORITY_LOW,
157 PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
158 (void)PR_CreateThread(
159 PR_USER_THREAD, High, highCount, PR_PRIORITY_HIGH,
160 PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
161 } /* CreateThreads */
163 PRIntn main(PRIntn argc, char **argv)
165 PLOptStatus os;
166 PRIntn duration = DEFAULT_DURATION;
167 PRUint32 totalCount, highCount = 0, lowCount = 0;
168 PLOptState *opt = PL_CreateOptState(argc, argv, "hdc:");
170 debug_out = PR_STDOUT;
171 oneSecond = PR_SecondsToInterval(1);
173 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
175 if (PL_OPT_BAD == os) continue;
176 switch (opt->option)
178 case 'd': /* debug mode */
179 debug_mode = PR_TRUE;
180 break;
181 case 'c': /* test duration */
182 duration = atoi(opt->value);
183 break;
184 case 'h': /* help message */
185 default:
186 Help();
187 return 2;
190 PL_DestroyOptState(opt);
191 PR_STDIO_INIT();
193 if (duration == 0) duration = DEFAULT_DURATION;
195 RudimentaryTests();
197 printf("Priority test: running for %d seconds\n\n", duration);
199 (void)PerSecond(PR_IntervalNow());
200 totalCount = PerSecond(PR_IntervalNow());
202 PR_SetThreadPriority(PR_GetCurrentThread(), PR_PRIORITY_URGENT);
204 if (debug_mode)
206 PR_fprintf(debug_out,
207 "The high priority thread should get approximately three\n");
208 PR_fprintf( debug_out,
209 "times what the low priority thread manages. A maximum of \n");
210 PR_fprintf( debug_out, "%d cycles are available.\n\n", totalCount);
213 duration = (duration + 4) / 5;
214 CreateThreads(&lowCount, &highCount);
215 while (duration--)
217 PRIntn loop = 5;
218 while (loop--) PR_Sleep(oneSecond);
219 if (debug_mode)
220 PR_fprintf(debug_out, "high : low :: %d : %d\n", highCount, lowCount);
224 PR_ProcessExit((failed) ? 1 : 0);
226 PR_ASSERT(!"You can't get here -- but you did!");
227 return 1; /* or here */
229 } /* main */
231 #endif /* ifdef XP_MAC */
233 /* priotest.c */