michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef js_SliceBudget_h michael@0: #define js_SliceBudget_h michael@0: michael@0: #include michael@0: michael@0: namespace js { michael@0: michael@0: /* michael@0: * This class records how much work has been done in a given collection slice, so that michael@0: * we can return before pausing for too long. Some slices are allowed to run for michael@0: * unlimited time, and others are bounded. To reduce the number of gettimeofday michael@0: * calls, we only check the time every 1000 operations. michael@0: */ michael@0: struct JS_PUBLIC_API(SliceBudget) michael@0: { michael@0: int64_t deadline; /* in microseconds */ michael@0: intptr_t counter; michael@0: michael@0: static const intptr_t CounterReset = 1000; michael@0: michael@0: static const int64_t Unlimited = 0; michael@0: static int64_t TimeBudget(int64_t millis); michael@0: static int64_t WorkBudget(int64_t work); michael@0: michael@0: /* Equivalent to SliceBudget(UnlimitedBudget). */ michael@0: SliceBudget(); michael@0: michael@0: /* Instantiate as SliceBudget(Time/WorkBudget(n)). */ michael@0: SliceBudget(int64_t budget); michael@0: michael@0: void reset() { michael@0: deadline = INT64_MAX; michael@0: counter = INTPTR_MAX; michael@0: } michael@0: michael@0: void step(intptr_t amt = 1) { michael@0: counter -= amt; michael@0: } michael@0: michael@0: bool checkOverBudget(); michael@0: michael@0: bool isOverBudget() { michael@0: if (counter >= 0) michael@0: return false; michael@0: return checkOverBudget(); michael@0: } michael@0: }; michael@0: michael@0: } // namespace js michael@0: michael@0: #endif /* js_SliceBudget_h */