tor-browser

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

initclk.c (2159B)


      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 /*
      7 * This is a regression test for the bug that the interval timer
      8 * is not initialized when _PR_CreateCPU calls PR_IntervalNow.
      9 * The bug would make this test program finish prematurely,
     10 * when the SHORT_TIMEOUT period expires.  The correct behavior
     11 * is for the test to finish when the LONG_TIMEOUT period expires.
     12 */
     13 
     14 #include "prlock.h"
     15 #include "prcvar.h"
     16 #include "prthread.h"
     17 #include "prinrval.h"
     18 #include "prlog.h"
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 
     22 /* The timeouts, in milliseconds */
     23 #define SHORT_TIMEOUT 1000
     24 #define LONG_TIMEOUT 3000
     25 
     26 PRLock *lock1, *lock2;
     27 PRCondVar *cv1, *cv2;
     28 
     29 void ThreadFunc(void* arg) {
     30  PR_Lock(lock1);
     31  PR_WaitCondVar(cv1, PR_MillisecondsToInterval(SHORT_TIMEOUT));
     32  PR_Unlock(lock1);
     33 }
     34 
     35 int main(int argc, char** argv) {
     36  PRThread* thread;
     37  PRIntervalTime start, end;
     38  PRUint32 elapsed_ms;
     39 
     40  lock1 = PR_NewLock();
     41  PR_ASSERT(NULL != lock1);
     42  cv1 = PR_NewCondVar(lock1);
     43  PR_ASSERT(NULL != cv1);
     44  lock2 = PR_NewLock();
     45  PR_ASSERT(NULL != lock2);
     46  cv2 = PR_NewCondVar(lock2);
     47  PR_ASSERT(NULL != cv2);
     48  start = PR_IntervalNow();
     49  thread = PR_CreateThread(PR_USER_THREAD, ThreadFunc, NULL, PR_PRIORITY_NORMAL,
     50                           PR_LOCAL_THREAD, PR_JOINABLE_THREAD, 0);
     51  PR_ASSERT(NULL != thread);
     52  PR_Lock(lock2);
     53  PR_WaitCondVar(cv2, PR_MillisecondsToInterval(LONG_TIMEOUT));
     54  PR_Unlock(lock2);
     55  PR_JoinThread(thread);
     56  end = PR_IntervalNow();
     57  elapsed_ms = PR_IntervalToMilliseconds((PRIntervalTime)(end - start));
     58  /* Allow -100ms/+200ms imprecision */
     59  if (elapsed_ms < LONG_TIMEOUT - 100 || elapsed_ms > LONG_TIMEOUT + 200) {
     60    printf("Elapsed time should be %u ms but is %u ms\n", LONG_TIMEOUT,
     61           elapsed_ms);
     62    printf("FAIL\n");
     63    exit(1);
     64  }
     65  printf("Elapsed time: %u ms, expected time: %u ms\n", LONG_TIMEOUT,
     66         elapsed_ms);
     67  printf("PASS\n");
     68  return 0;
     69 }