michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* interval.cpp - a test program */ michael@0: michael@0: #include "rclock.h" michael@0: #include "rcthread.h" michael@0: #include "rcinrval.h" michael@0: #include "rccv.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define DEFAULT_ITERATIONS 100 michael@0: michael@0: PRIntn main(PRIntn argc, char **argv) michael@0: { michael@0: RCLock ml; michael@0: PRStatus rv; michael@0: RCCondition cv(&ml); michael@0: michael@0: RCInterval now, timeout, epoch, elapsed; michael@0: PRFileDesc *output = PR_GetSpecialFD(PR_StandardOutput); michael@0: PRIntn msecs, seconds, loops, iterations = DEFAULT_ITERATIONS; michael@0: michael@0: /* slow, agonizing waits */ michael@0: for (seconds = 0; seconds < 10; ++seconds) michael@0: { michael@0: timeout = RCInterval::FromSeconds(seconds); michael@0: cv.SetTimeout(timeout); michael@0: { michael@0: RCEnter lock(&ml); michael@0: michael@0: epoch.SetToNow(); michael@0: michael@0: rv = cv.Wait(); michael@0: PR_ASSERT(PR_SUCCESS == rv); michael@0: michael@0: now = RCInterval(RCInterval::now); michael@0: elapsed = now - epoch; michael@0: } michael@0: michael@0: PR_fprintf( michael@0: output, "Waiting %u seconds took %s%u milliseconds\n", michael@0: seconds, ((elapsed < timeout)? "**" : ""), michael@0: elapsed.ToMilliseconds()); michael@0: } michael@0: michael@0: /* more slow, agonizing sleeps */ michael@0: for (seconds = 0; seconds < 10; ++seconds) michael@0: { michael@0: timeout = RCInterval::FromSeconds(seconds); michael@0: { michael@0: epoch.SetToNow(); michael@0: michael@0: rv = RCThread::Sleep(timeout); michael@0: PR_ASSERT(PR_SUCCESS == rv); michael@0: michael@0: now = RCInterval(RCInterval::now); michael@0: elapsed = now - epoch; michael@0: } michael@0: michael@0: PR_fprintf( michael@0: output, "Sleeping %u seconds took %s%u milliseconds\n", michael@0: seconds, ((elapsed < timeout)? "**" : ""), michael@0: elapsed.ToMilliseconds()); michael@0: } michael@0: michael@0: /* fast, spritely little devils */ michael@0: for (msecs = 10; msecs < 100; msecs += 10) michael@0: { michael@0: timeout = RCInterval::FromMilliseconds(msecs); michael@0: cv.SetTimeout(timeout); michael@0: { michael@0: RCEnter lock(&ml); michael@0: michael@0: epoch.SetToNow(); michael@0: michael@0: for (loops = 0; loops < iterations; ++loops) michael@0: { michael@0: rv = cv.Wait(); michael@0: PR_ASSERT(PR_SUCCESS == rv); michael@0: } michael@0: michael@0: now = RCInterval(RCInterval::now); michael@0: elapsed = now - epoch; michael@0: } michael@0: elapsed /= iterations; michael@0: michael@0: PR_fprintf( michael@0: output, "Waiting %u msecs took %s%u milliseconds average\n", michael@0: msecs, ((elapsed < timeout)? "**" : ""), elapsed.ToMilliseconds()); michael@0: } michael@0: return 0; michael@0: } /* main */ michael@0: michael@0: /* interval.cpp */ michael@0: