js/public/SliceBudget.h

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2  * vim: set ts=8 sts=4 et sw=4 tw=99:
     3  * This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef js_SliceBudget_h
     8 #define js_SliceBudget_h
    10 #include <stdint.h>
    12 namespace js {
    14 /*
    15  * This class records how much work has been done in a given collection slice, so that
    16  * we can return before pausing for too long. Some slices are allowed to run for
    17  * unlimited time, and others are bounded. To reduce the number of gettimeofday
    18  * calls, we only check the time every 1000 operations.
    19  */
    20 struct JS_PUBLIC_API(SliceBudget)
    21 {
    22     int64_t deadline; /* in microseconds */
    23     intptr_t counter;
    25     static const intptr_t CounterReset = 1000;
    27     static const int64_t Unlimited = 0;
    28     static int64_t TimeBudget(int64_t millis);
    29     static int64_t WorkBudget(int64_t work);
    31     /* Equivalent to SliceBudget(UnlimitedBudget). */
    32     SliceBudget();
    34     /* Instantiate as SliceBudget(Time/WorkBudget(n)). */
    35     SliceBudget(int64_t budget);
    37     void reset() {
    38         deadline = INT64_MAX;
    39         counter = INTPTR_MAX;
    40     }
    42     void step(intptr_t amt = 1) {
    43         counter -= amt;
    44     }
    46     bool checkOverBudget();
    48     bool isOverBudget() {
    49         if (counter >= 0)
    50             return false;
    51         return checkOverBudget();
    52     }
    53 };
    55 } // namespace js
    57 #endif /* js_SliceBudget_h */

mercurial