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.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* interval.cpp - a test program */
8 #include "rclock.h"
9 #include "rcthread.h"
10 #include "rcinrval.h"
11 #include "rccv.h"
13 #include <prio.h>
14 #include <prlog.h>
15 #include <prprf.h>
17 #define DEFAULT_ITERATIONS 100
19 PRIntn main(PRIntn argc, char **argv)
20 {
21 RCLock ml;
22 PRStatus rv;
23 RCCondition cv(&ml);
25 RCInterval now, timeout, epoch, elapsed;
26 PRFileDesc *output = PR_GetSpecialFD(PR_StandardOutput);
27 PRIntn msecs, seconds, loops, iterations = DEFAULT_ITERATIONS;
29 /* slow, agonizing waits */
30 for (seconds = 0; seconds < 10; ++seconds)
31 {
32 timeout = RCInterval::FromSeconds(seconds);
33 cv.SetTimeout(timeout);
34 {
35 RCEnter lock(&ml);
37 epoch.SetToNow();
39 rv = cv.Wait();
40 PR_ASSERT(PR_SUCCESS == rv);
42 now = RCInterval(RCInterval::now);
43 elapsed = now - epoch;
44 }
46 PR_fprintf(
47 output, "Waiting %u seconds took %s%u milliseconds\n",
48 seconds, ((elapsed < timeout)? "**" : ""),
49 elapsed.ToMilliseconds());
50 }
52 /* more slow, agonizing sleeps */
53 for (seconds = 0; seconds < 10; ++seconds)
54 {
55 timeout = RCInterval::FromSeconds(seconds);
56 {
57 epoch.SetToNow();
59 rv = RCThread::Sleep(timeout);
60 PR_ASSERT(PR_SUCCESS == rv);
62 now = RCInterval(RCInterval::now);
63 elapsed = now - epoch;
64 }
66 PR_fprintf(
67 output, "Sleeping %u seconds took %s%u milliseconds\n",
68 seconds, ((elapsed < timeout)? "**" : ""),
69 elapsed.ToMilliseconds());
70 }
72 /* fast, spritely little devils */
73 for (msecs = 10; msecs < 100; msecs += 10)
74 {
75 timeout = RCInterval::FromMilliseconds(msecs);
76 cv.SetTimeout(timeout);
77 {
78 RCEnter lock(&ml);
80 epoch.SetToNow();
82 for (loops = 0; loops < iterations; ++loops)
83 {
84 rv = cv.Wait();
85 PR_ASSERT(PR_SUCCESS == rv);
86 }
88 now = RCInterval(RCInterval::now);
89 elapsed = now - epoch;
90 }
91 elapsed /= iterations;
93 PR_fprintf(
94 output, "Waiting %u msecs took %s%u milliseconds average\n",
95 msecs, ((elapsed < timeout)? "**" : ""), elapsed.ToMilliseconds());
96 }
97 return 0;
98 } /* main */
100 /* interval.cpp */