Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * Test: y2ktmo |
michael@0 | 8 | * |
michael@0 | 9 | * Description: |
michael@0 | 10 | * This test tests the interval time facilities in NSPR for Y2K |
michael@0 | 11 | * compliance. All the functions that take a timeout argument |
michael@0 | 12 | * are tested: PR_Sleep, socket I/O (PR_Accept is taken as a |
michael@0 | 13 | * representative), PR_Poll, PR_WaitCondVar, PR_Wait, and |
michael@0 | 14 | * PR_CWait. A thread of each thread scope (local, global, and |
michael@0 | 15 | * global bound) is created to call each of these functions. |
michael@0 | 16 | * The test should be started at the specified number of seconds |
michael@0 | 17 | * (called the lead time) before a Y2K rollover test date. The |
michael@0 | 18 | * timeout values for these threads will span over the rollover |
michael@0 | 19 | * date by at least the specified number of seconds. For |
michael@0 | 20 | * example, if the lead time is 5 seconds, the test should |
michael@0 | 21 | * be started at time (D - 5), where D is a rollover date, and |
michael@0 | 22 | * the threads will time out at or after time (D + 5). The |
michael@0 | 23 | * timeout values for the threads are spaced one second apart. |
michael@0 | 24 | * |
michael@0 | 25 | * When a thread times out, it calls PR_IntervalNow() to verify |
michael@0 | 26 | * that it did wait for the specified time. In addition, it |
michael@0 | 27 | * calls a platform-native function to verify the actual elapsed |
michael@0 | 28 | * time again, to rule out the possibility that PR_IntervalNow() |
michael@0 | 29 | * is broken. We allow the actual elapsed time to deviate from |
michael@0 | 30 | * the specified timeout by a certain tolerance (in milliseconds). |
michael@0 | 31 | */ |
michael@0 | 32 | |
michael@0 | 33 | #include "nspr.h" |
michael@0 | 34 | #include "plgetopt.h" |
michael@0 | 35 | |
michael@0 | 36 | #include <stdio.h> |
michael@0 | 37 | #include <stdlib.h> |
michael@0 | 38 | #include <string.h> |
michael@0 | 39 | #if defined(XP_UNIX) |
michael@0 | 40 | #include <sys/time.h> /* for gettimeofday */ |
michael@0 | 41 | #endif |
michael@0 | 42 | #if defined(WIN32) |
michael@0 | 43 | #if defined(WINCE) |
michael@0 | 44 | #include <windows.h> |
michael@0 | 45 | #else |
michael@0 | 46 | #include <sys/types.h> |
michael@0 | 47 | #include <sys/timeb.h> /* for _ftime */ |
michael@0 | 48 | #endif |
michael@0 | 49 | #endif |
michael@0 | 50 | |
michael@0 | 51 | #define DEFAULT_LEAD_TIME_SECS 5 |
michael@0 | 52 | #define DEFAULT_TOLERANCE_MSECS 500 |
michael@0 | 53 | |
michael@0 | 54 | static PRBool debug_mode = PR_FALSE; |
michael@0 | 55 | static PRInt32 lead_time_secs = DEFAULT_LEAD_TIME_SECS; |
michael@0 | 56 | static PRInt32 tolerance_msecs = DEFAULT_TOLERANCE_MSECS; |
michael@0 | 57 | static PRIntervalTime start_time; |
michael@0 | 58 | static PRIntervalTime tolerance; |
michael@0 | 59 | |
michael@0 | 60 | #if defined(XP_UNIX) |
michael@0 | 61 | static struct timeval start_time_tv; |
michael@0 | 62 | #endif |
michael@0 | 63 | #if defined(WIN32) |
michael@0 | 64 | #if defined(WINCE) |
michael@0 | 65 | static DWORD start_time_tick; |
michael@0 | 66 | #else |
michael@0 | 67 | static struct _timeb start_time_tb; |
michael@0 | 68 | #endif |
michael@0 | 69 | #endif |
michael@0 | 70 | |
michael@0 | 71 | static void SleepThread(void *arg) |
michael@0 | 72 | { |
michael@0 | 73 | PRIntervalTime timeout = (PRIntervalTime) arg; |
michael@0 | 74 | PRIntervalTime elapsed; |
michael@0 | 75 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 76 | PRInt32 timeout_msecs = PR_IntervalToMilliseconds(timeout); |
michael@0 | 77 | PRInt32 elapsed_msecs; |
michael@0 | 78 | #endif |
michael@0 | 79 | #if defined(XP_UNIX) |
michael@0 | 80 | struct timeval end_time_tv; |
michael@0 | 81 | #endif |
michael@0 | 82 | #if defined(WIN32) && !defined(WINCE) |
michael@0 | 83 | struct _timeb end_time_tb; |
michael@0 | 84 | #endif |
michael@0 | 85 | |
michael@0 | 86 | if (PR_Sleep(timeout) == PR_FAILURE) { |
michael@0 | 87 | fprintf(stderr, "PR_Sleep failed\n"); |
michael@0 | 88 | exit(1); |
michael@0 | 89 | } |
michael@0 | 90 | elapsed = (PRIntervalTime)(PR_IntervalNow() - start_time); |
michael@0 | 91 | if (elapsed + tolerance < timeout || elapsed > timeout + tolerance) { |
michael@0 | 92 | fprintf(stderr, "timeout wrong\n"); |
michael@0 | 93 | exit(1); |
michael@0 | 94 | } |
michael@0 | 95 | #if defined(XP_UNIX) |
michael@0 | 96 | gettimeofday(&end_time_tv, NULL); |
michael@0 | 97 | elapsed_msecs = 1000*(end_time_tv.tv_sec - start_time_tv.tv_sec) |
michael@0 | 98 | + (end_time_tv.tv_usec - start_time_tv.tv_usec)/1000; |
michael@0 | 99 | #endif |
michael@0 | 100 | #if defined(WIN32) |
michael@0 | 101 | #if defined(WINCE) |
michael@0 | 102 | elapsed_msecs = GetTickCount() - start_time_tick; |
michael@0 | 103 | #else |
michael@0 | 104 | _ftime(&end_time_tb); |
michael@0 | 105 | elapsed_msecs = 1000*(end_time_tb.time - start_time_tb.time) |
michael@0 | 106 | + (end_time_tb.millitm - start_time_tb.millitm); |
michael@0 | 107 | #endif |
michael@0 | 108 | #endif |
michael@0 | 109 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 110 | if (elapsed_msecs + tolerance_msecs < timeout_msecs |
michael@0 | 111 | || elapsed_msecs > timeout_msecs + tolerance_msecs) { |
michael@0 | 112 | fprintf(stderr, "timeout wrong\n"); |
michael@0 | 113 | exit(1); |
michael@0 | 114 | } |
michael@0 | 115 | #endif |
michael@0 | 116 | if (debug_mode) { |
michael@0 | 117 | fprintf(stderr, "Sleep thread (scope %d) done\n", |
michael@0 | 118 | PR_GetThreadScope(PR_GetCurrentThread())); |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | static void AcceptThread(void *arg) |
michael@0 | 123 | { |
michael@0 | 124 | PRIntervalTime timeout = (PRIntervalTime) arg; |
michael@0 | 125 | PRIntervalTime elapsed; |
michael@0 | 126 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 127 | PRInt32 timeout_msecs = PR_IntervalToMilliseconds(timeout); |
michael@0 | 128 | PRInt32 elapsed_msecs; |
michael@0 | 129 | #endif |
michael@0 | 130 | #if defined(XP_UNIX) |
michael@0 | 131 | struct timeval end_time_tv; |
michael@0 | 132 | #endif |
michael@0 | 133 | #if defined(WIN32) && !defined(WINCE) |
michael@0 | 134 | struct _timeb end_time_tb; |
michael@0 | 135 | #endif |
michael@0 | 136 | PRFileDesc *sock; |
michael@0 | 137 | PRNetAddr addr; |
michael@0 | 138 | PRFileDesc *accepted; |
michael@0 | 139 | |
michael@0 | 140 | sock = PR_NewTCPSocket(); |
michael@0 | 141 | if (sock == NULL) { |
michael@0 | 142 | fprintf(stderr, "PR_NewTCPSocket failed\n"); |
michael@0 | 143 | exit(1); |
michael@0 | 144 | } |
michael@0 | 145 | memset(&addr, 0, sizeof(addr)); |
michael@0 | 146 | addr.inet.family = PR_AF_INET; |
michael@0 | 147 | addr.inet.port = 0; |
michael@0 | 148 | addr.inet.ip = PR_htonl(PR_INADDR_ANY); |
michael@0 | 149 | if (PR_Bind(sock, &addr) == PR_FAILURE) { |
michael@0 | 150 | fprintf(stderr, "PR_Bind failed\n"); |
michael@0 | 151 | exit(1); |
michael@0 | 152 | } |
michael@0 | 153 | if (PR_Listen(sock, 5) == PR_FAILURE) { |
michael@0 | 154 | fprintf(stderr, "PR_Listen failed\n"); |
michael@0 | 155 | exit(1); |
michael@0 | 156 | } |
michael@0 | 157 | accepted = PR_Accept(sock, NULL, timeout); |
michael@0 | 158 | if (accepted != NULL || PR_GetError() != PR_IO_TIMEOUT_ERROR) { |
michael@0 | 159 | fprintf(stderr, "PR_Accept did not time out\n"); |
michael@0 | 160 | exit(1); |
michael@0 | 161 | } |
michael@0 | 162 | elapsed = (PRIntervalTime)(PR_IntervalNow() - start_time); |
michael@0 | 163 | if (elapsed + tolerance < timeout || elapsed > timeout + tolerance) { |
michael@0 | 164 | fprintf(stderr, "timeout wrong\n"); |
michael@0 | 165 | exit(1); |
michael@0 | 166 | } |
michael@0 | 167 | #if defined(XP_UNIX) |
michael@0 | 168 | gettimeofday(&end_time_tv, NULL); |
michael@0 | 169 | elapsed_msecs = 1000*(end_time_tv.tv_sec - start_time_tv.tv_sec) |
michael@0 | 170 | + (end_time_tv.tv_usec - start_time_tv.tv_usec)/1000; |
michael@0 | 171 | #endif |
michael@0 | 172 | #if defined(WIN32) |
michael@0 | 173 | #if defined(WINCE) |
michael@0 | 174 | elapsed_msecs = GetTickCount() - start_time_tick; |
michael@0 | 175 | #else |
michael@0 | 176 | _ftime(&end_time_tb); |
michael@0 | 177 | elapsed_msecs = 1000*(end_time_tb.time - start_time_tb.time) |
michael@0 | 178 | + (end_time_tb.millitm - start_time_tb.millitm); |
michael@0 | 179 | #endif |
michael@0 | 180 | #endif |
michael@0 | 181 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 182 | if (elapsed_msecs + tolerance_msecs < timeout_msecs |
michael@0 | 183 | || elapsed_msecs > timeout_msecs + tolerance_msecs) { |
michael@0 | 184 | fprintf(stderr, "timeout wrong\n"); |
michael@0 | 185 | exit(1); |
michael@0 | 186 | } |
michael@0 | 187 | #endif |
michael@0 | 188 | if (PR_Close(sock) == PR_FAILURE) { |
michael@0 | 189 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 190 | exit(1); |
michael@0 | 191 | } |
michael@0 | 192 | if (debug_mode) { |
michael@0 | 193 | fprintf(stderr, "Accept thread (scope %d) done\n", |
michael@0 | 194 | PR_GetThreadScope(PR_GetCurrentThread())); |
michael@0 | 195 | } |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | static void PollThread(void *arg) |
michael@0 | 199 | { |
michael@0 | 200 | PRIntervalTime timeout = (PRIntervalTime) arg; |
michael@0 | 201 | PRIntervalTime elapsed; |
michael@0 | 202 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 203 | PRInt32 timeout_msecs = PR_IntervalToMilliseconds(timeout); |
michael@0 | 204 | PRInt32 elapsed_msecs; |
michael@0 | 205 | #endif |
michael@0 | 206 | #if defined(XP_UNIX) |
michael@0 | 207 | struct timeval end_time_tv; |
michael@0 | 208 | #endif |
michael@0 | 209 | #if defined(WIN32) && !defined(WINCE) |
michael@0 | 210 | struct _timeb end_time_tb; |
michael@0 | 211 | #endif |
michael@0 | 212 | PRFileDesc *sock; |
michael@0 | 213 | PRNetAddr addr; |
michael@0 | 214 | PRPollDesc pd; |
michael@0 | 215 | PRIntn rv; |
michael@0 | 216 | |
michael@0 | 217 | sock = PR_NewTCPSocket(); |
michael@0 | 218 | if (sock == NULL) { |
michael@0 | 219 | fprintf(stderr, "PR_NewTCPSocket failed\n"); |
michael@0 | 220 | exit(1); |
michael@0 | 221 | } |
michael@0 | 222 | memset(&addr, 0, sizeof(addr)); |
michael@0 | 223 | addr.inet.family = PR_AF_INET; |
michael@0 | 224 | addr.inet.port = 0; |
michael@0 | 225 | addr.inet.ip = PR_htonl(PR_INADDR_ANY); |
michael@0 | 226 | if (PR_Bind(sock, &addr) == PR_FAILURE) { |
michael@0 | 227 | fprintf(stderr, "PR_Bind failed\n"); |
michael@0 | 228 | exit(1); |
michael@0 | 229 | } |
michael@0 | 230 | if (PR_Listen(sock, 5) == PR_FAILURE) { |
michael@0 | 231 | fprintf(stderr, "PR_Listen failed\n"); |
michael@0 | 232 | exit(1); |
michael@0 | 233 | } |
michael@0 | 234 | pd.fd = sock; |
michael@0 | 235 | pd.in_flags = PR_POLL_READ; |
michael@0 | 236 | rv = PR_Poll(&pd, 1, timeout); |
michael@0 | 237 | if (rv != 0) { |
michael@0 | 238 | fprintf(stderr, "PR_Poll did not time out\n"); |
michael@0 | 239 | exit(1); |
michael@0 | 240 | } |
michael@0 | 241 | elapsed = (PRIntervalTime)(PR_IntervalNow() - start_time); |
michael@0 | 242 | if (elapsed + tolerance < timeout || elapsed > timeout + tolerance) { |
michael@0 | 243 | fprintf(stderr, "timeout wrong\n"); |
michael@0 | 244 | exit(1); |
michael@0 | 245 | } |
michael@0 | 246 | #if defined(XP_UNIX) |
michael@0 | 247 | gettimeofday(&end_time_tv, NULL); |
michael@0 | 248 | elapsed_msecs = 1000*(end_time_tv.tv_sec - start_time_tv.tv_sec) |
michael@0 | 249 | + (end_time_tv.tv_usec - start_time_tv.tv_usec)/1000; |
michael@0 | 250 | #endif |
michael@0 | 251 | #if defined(WIN32) |
michael@0 | 252 | #if defined(WINCE) |
michael@0 | 253 | elapsed_msecs = GetTickCount() - start_time_tick; |
michael@0 | 254 | #else |
michael@0 | 255 | _ftime(&end_time_tb); |
michael@0 | 256 | elapsed_msecs = 1000*(end_time_tb.time - start_time_tb.time) |
michael@0 | 257 | + (end_time_tb.millitm - start_time_tb.millitm); |
michael@0 | 258 | #endif |
michael@0 | 259 | #endif |
michael@0 | 260 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 261 | if (elapsed_msecs + tolerance_msecs < timeout_msecs |
michael@0 | 262 | || elapsed_msecs > timeout_msecs + tolerance_msecs) { |
michael@0 | 263 | fprintf(stderr, "timeout wrong\n"); |
michael@0 | 264 | exit(1); |
michael@0 | 265 | } |
michael@0 | 266 | #endif |
michael@0 | 267 | if (PR_Close(sock) == PR_FAILURE) { |
michael@0 | 268 | fprintf(stderr, "PR_Close failed\n"); |
michael@0 | 269 | exit(1); |
michael@0 | 270 | } |
michael@0 | 271 | if (debug_mode) { |
michael@0 | 272 | fprintf(stderr, "Poll thread (scope %d) done\n", |
michael@0 | 273 | PR_GetThreadScope(PR_GetCurrentThread())); |
michael@0 | 274 | } |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | static void WaitCondVarThread(void *arg) |
michael@0 | 278 | { |
michael@0 | 279 | PRIntervalTime timeout = (PRIntervalTime) arg; |
michael@0 | 280 | PRIntervalTime elapsed; |
michael@0 | 281 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 282 | PRInt32 timeout_msecs = PR_IntervalToMilliseconds(timeout); |
michael@0 | 283 | PRInt32 elapsed_msecs; |
michael@0 | 284 | #endif |
michael@0 | 285 | #if defined(XP_UNIX) |
michael@0 | 286 | struct timeval end_time_tv; |
michael@0 | 287 | #endif |
michael@0 | 288 | #if defined(WIN32) && !defined(WINCE) |
michael@0 | 289 | struct _timeb end_time_tb; |
michael@0 | 290 | #endif |
michael@0 | 291 | PRLock *ml; |
michael@0 | 292 | PRCondVar *cv; |
michael@0 | 293 | |
michael@0 | 294 | ml = PR_NewLock(); |
michael@0 | 295 | if (ml == NULL) { |
michael@0 | 296 | fprintf(stderr, "PR_NewLock failed\n"); |
michael@0 | 297 | exit(1); |
michael@0 | 298 | } |
michael@0 | 299 | cv = PR_NewCondVar(ml); |
michael@0 | 300 | if (cv == NULL) { |
michael@0 | 301 | fprintf(stderr, "PR_NewCondVar failed\n"); |
michael@0 | 302 | exit(1); |
michael@0 | 303 | } |
michael@0 | 304 | PR_Lock(ml); |
michael@0 | 305 | PR_WaitCondVar(cv, timeout); |
michael@0 | 306 | PR_Unlock(ml); |
michael@0 | 307 | elapsed = (PRIntervalTime)(PR_IntervalNow() - start_time); |
michael@0 | 308 | if (elapsed + tolerance < timeout || elapsed > timeout + tolerance) { |
michael@0 | 309 | fprintf(stderr, "timeout wrong\n"); |
michael@0 | 310 | exit(1); |
michael@0 | 311 | } |
michael@0 | 312 | #if defined(XP_UNIX) |
michael@0 | 313 | gettimeofday(&end_time_tv, NULL); |
michael@0 | 314 | elapsed_msecs = 1000*(end_time_tv.tv_sec - start_time_tv.tv_sec) |
michael@0 | 315 | + (end_time_tv.tv_usec - start_time_tv.tv_usec)/1000; |
michael@0 | 316 | #endif |
michael@0 | 317 | #if defined(WIN32) |
michael@0 | 318 | #if defined(WINCE) |
michael@0 | 319 | elapsed_msecs = GetTickCount() - start_time_tick; |
michael@0 | 320 | #else |
michael@0 | 321 | _ftime(&end_time_tb); |
michael@0 | 322 | elapsed_msecs = 1000*(end_time_tb.time - start_time_tb.time) |
michael@0 | 323 | + (end_time_tb.millitm - start_time_tb.millitm); |
michael@0 | 324 | #endif |
michael@0 | 325 | #endif |
michael@0 | 326 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 327 | if (elapsed_msecs + tolerance_msecs < timeout_msecs |
michael@0 | 328 | || elapsed_msecs > timeout_msecs + tolerance_msecs) { |
michael@0 | 329 | fprintf(stderr, "timeout wrong\n"); |
michael@0 | 330 | exit(1); |
michael@0 | 331 | } |
michael@0 | 332 | #endif |
michael@0 | 333 | PR_DestroyCondVar(cv); |
michael@0 | 334 | PR_DestroyLock(ml); |
michael@0 | 335 | if (debug_mode) { |
michael@0 | 336 | fprintf(stderr, "wait cond var thread (scope %d) done\n", |
michael@0 | 337 | PR_GetThreadScope(PR_GetCurrentThread())); |
michael@0 | 338 | } |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | static void WaitMonitorThread(void *arg) |
michael@0 | 342 | { |
michael@0 | 343 | PRIntervalTime timeout = (PRIntervalTime) arg; |
michael@0 | 344 | PRIntervalTime elapsed; |
michael@0 | 345 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 346 | PRInt32 timeout_msecs = PR_IntervalToMilliseconds(timeout); |
michael@0 | 347 | PRInt32 elapsed_msecs; |
michael@0 | 348 | #endif |
michael@0 | 349 | #if defined(XP_UNIX) |
michael@0 | 350 | struct timeval end_time_tv; |
michael@0 | 351 | #endif |
michael@0 | 352 | #if defined(WIN32) && !defined(WINCE) |
michael@0 | 353 | struct _timeb end_time_tb; |
michael@0 | 354 | #endif |
michael@0 | 355 | PRMonitor *mon; |
michael@0 | 356 | |
michael@0 | 357 | mon = PR_NewMonitor(); |
michael@0 | 358 | if (mon == NULL) { |
michael@0 | 359 | fprintf(stderr, "PR_NewMonitor failed\n"); |
michael@0 | 360 | exit(1); |
michael@0 | 361 | } |
michael@0 | 362 | PR_EnterMonitor(mon); |
michael@0 | 363 | PR_Wait(mon, timeout); |
michael@0 | 364 | PR_ExitMonitor(mon); |
michael@0 | 365 | elapsed = (PRIntervalTime)(PR_IntervalNow() - start_time); |
michael@0 | 366 | if (elapsed + tolerance < timeout || elapsed > timeout + tolerance) { |
michael@0 | 367 | fprintf(stderr, "timeout wrong\n"); |
michael@0 | 368 | exit(1); |
michael@0 | 369 | } |
michael@0 | 370 | #if defined(XP_UNIX) |
michael@0 | 371 | gettimeofday(&end_time_tv, NULL); |
michael@0 | 372 | elapsed_msecs = 1000*(end_time_tv.tv_sec - start_time_tv.tv_sec) |
michael@0 | 373 | + (end_time_tv.tv_usec - start_time_tv.tv_usec)/1000; |
michael@0 | 374 | #endif |
michael@0 | 375 | #if defined(WIN32) |
michael@0 | 376 | #if defined(WINCE) |
michael@0 | 377 | elapsed_msecs = GetTickCount() - start_time_tick; |
michael@0 | 378 | #else |
michael@0 | 379 | _ftime(&end_time_tb); |
michael@0 | 380 | elapsed_msecs = 1000*(end_time_tb.time - start_time_tb.time) |
michael@0 | 381 | + (end_time_tb.millitm - start_time_tb.millitm); |
michael@0 | 382 | #endif |
michael@0 | 383 | #endif |
michael@0 | 384 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 385 | if (elapsed_msecs + tolerance_msecs < timeout_msecs |
michael@0 | 386 | || elapsed_msecs > timeout_msecs + tolerance_msecs) { |
michael@0 | 387 | fprintf(stderr, "timeout wrong\n"); |
michael@0 | 388 | exit(1); |
michael@0 | 389 | } |
michael@0 | 390 | #endif |
michael@0 | 391 | PR_DestroyMonitor(mon); |
michael@0 | 392 | if (debug_mode) { |
michael@0 | 393 | fprintf(stderr, "wait monitor thread (scope %d) done\n", |
michael@0 | 394 | PR_GetThreadScope(PR_GetCurrentThread())); |
michael@0 | 395 | } |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | static void WaitCMonitorThread(void *arg) |
michael@0 | 399 | { |
michael@0 | 400 | PRIntervalTime timeout = (PRIntervalTime) arg; |
michael@0 | 401 | PRIntervalTime elapsed; |
michael@0 | 402 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 403 | PRInt32 timeout_msecs = PR_IntervalToMilliseconds(timeout); |
michael@0 | 404 | PRInt32 elapsed_msecs; |
michael@0 | 405 | #endif |
michael@0 | 406 | #if defined(XP_UNIX) |
michael@0 | 407 | struct timeval end_time_tv; |
michael@0 | 408 | #endif |
michael@0 | 409 | #if defined(WIN32) && !defined(WINCE) |
michael@0 | 410 | struct _timeb end_time_tb; |
michael@0 | 411 | #endif |
michael@0 | 412 | int dummy; |
michael@0 | 413 | |
michael@0 | 414 | PR_CEnterMonitor(&dummy); |
michael@0 | 415 | PR_CWait(&dummy, timeout); |
michael@0 | 416 | PR_CExitMonitor(&dummy); |
michael@0 | 417 | elapsed = (PRIntervalTime)(PR_IntervalNow() - start_time); |
michael@0 | 418 | if (elapsed + tolerance < timeout || elapsed > timeout + tolerance) { |
michael@0 | 419 | fprintf(stderr, "timeout wrong\n"); |
michael@0 | 420 | exit(1); |
michael@0 | 421 | } |
michael@0 | 422 | #if defined(XP_UNIX) |
michael@0 | 423 | gettimeofday(&end_time_tv, NULL); |
michael@0 | 424 | elapsed_msecs = 1000*(end_time_tv.tv_sec - start_time_tv.tv_sec) |
michael@0 | 425 | + (end_time_tv.tv_usec - start_time_tv.tv_usec)/1000; |
michael@0 | 426 | #endif |
michael@0 | 427 | #if defined(WIN32) |
michael@0 | 428 | #if defined(WINCE) |
michael@0 | 429 | elapsed_msecs = GetTickCount() - start_time_tick; |
michael@0 | 430 | #else |
michael@0 | 431 | _ftime(&end_time_tb); |
michael@0 | 432 | elapsed_msecs = 1000*(end_time_tb.time - start_time_tb.time) |
michael@0 | 433 | + (end_time_tb.millitm - start_time_tb.millitm); |
michael@0 | 434 | #endif |
michael@0 | 435 | #endif |
michael@0 | 436 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 437 | if (elapsed_msecs + tolerance_msecs < timeout_msecs |
michael@0 | 438 | || elapsed_msecs > timeout_msecs + tolerance_msecs) { |
michael@0 | 439 | fprintf(stderr, "timeout wrong\n"); |
michael@0 | 440 | exit(1); |
michael@0 | 441 | } |
michael@0 | 442 | #endif |
michael@0 | 443 | if (debug_mode) { |
michael@0 | 444 | fprintf(stderr, "wait cached monitor thread (scope %d) done\n", |
michael@0 | 445 | PR_GetThreadScope(PR_GetCurrentThread())); |
michael@0 | 446 | } |
michael@0 | 447 | } |
michael@0 | 448 | |
michael@0 | 449 | typedef void (*NSPRThreadFunc)(void*); |
michael@0 | 450 | |
michael@0 | 451 | static NSPRThreadFunc threadFuncs[] = { |
michael@0 | 452 | SleepThread, AcceptThread, PollThread, |
michael@0 | 453 | WaitCondVarThread, WaitMonitorThread, WaitCMonitorThread}; |
michael@0 | 454 | |
michael@0 | 455 | static PRThreadScope threadScopes[] = { |
michael@0 | 456 | PR_LOCAL_THREAD, PR_GLOBAL_THREAD, PR_GLOBAL_BOUND_THREAD}; |
michael@0 | 457 | |
michael@0 | 458 | static void Help(void) |
michael@0 | 459 | { |
michael@0 | 460 | fprintf(stderr, "y2ktmo test program usage:\n"); |
michael@0 | 461 | fprintf(stderr, "\t-d debug mode (FALSE)\n"); |
michael@0 | 462 | fprintf(stderr, "\t-l <secs> lead time (%d)\n", |
michael@0 | 463 | DEFAULT_LEAD_TIME_SECS); |
michael@0 | 464 | fprintf(stderr, "\t-t <msecs> tolerance (%d)\n", |
michael@0 | 465 | DEFAULT_TOLERANCE_MSECS); |
michael@0 | 466 | fprintf(stderr, "\t-h this message\n"); |
michael@0 | 467 | } /* Help */ |
michael@0 | 468 | |
michael@0 | 469 | int main(int argc, char **argv) |
michael@0 | 470 | { |
michael@0 | 471 | PRThread **threads; |
michael@0 | 472 | int num_thread_funcs = sizeof(threadFuncs)/sizeof(NSPRThreadFunc); |
michael@0 | 473 | int num_thread_scopes = sizeof(threadScopes)/sizeof(PRThreadScope); |
michael@0 | 474 | int i, j; |
michael@0 | 475 | int idx; |
michael@0 | 476 | PRInt32 secs; |
michael@0 | 477 | PLOptStatus os; |
michael@0 | 478 | PLOptState *opt = PL_CreateOptState(argc, argv, "dl:t:h"); |
michael@0 | 479 | |
michael@0 | 480 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { |
michael@0 | 481 | if (PL_OPT_BAD == os) continue; |
michael@0 | 482 | switch (opt->option) { |
michael@0 | 483 | case 'd': /* debug mode */ |
michael@0 | 484 | debug_mode = PR_TRUE; |
michael@0 | 485 | break; |
michael@0 | 486 | case 'l': /* lead time */ |
michael@0 | 487 | lead_time_secs = atoi(opt->value); |
michael@0 | 488 | break; |
michael@0 | 489 | case 't': /* tolerance */ |
michael@0 | 490 | tolerance_msecs = atoi(opt->value); |
michael@0 | 491 | break; |
michael@0 | 492 | case 'h': |
michael@0 | 493 | default: |
michael@0 | 494 | Help(); |
michael@0 | 495 | return 2; |
michael@0 | 496 | } |
michael@0 | 497 | } |
michael@0 | 498 | PL_DestroyOptState(opt); |
michael@0 | 499 | |
michael@0 | 500 | if (debug_mode) { |
michael@0 | 501 | fprintf(stderr, "lead time: %d secs\n", lead_time_secs); |
michael@0 | 502 | fprintf(stderr, "tolerance: %d msecs\n", tolerance_msecs); |
michael@0 | 503 | } |
michael@0 | 504 | |
michael@0 | 505 | start_time = PR_IntervalNow(); |
michael@0 | 506 | #if defined(XP_UNIX) |
michael@0 | 507 | gettimeofday(&start_time_tv, NULL); |
michael@0 | 508 | #endif |
michael@0 | 509 | #if defined(WIN32) |
michael@0 | 510 | #ifdef WINCE |
michael@0 | 511 | start_time_tick = GetTickCount(); |
michael@0 | 512 | #else |
michael@0 | 513 | _ftime(&start_time_tb); |
michael@0 | 514 | #endif |
michael@0 | 515 | #endif |
michael@0 | 516 | tolerance = PR_MillisecondsToInterval(tolerance_msecs); |
michael@0 | 517 | |
michael@0 | 518 | threads = PR_Malloc( |
michael@0 | 519 | num_thread_scopes * num_thread_funcs * sizeof(PRThread*)); |
michael@0 | 520 | if (threads == NULL) { |
michael@0 | 521 | fprintf(stderr, "PR_Malloc failed\n"); |
michael@0 | 522 | exit(1); |
michael@0 | 523 | } |
michael@0 | 524 | |
michael@0 | 525 | /* start to time out 5 seconds after a rollover date */ |
michael@0 | 526 | secs = lead_time_secs + 5; |
michael@0 | 527 | idx = 0; |
michael@0 | 528 | for (i = 0; i < num_thread_scopes; i++) { |
michael@0 | 529 | for (j = 0; j < num_thread_funcs; j++) { |
michael@0 | 530 | threads[idx] = PR_CreateThread(PR_USER_THREAD, threadFuncs[j], |
michael@0 | 531 | (void*)PR_SecondsToInterval(secs), PR_PRIORITY_NORMAL, |
michael@0 | 532 | threadScopes[i], PR_JOINABLE_THREAD, 0); |
michael@0 | 533 | if (threads[idx] == NULL) { |
michael@0 | 534 | fprintf(stderr, "PR_CreateThread failed\n"); |
michael@0 | 535 | exit(1); |
michael@0 | 536 | } |
michael@0 | 537 | secs++; |
michael@0 | 538 | idx++; |
michael@0 | 539 | } |
michael@0 | 540 | } |
michael@0 | 541 | for (idx = 0; idx < num_thread_scopes*num_thread_funcs; idx++) { |
michael@0 | 542 | if (PR_JoinThread(threads[idx]) == PR_FAILURE) { |
michael@0 | 543 | fprintf(stderr, "PR_JoinThread failed\n"); |
michael@0 | 544 | exit(1); |
michael@0 | 545 | } |
michael@0 | 546 | } |
michael@0 | 547 | PR_Free(threads); |
michael@0 | 548 | printf("PASS\n"); |
michael@0 | 549 | return 0; |
michael@0 | 550 | } |