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
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.
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 ***** */
40 * Purpose: testing priorities
44 #error "This test does not run on Macintosh"
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
)
74 while (((PRIntervalTime
)(PR_IntervalNow()) - timein
) < oneSecond
)
79 static void PR_CALLBACK
Low(void *arg
)
81 PRUint32 t3
= 0, t2
= 0, t1
= 0, t0
, *tn
= (PRUint32
*)arg
;
84 t0
= PerSecond(PR_IntervalNow());
85 *tn
= (t3
+ 3 * t2
+ 3 * t1
+ t0
) / 8;
86 t3
= t2
; t2
= t1
; t1
= t0
;
90 static void PR_CALLBACK
High(void *arg
)
92 PRUint32 t3
= 0, t2
= 0, t1
= 0, t0
, *tn
= (PRUint32
*)arg
;
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
;
103 static void Help(void)
106 debug_out
, "Usage: priotest [-d] [-c n]\n");
108 debug_out
, "-c n\tduration of test in seconds (default: %d)\n", DEFAULT_DURATION
);
110 debug_out
, "-d\tturn on debugging output (default: FALSE)\n");
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
)
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;
178 case 'd': /* debug mode */
179 debug_mode
= PR_TRUE
;
181 case 'c': /* test duration */
182 duration
= atoi(opt
->value
);
184 case 'h': /* help message */
190 PL_DestroyOptState(opt
);
193 if (duration
== 0) duration
= DEFAULT_DURATION
;
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
);
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
);
218 while (loop
--) PR_Sleep(oneSecond
);
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 */
231 #endif /* ifdef XP_MAC */