Revision: 18381
Author:   [email protected]
Date:     Fri Dec 20 08:34:42 2013 UTC
Log: Lazily initialize thread pool to avoid allocating all the threads during startup

[email protected]
BUG=none
LOG=n

Review URL: https://codereview.chromium.org/98123004
http://code.google.com/p/v8/source/detail?r=18381

Modified:
 /branches/bleeding_edge/src/libplatform/default-platform.cc
 /branches/bleeding_edge/src/libplatform/default-platform.h

=======================================
--- /branches/bleeding_edge/src/libplatform/default-platform.cc Fri Dec 20 07:52:58 2013 UTC +++ /branches/bleeding_edge/src/libplatform/default-platform.cc Fri Dec 20 08:34:42 2013 UTC
@@ -40,7 +40,7 @@


 DefaultPlatform::DefaultPlatform()
-    : initialized_(false) {}
+    : initialized_(false), thread_pool_size_(0) {}


 DefaultPlatform::~DefaultPlatform() {
@@ -58,24 +58,24 @@
 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);
+  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_));
 }

 void DefaultPlatform::CallOnBackgroundThread(Task *task,
ExpectedRuntime expected_runtime) {
-#ifdef DEBUG
-  {
-    LockGuard<Mutex> guard(&lock_);
-    ASSERT(initialized_);
-  }
-#endif
+  EnsureInitialized();
   queue_.Append(task);
 }

=======================================
--- /branches/bleeding_edge/src/libplatform/default-platform.h Fri Dec 20 07:52:58 2013 UTC +++ /branches/bleeding_edge/src/libplatform/default-platform.h Fri Dec 20 08:34:42 2013 UTC
@@ -59,8 +59,11 @@
  private:
   static const int kMaxThreadPoolSize = 4;

+  void EnsureInitialized();
+
   Mutex lock_;
   bool initialized_;
+  int thread_pool_size_;
   std::vector<WorkerThread*> thread_pool_;
   TaskQueue queue_;

--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to