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
}
+bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate) {
+ return reinterpret_cast<DefaultPlatform*>(platform)->PumpMessageLoop(isolate);
+}
+
+
const int DefaultPlatform::kMaxThreadPoolSize = 4;
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();
+ }
+ }
}
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();
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
#ifndef V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
#define V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
+#include <map>
+#include <queue>
#include <vector>
#include "include/v8-platform.h"
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;
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);
};
'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',
--- /dev/null
+// 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()));
+}