Add an API to pump the message loop to libplatform
BUG=none LOG=n R=marja@chromium.org Review URL: https://codereview.chromium.org/363303002 git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@22187 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
parent
ba584456e6
commit
c1231d426f
@ -21,6 +21,17 @@ namespace platform {
|
||||
v8::Platform* CreateDefaultPlatform(int thread_pool_size = 0);
|
||||
|
||||
|
||||
/**
|
||||
* Pumps the message loop for the given isolate.
|
||||
*
|
||||
* The caller has to make sure that this is called from the right thread.
|
||||
* Returns true if a task was executed, and false otherwise. This call does
|
||||
* not block if no task is pending. The |platform| has to be created using
|
||||
* |CreateDefaultPlatform|.
|
||||
*/
|
||||
bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate);
|
||||
|
||||
|
||||
} // namespace platform
|
||||
} // namespace v8
|
||||
|
||||
|
@ -23,6 +23,11 @@ v8::Platform* CreateDefaultPlatform(int thread_pool_size) {
|
||||
}
|
||||
|
||||
|
||||
bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate) {
|
||||
return reinterpret_cast<DefaultPlatform*>(platform)->PumpMessageLoop(isolate);
|
||||
}
|
||||
|
||||
|
||||
const int DefaultPlatform::kMaxThreadPoolSize = 4;
|
||||
|
||||
|
||||
@ -39,6 +44,14 @@ DefaultPlatform::~DefaultPlatform() {
|
||||
delete *i;
|
||||
}
|
||||
}
|
||||
for (std::map<v8::Isolate*, std::queue<Task*> >::iterator i =
|
||||
main_thread_queue_.begin();
|
||||
i != main_thread_queue_.end(); ++i) {
|
||||
while (!i->second.empty()) {
|
||||
delete i->second.front();
|
||||
i->second.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -61,6 +74,24 @@ void DefaultPlatform::EnsureInitialized() {
|
||||
thread_pool_.push_back(new WorkerThread(&queue_));
|
||||
}
|
||||
|
||||
|
||||
bool DefaultPlatform::PumpMessageLoop(v8::Isolate* isolate) {
|
||||
Task* task = NULL;
|
||||
{
|
||||
base::LockGuard<base::Mutex> guard(&lock_);
|
||||
std::map<v8::Isolate*, std::queue<Task*> >::iterator it =
|
||||
main_thread_queue_.find(isolate);
|
||||
if (it == main_thread_queue_.end() || it->second.empty()) {
|
||||
return false;
|
||||
}
|
||||
task = it->second.front();
|
||||
it->second.pop();
|
||||
}
|
||||
task->Run();
|
||||
delete task;
|
||||
return true;
|
||||
}
|
||||
|
||||
void DefaultPlatform::CallOnBackgroundThread(Task *task,
|
||||
ExpectedRuntime expected_runtime) {
|
||||
EnsureInitialized();
|
||||
@ -69,9 +100,8 @@ void DefaultPlatform::CallOnBackgroundThread(Task *task,
|
||||
|
||||
|
||||
void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) {
|
||||
// TODO(jochen): implement.
|
||||
task->Run();
|
||||
delete task;
|
||||
base::LockGuard<base::Mutex> guard(&lock_);
|
||||
main_thread_queue_[isolate].push(task);
|
||||
}
|
||||
|
||||
} } // namespace v8::platform
|
||||
|
@ -5,6 +5,8 @@
|
||||
#ifndef V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
|
||||
#define V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
|
||||
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
|
||||
#include "include/v8-platform.h"
|
||||
@ -28,11 +30,13 @@ class DefaultPlatform : public Platform {
|
||||
|
||||
void EnsureInitialized();
|
||||
|
||||
bool PumpMessageLoop(v8::Isolate* isolate);
|
||||
|
||||
// v8::Platform implementation.
|
||||
virtual void CallOnBackgroundThread(
|
||||
Task *task, ExpectedRuntime expected_runtime) V8_OVERRIDE;
|
||||
virtual void CallOnForegroundThread(v8::Isolate *isolate,
|
||||
Task *task) V8_OVERRIDE;
|
||||
Task* task, ExpectedRuntime expected_runtime) V8_OVERRIDE;
|
||||
virtual void CallOnForegroundThread(v8::Isolate* isolate,
|
||||
Task* task) V8_OVERRIDE;
|
||||
|
||||
private:
|
||||
static const int kMaxThreadPoolSize;
|
||||
@ -42,6 +46,7 @@ class DefaultPlatform : public Platform {
|
||||
int thread_pool_size_;
|
||||
std::vector<WorkerThread*> thread_pool_;
|
||||
TaskQueue queue_;
|
||||
std::map<v8::Isolate*, std::queue<Task*> > main_thread_queue_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(DefaultPlatform);
|
||||
};
|
||||
|
@ -85,6 +85,7 @@
|
||||
'test-heap.cc',
|
||||
'test-heap-profiler.cc',
|
||||
'test-hydrogen-types.cc',
|
||||
'test-libplatform-default-platform.cc',
|
||||
'test-libplatform-task-queue.cc',
|
||||
'test-libplatform-worker-thread.cc',
|
||||
'test-list.cc',
|
||||
|
30
test/cctest/test-libplatform-default-platform.cc
Normal file
30
test/cctest/test-libplatform-default-platform.cc
Normal file
@ -0,0 +1,30 @@
|
||||
// Copyright 2014 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "src/v8.h"
|
||||
|
||||
#include "src/libplatform/default-platform.h"
|
||||
#include "test/cctest/cctest.h"
|
||||
#include "test/cctest/test-libplatform.h"
|
||||
|
||||
using namespace v8::internal;
|
||||
using namespace v8::platform;
|
||||
|
||||
|
||||
TEST(DefaultPlatformMessagePump) {
|
||||
TaskCounter task_counter;
|
||||
|
||||
DefaultPlatform platform;
|
||||
|
||||
TestTask* task = new TestTask(&task_counter, true);
|
||||
|
||||
CHECK(!platform.PumpMessageLoop(CcTest::isolate()));
|
||||
|
||||
platform.CallOnForegroundThread(CcTest::isolate(), task);
|
||||
|
||||
CHECK_EQ(1, task_counter.GetCount());
|
||||
CHECK(platform.PumpMessageLoop(CcTest::isolate()));
|
||||
CHECK_EQ(0, task_counter.GetCount());
|
||||
CHECK(!platform.PumpMessageLoop(CcTest::isolate()));
|
||||
}
|
Loading…
Reference in New Issue
Block a user