2013-12-03 10:47:47 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef js_SliceBudget_h
|
|
|
|
#define js_SliceBudget_h
|
|
|
|
|
2013-12-03 15:53:40 -08:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-12-03 10:47:47 -08:00
|
|
|
namespace js {
|
|
|
|
|
2014-10-22 05:13:00 -07:00
|
|
|
struct JS_PUBLIC_API(TimeBudget)
|
|
|
|
{
|
|
|
|
int64_t budget;
|
|
|
|
|
|
|
|
explicit TimeBudget(int64_t milliseconds) { budget = milliseconds; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct JS_PUBLIC_API(WorkBudget)
|
|
|
|
{
|
|
|
|
int64_t budget;
|
|
|
|
|
|
|
|
explicit WorkBudget(int64_t work) { budget = work; }
|
|
|
|
};
|
|
|
|
|
2013-12-03 10:47:47 -08:00
|
|
|
/*
|
2014-10-22 05:13:00 -07:00
|
|
|
* This class records how much work has been done in a given collection slice,
|
|
|
|
* so that we can return before pausing for too long. Some slices are allowed
|
|
|
|
* to run for unlimited time, and others are bounded. To reduce the number of
|
|
|
|
* gettimeofday calls, we only check the time every 1000 operations.
|
2013-12-03 10:47:47 -08:00
|
|
|
*/
|
2013-12-03 15:53:40 -08:00
|
|
|
struct JS_PUBLIC_API(SliceBudget)
|
|
|
|
{
|
2013-12-03 10:47:47 -08:00
|
|
|
int64_t deadline; /* in microseconds */
|
|
|
|
intptr_t counter;
|
|
|
|
|
|
|
|
static const intptr_t CounterReset = 1000;
|
|
|
|
|
2014-11-03 22:34:00 -08:00
|
|
|
static const int64_t Unlimited = -1;
|
2013-12-03 10:47:47 -08:00
|
|
|
|
2014-10-22 05:13:00 -07:00
|
|
|
/* Use to create an unlimited budget. */
|
2013-12-03 10:47:47 -08:00
|
|
|
SliceBudget();
|
|
|
|
|
2014-10-22 05:13:00 -07:00
|
|
|
/* Instantiate as SliceBudget(TimeBudget(n)). */
|
|
|
|
explicit SliceBudget(TimeBudget time);
|
|
|
|
|
|
|
|
/* Instantiate as SliceBudget(WorkBudget(n)). */
|
|
|
|
explicit SliceBudget(WorkBudget work);
|
2013-12-03 10:47:47 -08:00
|
|
|
|
2014-11-03 22:35:00 -08:00
|
|
|
void makeUnlimited() {
|
2014-05-14 09:45:49 -07:00
|
|
|
deadline = unlimitedDeadline;
|
|
|
|
counter = unlimitedStartCounter;
|
2013-12-03 10:47:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void step(intptr_t amt = 1) {
|
|
|
|
counter -= amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isOverBudget() {
|
2014-11-03 22:34:00 -08:00
|
|
|
if (counter > 0)
|
2013-12-03 10:47:47 -08:00
|
|
|
return false;
|
|
|
|
return checkOverBudget();
|
|
|
|
}
|
2014-05-14 09:45:49 -07:00
|
|
|
|
|
|
|
bool isUnlimited() {
|
|
|
|
return deadline == unlimitedDeadline;
|
|
|
|
}
|
|
|
|
|
2014-10-22 05:13:00 -07:00
|
|
|
private:
|
|
|
|
bool checkOverBudget();
|
|
|
|
|
2014-05-14 09:45:49 -07:00
|
|
|
static const int64_t unlimitedDeadline = INT64_MAX;
|
|
|
|
static const intptr_t unlimitedStartCounter = INTPTR_MAX;
|
2013-12-03 10:47:47 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace js
|
|
|
|
|
|
|
|
#endif /* js_SliceBudget_h */
|