tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

interval.cpp (2678B)


      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/. */
      5 
      6 /* interval.cpp - a test program */
      7 
      8 #include "rclock.h"
      9 #include "rcthread.h"
     10 #include "rcinrval.h"
     11 #include "rccv.h"
     12 
     13 #include <prio.h>
     14 #include <prlog.h>
     15 #include <prprf.h>
     16 
     17 #define DEFAULT_ITERATIONS 100
     18 
     19 PRIntn main(PRIntn argc, char **argv)
     20 {
     21    RCLock ml;
     22    PRStatus rv;
     23    RCCondition cv(&ml);
     24 
     25    RCInterval now, timeout, epoch, elapsed;
     26    PRFileDesc *output = PR_GetSpecialFD(PR_StandardOutput);
     27    PRIntn msecs, seconds, loops, iterations = DEFAULT_ITERATIONS;
     28 
     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);
     36 
     37            epoch.SetToNow();
     38 
     39            rv = cv.Wait();
     40            PR_ASSERT(PR_SUCCESS == rv);
     41 
     42            now = RCInterval(RCInterval::now);
     43            elapsed = now - epoch;
     44        }
     45 
     46        PR_fprintf(
     47            output, "Waiting %u seconds took %s%u milliseconds\n",
     48            seconds, ((elapsed < timeout)? "**" : ""),
     49            elapsed.ToMilliseconds());
     50    }
     51 
     52    /* more slow, agonizing sleeps */
     53    for (seconds = 0; seconds < 10; ++seconds)
     54    {
     55        timeout = RCInterval::FromSeconds(seconds);
     56        {
     57            epoch.SetToNow();
     58 
     59            rv = RCThread::Sleep(timeout);
     60            PR_ASSERT(PR_SUCCESS == rv);
     61 
     62            now = RCInterval(RCInterval::now);
     63            elapsed = now - epoch;
     64        }
     65 
     66        PR_fprintf(
     67            output, "Sleeping %u seconds took %s%u milliseconds\n",
     68            seconds, ((elapsed < timeout)? "**" : ""),
     69            elapsed.ToMilliseconds());
     70    }
     71 
     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);
     79 
     80            epoch.SetToNow();
     81 
     82            for (loops = 0; loops < iterations; ++loops)
     83            {
     84                rv = cv.Wait();
     85                PR_ASSERT(PR_SUCCESS == rv);
     86            }
     87 
     88            now = RCInterval(RCInterval::now);
     89            elapsed = now - epoch;
     90        }
     91        elapsed /= iterations;
     92 
     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 */
     99 
    100 /* interval.cpp */