Commit 1b93245a authored by jochen@chromium.org's avatar jochen@chromium.org

Lazily initialize thread pool to avoid allocating all the threads during startup

R=hpayer@chromium.org
BUG=none
LOG=n

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@18381 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent e6ef64c5
...@@ -40,7 +40,7 @@ namespace internal { ...@@ -40,7 +40,7 @@ namespace internal {
DefaultPlatform::DefaultPlatform() DefaultPlatform::DefaultPlatform()
: initialized_(false) {} : initialized_(false), thread_pool_size_(0) {}
DefaultPlatform::~DefaultPlatform() { DefaultPlatform::~DefaultPlatform() {
...@@ -58,24 +58,24 @@ DefaultPlatform::~DefaultPlatform() { ...@@ -58,24 +58,24 @@ DefaultPlatform::~DefaultPlatform() {
void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) { void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) {
LockGuard<Mutex> guard(&lock_); LockGuard<Mutex> guard(&lock_);
ASSERT(thread_pool_size >= 0); ASSERT(thread_pool_size >= 0);
if (initialized_) return;
initialized_ = true;
if (thread_pool_size < 1) if (thread_pool_size < 1)
thread_pool_size = CPU::NumberOfProcessorsOnline(); thread_pool_size = CPU::NumberOfProcessorsOnline();
thread_pool_size = Max(Min(thread_pool_size, kMaxThreadPoolSize), 1); thread_pool_size_ = Max(Min(thread_pool_size, kMaxThreadPoolSize), 1);
}
for (int i = 0; i < thread_pool_size; ++i)
void DefaultPlatform::EnsureInitialized() {
LockGuard<Mutex> guard(&lock_);
if (initialized_) return;
initialized_ = true;
for (int i = 0; i < thread_pool_size_; ++i)
thread_pool_.push_back(new WorkerThread(&queue_)); thread_pool_.push_back(new WorkerThread(&queue_));
} }
void DefaultPlatform::CallOnBackgroundThread(Task *task, void DefaultPlatform::CallOnBackgroundThread(Task *task,
ExpectedRuntime expected_runtime) { ExpectedRuntime expected_runtime) {
#ifdef DEBUG EnsureInitialized();
{
LockGuard<Mutex> guard(&lock_);
ASSERT(initialized_);
}
#endif
queue_.Append(task); queue_.Append(task);
} }
......
...@@ -59,8 +59,11 @@ class DefaultPlatform : public Platform { ...@@ -59,8 +59,11 @@ class DefaultPlatform : public Platform {
private: private:
static const int kMaxThreadPoolSize = 4; static const int kMaxThreadPoolSize = 4;
void EnsureInitialized();
Mutex lock_; Mutex lock_;
bool initialized_; bool initialized_;
int thread_pool_size_;
std::vector<WorkerThread*> thread_pool_; std::vector<WorkerThread*> thread_pool_;
TaskQueue queue_; TaskQueue queue_;
......
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