|
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 /* |
|
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; |
|
24 |
|
25 static const intptr_t CounterReset = 1000; |
|
26 |
|
27 static const int64_t Unlimited = 0; |
|
28 static int64_t TimeBudget(int64_t millis); |
|
29 static int64_t WorkBudget(int64_t work); |
|
30 |
|
31 /* Equivalent to SliceBudget(UnlimitedBudget). */ |
|
32 SliceBudget(); |
|
33 |
|
34 /* Instantiate as SliceBudget(Time/WorkBudget(n)). */ |
|
35 SliceBudget(int64_t budget); |
|
36 |
|
37 void reset() { |
|
38 deadline = INT64_MAX; |
|
39 counter = INTPTR_MAX; |
|
40 } |
|
41 |
|
42 void step(intptr_t amt = 1) { |
|
43 counter -= amt; |
|
44 } |
|
45 |
|
46 bool checkOverBudget(); |
|
47 |
|
48 bool isOverBudget() { |
|
49 if (counter >= 0) |
|
50 return false; |
|
51 return checkOverBudget(); |
|
52 } |
|
53 }; |
|
54 |
|
55 } // namespace js |
|
56 |
|
57 #endif /* js_SliceBudget_h */ |