js/public/SliceBudget.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/public/SliceBudget.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,57 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef js_SliceBudget_h
    1.11 +#define js_SliceBudget_h
    1.12 +
    1.13 +#include <stdint.h>
    1.14 +
    1.15 +namespace js {
    1.16 +
    1.17 +/*
    1.18 + * This class records how much work has been done in a given collection slice, so that
    1.19 + * we can return before pausing for too long. Some slices are allowed to run for
    1.20 + * unlimited time, and others are bounded. To reduce the number of gettimeofday
    1.21 + * calls, we only check the time every 1000 operations.
    1.22 + */
    1.23 +struct JS_PUBLIC_API(SliceBudget)
    1.24 +{
    1.25 +    int64_t deadline; /* in microseconds */
    1.26 +    intptr_t counter;
    1.27 +
    1.28 +    static const intptr_t CounterReset = 1000;
    1.29 +
    1.30 +    static const int64_t Unlimited = 0;
    1.31 +    static int64_t TimeBudget(int64_t millis);
    1.32 +    static int64_t WorkBudget(int64_t work);
    1.33 +
    1.34 +    /* Equivalent to SliceBudget(UnlimitedBudget). */
    1.35 +    SliceBudget();
    1.36 +
    1.37 +    /* Instantiate as SliceBudget(Time/WorkBudget(n)). */
    1.38 +    SliceBudget(int64_t budget);
    1.39 +
    1.40 +    void reset() {
    1.41 +        deadline = INT64_MAX;
    1.42 +        counter = INTPTR_MAX;
    1.43 +    }
    1.44 +
    1.45 +    void step(intptr_t amt = 1) {
    1.46 +        counter -= amt;
    1.47 +    }
    1.48 +
    1.49 +    bool checkOverBudget();
    1.50 +
    1.51 +    bool isOverBudget() {
    1.52 +        if (counter >= 0)
    1.53 +            return false;
    1.54 +        return checkOverBudget();
    1.55 +    }
    1.56 +};
    1.57 +
    1.58 +} // namespace js
    1.59 +
    1.60 +#endif /* js_SliceBudget_h */

mercurial