Skip to content

Commit dab18fb

Browse files
ulanCommit bot
authored andcommitted
Make idle tasks optional in the default platform.
BUG=v8:6056 Review-Url: https://codereview.chromium.org/2737743002 Cr-Commit-Position: refs/heads/master@{#43640}
1 parent 698c2f3 commit dab18fb

6 files changed

Lines changed: 28 additions & 11 deletions

File tree

include/libplatform/libplatform.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,22 @@
1212
namespace v8 {
1313
namespace platform {
1414

15+
enum class IdleTaskSupport { kDisabled, kEnabled };
16+
1517
/**
1618
* Returns a new instance of the default v8::Platform implementation.
1719
*
1820
* The caller will take ownership of the returned pointer. |thread_pool_size|
1921
* is the number of worker threads to allocate for background jobs. If a value
2022
* of zero is passed, a suitable default based on the current number of
2123
* processors online will be chosen.
24+
* If |idle_task_support| is enabled then the platform will accept idle
25+
* tasks (IdleTasksEnabled will return true) and will rely on the embedder
26+
* calling v8::platform::RunIdleTasks to process the idle tasks.
2227
*/
2328
V8_PLATFORM_EXPORT v8::Platform* CreateDefaultPlatform(
24-
int thread_pool_size = 0);
29+
int thread_pool_size = 0,
30+
IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled);
2531

2632
/**
2733
* Pumps the message loop for the given isolate.

src/d8.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2892,7 +2892,8 @@ int Shell::Main(int argc, char* argv[]) {
28922892
v8::V8::InitializeICUDefaultLocation(argv[0], options.icu_data_file);
28932893
g_platform = i::FLAG_verify_predictable
28942894
? new PredictablePlatform()
2895-
: v8::platform::CreateDefaultPlatform();
2895+
: v8::platform::CreateDefaultPlatform(
2896+
0, v8::platform::IdleTaskSupport::kEnabled);
28962897

28972898
platform::tracing::TracingController* tracing_controller;
28982899
if (options.trace_enabled) {

src/libplatform/default-platform.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
namespace v8 {
1818
namespace platform {
1919

20-
21-
v8::Platform* CreateDefaultPlatform(int thread_pool_size) {
22-
DefaultPlatform* platform = new DefaultPlatform();
20+
v8::Platform* CreateDefaultPlatform(int thread_pool_size,
21+
IdleTaskSupport idle_task_support) {
22+
DefaultPlatform* platform = new DefaultPlatform(idle_task_support);
2323
platform->SetThreadPoolSize(thread_pool_size);
2424
platform->EnsureInitialized();
2525
return platform;
@@ -45,8 +45,10 @@ void SetTracingController(
4545

4646
const int DefaultPlatform::kMaxThreadPoolSize = 8;
4747

48-
DefaultPlatform::DefaultPlatform()
49-
: initialized_(false), thread_pool_size_(0) {}
48+
DefaultPlatform::DefaultPlatform(IdleTaskSupport idle_task_support)
49+
: initialized_(false),
50+
thread_pool_size_(0),
51+
idle_task_support_(idle_task_support) {}
5052

5153
DefaultPlatform::~DefaultPlatform() {
5254
if (tracing_controller_) {
@@ -165,6 +167,7 @@ bool DefaultPlatform::PumpMessageLoop(v8::Isolate* isolate) {
165167

166168
void DefaultPlatform::RunIdleTasks(v8::Isolate* isolate,
167169
double idle_time_in_seconds) {
170+
DCHECK(IdleTaskSupport::kEnabled == idle_task_support_);
168171
double deadline_in_seconds =
169172
MonotonicallyIncreasingTime() + idle_time_in_seconds;
170173
while (deadline_in_seconds > MonotonicallyIncreasingTime()) {
@@ -208,7 +211,9 @@ void DefaultPlatform::CallIdleOnForegroundThread(Isolate* isolate,
208211
main_thread_idle_queue_[isolate].push(task);
209212
}
210213

211-
bool DefaultPlatform::IdleTasksEnabled(Isolate* isolate) { return true; }
214+
bool DefaultPlatform::IdleTasksEnabled(Isolate* isolate) {
215+
return idle_task_support_ == IdleTaskSupport::kEnabled;
216+
}
212217

213218
double DefaultPlatform::MonotonicallyIncreasingTime() {
214219
return base::TimeTicks::HighResolutionNow().ToInternalValue() /

src/libplatform/default-platform.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <vector>
1313

1414
#include "include/libplatform/libplatform-export.h"
15+
#include "include/libplatform/libplatform.h"
1516
#include "include/libplatform/v8-tracing.h"
1617
#include "include/v8-platform.h"
1718
#include "src/base/compiler-specific.h"
@@ -32,7 +33,8 @@ class TracingController;
3233

3334
class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
3435
public:
35-
DefaultPlatform();
36+
explicit DefaultPlatform(
37+
IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled);
3638
virtual ~DefaultPlatform();
3739

3840
void SetThreadPoolSize(int thread_pool_size);
@@ -81,6 +83,7 @@ class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
8183
base::Mutex lock_;
8284
bool initialized_;
8385
int thread_pool_size_;
86+
IdleTaskSupport idle_task_support_;
8487
std::vector<WorkerThread*> thread_pool_;
8588
TaskQueue queue_;
8689
std::map<v8::Isolate*, std::queue<Task*>> main_thread_queue_;

test/unittests/libplatform/default-platform-unittest.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ struct MockIdleTask : public IdleTask {
2727

2828
class DefaultPlatformWithMockTime : public DefaultPlatform {
2929
public:
30-
DefaultPlatformWithMockTime() : time_(0) {}
30+
DefaultPlatformWithMockTime()
31+
: DefaultPlatform(IdleTaskSupport::kEnabled), time_(0) {}
3132
double MonotonicallyIncreasingTime() override { return time_; }
3233
void IncreaseTime(double seconds) { time_ += seconds; }
3334

test/unittests/run-all-unittests.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class DefaultPlatformEnvironment final : public ::testing::Environment {
1515

1616
void SetUp() override {
1717
EXPECT_EQ(NULL, platform_);
18-
platform_ = v8::platform::CreateDefaultPlatform();
18+
platform_ = v8::platform::CreateDefaultPlatform(
19+
0, v8::platform::IdleTaskSupport::kEnabled);
1920
ASSERT_TRUE(platform_ != NULL);
2021
v8::V8::InitializePlatform(platform_);
2122
ASSERT_TRUE(v8::V8::Initialize());

0 commit comments

Comments
 (0)