Commit c1231d42 authored by jochen@chromium.org's avatar jochen@chromium.org

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
parent ba584456
...@@ -21,6 +21,17 @@ namespace platform { ...@@ -21,6 +21,17 @@ namespace platform {
v8::Platform* CreateDefaultPlatform(int thread_pool_size = 0); 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 platform
} // namespace v8 } // namespace v8
......
...@@ -23,6 +23,11 @@ v8::Platform* CreateDefaultPlatform(int thread_pool_size) { ...@@ -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; const int DefaultPlatform::kMaxThreadPoolSize = 4;
...@@ -39,6 +44,14 @@ DefaultPlatform::~DefaultPlatform() { ...@@ -39,6 +44,14 @@ DefaultPlatform::~DefaultPlatform() {
delete *i; 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() { ...@@ -61,6 +74,24 @@ void DefaultPlatform::EnsureInitialized() {
thread_pool_.push_back(new WorkerThread(&queue_)); 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, void DefaultPlatform::CallOnBackgroundThread(Task *task,
ExpectedRuntime expected_runtime) { ExpectedRuntime expected_runtime) {
EnsureInitialized(); EnsureInitialized();
...@@ -69,9 +100,8 @@ void DefaultPlatform::CallOnBackgroundThread(Task *task, ...@@ -69,9 +100,8 @@ void DefaultPlatform::CallOnBackgroundThread(Task *task,
void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) { void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) {
// TODO(jochen): implement. base::LockGuard<base::Mutex> guard(&lock_);
task->Run(); main_thread_queue_[isolate].push(task);
delete task;
} }
} } // namespace v8::platform } } // namespace v8::platform
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef V8_LIBPLATFORM_DEFAULT_PLATFORM_H_ #ifndef V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
#define V8_LIBPLATFORM_DEFAULT_PLATFORM_H_ #define V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
#include <map>
#include <queue>
#include <vector> #include <vector>
#include "include/v8-platform.h" #include "include/v8-platform.h"
...@@ -28,11 +30,13 @@ class DefaultPlatform : public Platform { ...@@ -28,11 +30,13 @@ class DefaultPlatform : public Platform {
void EnsureInitialized(); void EnsureInitialized();
bool PumpMessageLoop(v8::Isolate* isolate);
// v8::Platform implementation. // v8::Platform implementation.
virtual void CallOnBackgroundThread( virtual void CallOnBackgroundThread(
Task *task, ExpectedRuntime expected_runtime) V8_OVERRIDE; Task* task, ExpectedRuntime expected_runtime) V8_OVERRIDE;
virtual void CallOnForegroundThread(v8::Isolate *isolate, virtual void CallOnForegroundThread(v8::Isolate* isolate,
Task *task) V8_OVERRIDE; Task* task) V8_OVERRIDE;
private: private:
static const int kMaxThreadPoolSize; static const int kMaxThreadPoolSize;
...@@ -42,6 +46,7 @@ class DefaultPlatform : public Platform { ...@@ -42,6 +46,7 @@ class DefaultPlatform : public Platform {
int thread_pool_size_; int thread_pool_size_;
std::vector<WorkerThread*> thread_pool_; std::vector<WorkerThread*> thread_pool_;
TaskQueue queue_; TaskQueue queue_;
std::map<v8::Isolate*, std::queue<Task*> > main_thread_queue_;
DISALLOW_COPY_AND_ASSIGN(DefaultPlatform); DISALLOW_COPY_AND_ASSIGN(DefaultPlatform);
}; };
......
...@@ -85,6 +85,7 @@ ...@@ -85,6 +85,7 @@
'test-heap.cc', 'test-heap.cc',
'test-heap-profiler.cc', 'test-heap-profiler.cc',
'test-hydrogen-types.cc', 'test-hydrogen-types.cc',
'test-libplatform-default-platform.cc',
'test-libplatform-task-queue.cc', 'test-libplatform-task-queue.cc',
'test-libplatform-worker-thread.cc', 'test-libplatform-worker-thread.cc',
'test-list.cc', 'test-list.cc',
......
// 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()));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment