Skip to content

Commit

Permalink
Make idle tasks optional in the default platform.
Browse files Browse the repository at this point in the history
BUG=v8:6056

Review-Url: https://codereview.chromium.org/2737743002
Cr-Commit-Position: refs/heads/master@{#43640}
  • Loading branch information
ulan authored and Commit bot committed Mar 7, 2017
1 parent 698c2f3 commit dab18fb
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
8 changes: 7 additions & 1 deletion include/libplatform/libplatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@
namespace v8 {
namespace platform {

enum class IdleTaskSupport { kDisabled, kEnabled };

/**
* Returns a new instance of the default v8::Platform implementation.
*
* The caller will take ownership of the returned pointer. |thread_pool_size|
* is the number of worker threads to allocate for background jobs. If a value
* of zero is passed, a suitable default based on the current number of
* processors online will be chosen.
* If |idle_task_support| is enabled then the platform will accept idle
* tasks (IdleTasksEnabled will return true) and will rely on the embedder
* calling v8::platform::RunIdleTasks to process the idle tasks.
*/
V8_PLATFORM_EXPORT v8::Platform* CreateDefaultPlatform(
int thread_pool_size = 0);
int thread_pool_size = 0,
IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled);

/**
* Pumps the message loop for the given isolate.
Expand Down
3 changes: 2 additions & 1 deletion src/d8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2892,7 +2892,8 @@ int Shell::Main(int argc, char* argv[]) {
v8::V8::InitializeICUDefaultLocation(argv[0], options.icu_data_file);
g_platform = i::FLAG_verify_predictable
? new PredictablePlatform()
: v8::platform::CreateDefaultPlatform();
: v8::platform::CreateDefaultPlatform(
0, v8::platform::IdleTaskSupport::kEnabled);

platform::tracing::TracingController* tracing_controller;
if (options.trace_enabled) {
Expand Down
17 changes: 11 additions & 6 deletions src/libplatform/default-platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
namespace v8 {
namespace platform {


v8::Platform* CreateDefaultPlatform(int thread_pool_size) {
DefaultPlatform* platform = new DefaultPlatform();
v8::Platform* CreateDefaultPlatform(int thread_pool_size,
IdleTaskSupport idle_task_support) {
DefaultPlatform* platform = new DefaultPlatform(idle_task_support);
platform->SetThreadPoolSize(thread_pool_size);
platform->EnsureInitialized();
return platform;
Expand All @@ -45,8 +45,10 @@ void SetTracingController(

const int DefaultPlatform::kMaxThreadPoolSize = 8;

DefaultPlatform::DefaultPlatform()
: initialized_(false), thread_pool_size_(0) {}
DefaultPlatform::DefaultPlatform(IdleTaskSupport idle_task_support)
: initialized_(false),
thread_pool_size_(0),
idle_task_support_(idle_task_support) {}

DefaultPlatform::~DefaultPlatform() {
if (tracing_controller_) {
Expand Down Expand Up @@ -165,6 +167,7 @@ bool DefaultPlatform::PumpMessageLoop(v8::Isolate* isolate) {

void DefaultPlatform::RunIdleTasks(v8::Isolate* isolate,
double idle_time_in_seconds) {
DCHECK(IdleTaskSupport::kEnabled == idle_task_support_);
double deadline_in_seconds =
MonotonicallyIncreasingTime() + idle_time_in_seconds;
while (deadline_in_seconds > MonotonicallyIncreasingTime()) {
Expand Down Expand Up @@ -208,7 +211,9 @@ void DefaultPlatform::CallIdleOnForegroundThread(Isolate* isolate,
main_thread_idle_queue_[isolate].push(task);
}

bool DefaultPlatform::IdleTasksEnabled(Isolate* isolate) { return true; }
bool DefaultPlatform::IdleTasksEnabled(Isolate* isolate) {
return idle_task_support_ == IdleTaskSupport::kEnabled;
}

double DefaultPlatform::MonotonicallyIncreasingTime() {
return base::TimeTicks::HighResolutionNow().ToInternalValue() /
Expand Down
5 changes: 4 additions & 1 deletion src/libplatform/default-platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <vector>

#include "include/libplatform/libplatform-export.h"
#include "include/libplatform/libplatform.h"
#include "include/libplatform/v8-tracing.h"
#include "include/v8-platform.h"
#include "src/base/compiler-specific.h"
Expand All @@ -32,7 +33,8 @@ class TracingController;

class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
public:
DefaultPlatform();
explicit DefaultPlatform(
IdleTaskSupport idle_task_support = IdleTaskSupport::kDisabled);
virtual ~DefaultPlatform();

void SetThreadPoolSize(int thread_pool_size);
Expand Down Expand Up @@ -81,6 +83,7 @@ class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) {
base::Mutex lock_;
bool initialized_;
int thread_pool_size_;
IdleTaskSupport idle_task_support_;
std::vector<WorkerThread*> thread_pool_;
TaskQueue queue_;
std::map<v8::Isolate*, std::queue<Task*>> main_thread_queue_;
Expand Down
3 changes: 2 additions & 1 deletion test/unittests/libplatform/default-platform-unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ struct MockIdleTask : public IdleTask {

class DefaultPlatformWithMockTime : public DefaultPlatform {
public:
DefaultPlatformWithMockTime() : time_(0) {}
DefaultPlatformWithMockTime()
: DefaultPlatform(IdleTaskSupport::kEnabled), time_(0) {}
double MonotonicallyIncreasingTime() override { return time_; }
void IncreaseTime(double seconds) { time_ += seconds; }

Expand Down
3 changes: 2 additions & 1 deletion test/unittests/run-all-unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class DefaultPlatformEnvironment final : public ::testing::Environment {

void SetUp() override {
EXPECT_EQ(NULL, platform_);
platform_ = v8::platform::CreateDefaultPlatform();
platform_ = v8::platform::CreateDefaultPlatform(
0, v8::platform::IdleTaskSupport::kEnabled);
ASSERT_TRUE(platform_ != NULL);
v8::V8::InitializePlatform(platform_);
ASSERT_TRUE(v8::V8::Initialize());
Expand Down

0 comments on commit dab18fb

Please sign in to comment.