Line data Source 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/. */
6 :
7 : #ifndef js_SliceBudget_h
8 : #define js_SliceBudget_h
9 :
10 : #include <stdint.h>
11 :
12 : namespace js {
13 :
14 : struct JS_PUBLIC_API(TimeBudget)
15 : {
16 : int64_t budget;
17 :
18 3 : explicit TimeBudget(int64_t milliseconds) { budget = milliseconds; }
19 : };
20 :
21 : struct JS_PUBLIC_API(WorkBudget)
22 : {
23 : int64_t budget;
24 :
25 3 : explicit WorkBudget(int64_t work) { budget = work; }
26 : };
27 :
28 : /*
29 : * This class records how much work has been done in a given collection slice,
30 : * so that we can return before pausing for too long. Some slices are allowed
31 : * to run for unlimited time, and others are bounded. To reduce the number of
32 : * gettimeofday calls, we only check the time every 1000 operations.
33 : */
34 : class JS_PUBLIC_API(SliceBudget)
35 : {
36 : static const int64_t unlimitedDeadline = INT64_MAX;
37 : static const intptr_t unlimitedStartCounter = INTPTR_MAX;
38 :
39 : bool checkOverBudget();
40 :
41 : SliceBudget();
42 :
43 : public:
44 : // Memory of the originally requested budget. If isUnlimited, neither of
45 : // these are in use. If deadline==0, then workBudget is valid. Otherwise
46 : // timeBudget is valid.
47 : TimeBudget timeBudget;
48 : WorkBudget workBudget;
49 :
50 : int64_t deadline; /* in microseconds */
51 : intptr_t counter;
52 :
53 : static const intptr_t CounterReset = 1000;
54 :
55 : static const int64_t UnlimitedTimeBudget = -1;
56 : static const int64_t UnlimitedWorkBudget = -1;
57 :
58 : /* Use to create an unlimited budget. */
59 0 : static SliceBudget unlimited() { return SliceBudget(); }
60 :
61 : /* Instantiate as SliceBudget(TimeBudget(n)). */
62 : explicit SliceBudget(TimeBudget time);
63 :
64 : /* Instantiate as SliceBudget(WorkBudget(n)). */
65 : explicit SliceBudget(WorkBudget work);
66 :
67 0 : void makeUnlimited() {
68 0 : deadline = unlimitedDeadline;
69 0 : counter = unlimitedStartCounter;
70 0 : }
71 :
72 3000 : void step(intptr_t amt = 1) {
73 3000 : counter -= amt;
74 3000 : }
75 :
76 5351 : bool isOverBudget() {
77 5351 : if (counter > 0)
78 5345 : return false;
79 6 : return checkOverBudget();
80 : }
81 :
82 0 : bool isWorkBudget() const { return deadline == 0; }
83 3 : bool isTimeBudget() const { return deadline > 0 && !isUnlimited(); }
84 6 : bool isUnlimited() const { return deadline == unlimitedDeadline; }
85 :
86 : int describe(char* buffer, size_t maxlen) const;
87 : };
88 :
89 : } // namespace js
90 :
91 : #endif /* js_SliceBudget_h */
|