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

[platform] Implement a worker pool

We don't use the worker pool yet, however, there are tests. Yay. The
next step is to use the worker pool for parallel sweeping.

I've also started to move the platform related files into a sub
directory. The goal is to eventually build all the platform stuff as
a separate library which is used by d8 and cctest (and other embedders
that wish to use the default implementation) but not by chromium.

BUG=v8:3015
R=hpayer@chromium.org, svenpanne@chromium.org
LOG=n

Review URL: https://codereview.chromium.org/104583003

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@18380 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 0dbbedc3
......@@ -1082,6 +1082,10 @@ class Isolate {
bool IsDeferredHandle(Object** location);
#endif // DEBUG
int max_available_threads() const {
return max_available_threads_;
}
void set_max_available_threads(int value) {
max_available_threads_ = value;
}
......
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "default-platform.h"
#include <queue>
// TODO(jochen): We should have our own version of checks.h.
#include "../checks.h"
// TODO(jochen): Why is cpu.h not in platform/?
#include "../cpu.h"
#include "worker-thread.h"
namespace v8 {
namespace internal {
DefaultPlatform::DefaultPlatform()
: initialized_(false) {}
DefaultPlatform::~DefaultPlatform() {
LockGuard<Mutex> guard(&lock_);
if (initialized_) {
queue_.Terminate();
for (std::vector<WorkerThread*>::iterator i = thread_pool_.begin();
i != thread_pool_.end(); ++i) {
delete *i;
}
}
}
void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) {
LockGuard<Mutex> guard(&lock_);
ASSERT(thread_pool_size >= 0);
if (initialized_) return;
initialized_ = true;
if (thread_pool_size < 1)
thread_pool_size = CPU::NumberOfProcessorsOnline();
thread_pool_size = Max(Min(thread_pool_size, kMaxThreadPoolSize), 1);
for (int i = 0; i < thread_pool_size; ++i)
thread_pool_.push_back(new WorkerThread(&queue_));
}
void DefaultPlatform::CallOnBackgroundThread(Task *task,
ExpectedRuntime expected_runtime) {
#ifdef DEBUG
{
LockGuard<Mutex> guard(&lock_);
ASSERT(initialized_);
}
#endif
queue_.Append(task);
}
void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) {
// TODO(jochen): implement.
task->Run();
delete task;
}
} } // namespace v8::internal
......@@ -25,19 +25,31 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef V8_DEFAULT_PLATFORM_H_
#define V8_DEFAULT_PLATFORM_H_
#ifndef V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
#define V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
#include "v8.h"
#include <vector>
#include "../../include/v8-platform.h"
// TODO(jochen): We should have our own version of globals.h.
#include "../globals.h"
#include "../platform/mutex.h"
#include "task-queue.h"
namespace v8 {
namespace internal {
class TaskQueue;
class Thread;
class WorkerThread;
class DefaultPlatform : public Platform {
public:
DefaultPlatform();
virtual ~DefaultPlatform();
void SetThreadPoolSize(int thread_pool_size);
// v8::Platform implementation.
virtual void CallOnBackgroundThread(
Task *task, ExpectedRuntime expected_runtime) V8_OVERRIDE;
......@@ -45,6 +57,13 @@ class DefaultPlatform : public Platform {
Task *task) V8_OVERRIDE;
private:
static const int kMaxThreadPoolSize = 4;
Mutex lock_;
bool initialized_;
std::vector<WorkerThread*> thread_pool_;
TaskQueue queue_;
DISALLOW_COPY_AND_ASSIGN(DefaultPlatform);
};
......@@ -52,4 +71,4 @@ class DefaultPlatform : public Platform {
} } // namespace v8::internal
#endif // V8_DEFAULT_PLATFORM_H_
#endif // V8_LIBPLATFORM_DEFAULT_PLATFORM_H_
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "task-queue.h"
// TODO(jochen): We should have our own version of checks.h.
#include "../checks.h"
namespace v8 {
namespace internal {
TaskQueue::TaskQueue() : process_queue_semaphore_(0), terminated_(false) {}
TaskQueue::~TaskQueue() {
LockGuard<Mutex> guard(&lock_);
ASSERT(terminated_);
ASSERT(task_queue_.empty());
}
void TaskQueue::Append(Task* task) {
LockGuard<Mutex> guard(&lock_);
ASSERT(!terminated_);
task_queue_.push(task);
process_queue_semaphore_.Signal();
}
Task* TaskQueue::GetNext() {
for (;;) {
{
LockGuard<Mutex> guard(&lock_);
if (!task_queue_.empty()) {
Task* result = task_queue_.front();
task_queue_.pop();
return result;
}
if (terminated_) {
process_queue_semaphore_.Signal();
return NULL;
}
}
process_queue_semaphore_.Wait();
}
}
void TaskQueue::Terminate() {
LockGuard<Mutex> guard(&lock_);
ASSERT(!terminated_);
terminated_ = true;
process_queue_semaphore_.Signal();
}
} } // namespace v8::internal
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef V8_LIBPLATFORM_TASK_QUEUE_H_
#define V8_LIBPLATFORM_TASK_QUEUE_H_
#include <queue>
// TODO(jochen): We should have our own version of globals.h.
#include "../globals.h"
#include "../platform/mutex.h"
#include "../platform/semaphore.h"
namespace v8 {
class Task;
namespace internal {
class TaskQueue {
public:
TaskQueue();
~TaskQueue();
// Appends a task to the queue. The queue takes ownership of |task|.
void Append(Task* task);
// Returns the next task to process. Blocks if no task is available. Returns
// NULL if the queue is terminated.
Task* GetNext();
// Terminate the queue.
void Terminate();
private:
Mutex lock_;
Semaphore process_queue_semaphore_;
std::queue<Task*> task_queue_;
bool terminated_;
DISALLOW_COPY_AND_ASSIGN(TaskQueue);
};
} } // namespace v8::internal
#endif // V8_LIBPLATFORM_TASK_QUEUE_H_
......@@ -25,32 +25,32 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "v8.h"
#include "worker-thread.h"
#include "default-platform.h"
// TODO(jochen): We should have our own version of checks.h.
#include "../checks.h"
#include "../../include/v8-platform.h"
#include "task-queue.h"
namespace v8 {
namespace internal {
WorkerThread::WorkerThread(TaskQueue* queue)
: Thread("V8 WorkerThread"), queue_(queue) {
Start();
}
DefaultPlatform::DefaultPlatform() {}
DefaultPlatform::~DefaultPlatform() {}
void DefaultPlatform::CallOnBackgroundThread(Task *task,
ExpectedRuntime expected_runtime) {
// TODO(jochen): implement.
task->Run();
delete task;
WorkerThread::~WorkerThread() {
Join();
}
void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) {
// TODO(jochen): implement.
task->Run();
delete task;
void WorkerThread::Run() {
while (Task* task = queue_->GetNext()) {
task->Run();
delete task;
}
}
} } // namespace v8::internal
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef V8_LIBPLATFORM_WORKER_THREAD_H_
#define V8_LIBPLATFORM_WORKER_THREAD_H_
#include <queue>
// TODO(jochen): We should have our own version of globals.h.
#include "../globals.h"
#include "../platform.h"
namespace v8 {
namespace internal {
class TaskQueue;
class WorkerThread : public Thread {
public:
explicit WorkerThread(TaskQueue* queue);
virtual ~WorkerThread();
// Thread implementation.
virtual void Run() V8_OVERRIDE;
private:
friend class QuitTask;
TaskQueue* queue_;
DISALLOW_COPY_AND_ASSIGN(WorkerThread);
};
} } // namespace v8::internal
#endif // V8_LIBPLATFORM_WORKER_THREAD_H_
......@@ -32,13 +32,13 @@
#include "elements.h"
#include "bootstrapper.h"
#include "debug.h"
#ifdef V8_USE_DEFAULT_PLATFORM
#include "default-platform.h"
#endif
#include "deoptimizer.h"
#include "frames.h"
#include "heap-profiler.h"
#include "hydrogen.h"
#ifdef V8_USE_DEFAULT_PLATFORM
#include "libplatform/default-platform.h"
#endif
#include "lithium-allocator.h"
#include "objects.h"
#include "once.h"
......@@ -79,6 +79,11 @@ bool V8::Initialize(Deserializer* des) {
if (isolate->IsDead()) return false;
if (isolate->IsInitialized()) return true;
#ifdef V8_USE_DEFAULT_PLATFORM
DefaultPlatform* platform = static_cast<DefaultPlatform*>(platform_);
platform->SetThreadPoolSize(isolate->max_available_threads());
#endif
return isolate->Init(des);
}
......
......@@ -81,6 +81,8 @@
'test-hashmap.cc',
'test-heap.cc',
'test-heap-profiler.cc',
'test-libplatform-task-queue.cc',
'test-libplatform-worker-thread.cc',
'test-list.cc',
'test-liveedit.cc',
'test-lockers.cc',
......
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "v8.h"
#include "cctest.h"
#include "libplatform/task-queue.h"
#include "test-libplatform.h"
using namespace v8::internal;
TEST(TaskQueueBasic) {
TaskCounter task_counter;
TaskQueue queue;
TestTask* task = new TestTask(&task_counter);
queue.Append(task);
CHECK_EQ(1, task_counter.GetCount());
CHECK_EQ(task, queue.GetNext());
delete task;
CHECK_EQ(0, task_counter.GetCount());
queue.Terminate();
CHECK_EQ(NULL, queue.GetNext());
}
class ReadQueueTask : public TestTask {
public:
ReadQueueTask(TaskCounter* task_counter, TaskQueue* queue)
: TestTask(task_counter, true), queue_(queue) {}
virtual ~ReadQueueTask() {}
virtual void Run() V8_OVERRIDE {
TestTask::Run();
CHECK_EQ(NULL, queue_->GetNext());
}
private:
TaskQueue* queue_;
DISALLOW_COPY_AND_ASSIGN(ReadQueueTask);
};
TEST(TaskQueueTerminateMultipleReaders) {
TaskQueue queue;
TaskCounter task_counter;
ReadQueueTask* read1 = new ReadQueueTask(&task_counter, &queue);
ReadQueueTask* read2 = new ReadQueueTask(&task_counter, &queue);
TestWorkerThread thread1(read1);
TestWorkerThread thread2(read2);
thread1.Start();
thread2.Start();
CHECK_EQ(2, task_counter.GetCount());
thread1.Signal();
thread2.Signal();
queue.Terminate();
thread1.Join();
thread2.Join();
CHECK_EQ(0, task_counter.GetCount());
}
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "v8.h"
#include "cctest.h"
#include "libplatform/task-queue.h"
#include "libplatform/worker-thread.h"
#include "test-libplatform.h"
using namespace v8::internal;
TEST(WorkerThread) {
TaskQueue queue;
TaskCounter task_counter;
TestTask* task1 = new TestTask(&task_counter, true);
TestTask* task2 = new TestTask(&task_counter, true);
TestTask* task3 = new TestTask(&task_counter, true);
TestTask* task4 = new TestTask(&task_counter, true);
WorkerThread* thread1 = new WorkerThread(&queue);
WorkerThread* thread2 = new WorkerThread(&queue);
CHECK_EQ(4, task_counter.GetCount());
queue.Append(task1);
queue.Append(task2);
queue.Append(task3);
queue.Append(task4);
// TaskQueue ASSERTs that it is empty in its destructor.
queue.Terminate();
delete thread1;
delete thread2;
CHECK_EQ(0, task_counter.GetCount());
}
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef TEST_LIBPLATFORM_H_
#define TEST_LIBPLATFORM_H_
#include "v8.h"
#include "cctest.h"
using namespace v8::internal;
class TaskCounter {
public:
TaskCounter() : counter_(0) {}
~TaskCounter() { CHECK_EQ(0, counter_); }
int GetCount() const {
LockGuard<Mutex> guard(&lock_);
return counter_;
}
void Inc() {
LockGuard<Mutex> guard(&lock_);
++counter_;
}
void Dec() {
LockGuard<Mutex> guard(&lock_);
--counter_;
}
private:
mutable Mutex lock_;
int counter_;
DISALLOW_COPY_AND_ASSIGN(TaskCounter);
};
class TestTask : public v8::Task {
public:
TestTask(TaskCounter* task_counter, bool expected_to_run)
: task_counter_(task_counter),
expected_to_run_(expected_to_run),
executed_(false) {
task_counter_->Inc();
}
explicit TestTask(TaskCounter* task_counter)
: task_counter_(task_counter), expected_to_run_(false), executed_(false) {
task_counter_->Inc();
}
virtual ~TestTask() {
CHECK_EQ(expected_to_run_, executed_);
task_counter_->Dec();
}
// v8::Task implementation.
virtual void Run() V8_OVERRIDE { executed_ = true; }
private:
TaskCounter* task_counter_;
bool expected_to_run_;
bool executed_;
DISALLOW_COPY_AND_ASSIGN(TestTask);
};
class TestWorkerThread : public Thread {
public:
explicit TestWorkerThread(v8::Task* task)
: Thread("libplatform TestWorkerThread"), semaphore_(0), task_(task) {}
virtual ~TestWorkerThread() {}
void Signal() { semaphore_.Signal(); }
// Thread implementation.
virtual void Run() V8_OVERRIDE {
semaphore_.Wait();
if (task_) {
task_->Run();
delete task_;
}
}
private:
Semaphore semaphore_;
v8::Task* task_;
DISALLOW_COPY_AND_ASSIGN(TestWorkerThread);
};
#endif // TEST_LIBPLATFORM_H_
......@@ -313,8 +313,6 @@
'../../src/debug-agent.h',
'../../src/debug.cc',
'../../src/debug.h',
'../../src/default-platform.cc',
'../../src/default-platform.h',
'../../src/deoptimizer.cc',
'../../src/deoptimizer.h',
'../../src/disasm.h',
......@@ -446,6 +444,13 @@
'../../src/jsregexp.cc',
'../../src/jsregexp.h',
'../../src/lazy-instance.h',
# TODO(jochen): move libplatform/ files to their own target.
'../../src/libplatform/default-platform.cc',
'../../src/libplatform/default-platform.h',
'../../src/libplatform/task-queue.cc',
'../../src/libplatform/task-queue.h',
'../../src/libplatform/worker-thread.cc',
'../../src/libplatform/worker-thread.h',
'../../src/list-inl.h',
'../../src/list.h',
'../../src/lithium-allocator-inl.h',
......
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