Index: src/libplatform/default-platform.cc |
diff --git a/src/default-platform.cc b/src/libplatform/default-platform.cc |
similarity index 60% |
copy from src/default-platform.cc |
copy to src/libplatform/default-platform.cc |
index ef3c4ebd450bbc63ae99b4534a22fa5bd67a2f39..1101877c4b3f3601d5f818d9e5dfa3e62947023f 100644 |
--- a/src/default-platform.cc |
+++ b/src/libplatform/default-platform.cc |
@@ -25,24 +25,51 @@ |
// (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 "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 "task-queue.h" |
+#include "worker-thread.h" |
+ |
namespace v8 { |
namespace internal { |
+int DefaultPlatform::thread_pool_size_ = 0; |
+ |
-DefaultPlatform::DefaultPlatform() {} |
+DefaultPlatform::DefaultPlatform() : thread_pool_(NULL), queue_(NULL) {} |
-DefaultPlatform::~DefaultPlatform() {} |
+DefaultPlatform::~DefaultPlatform() { |
+ if (thread_pool_) { |
+ queue_->Terminate(); |
+ for (int i = 0; i < thread_pool_size_; ++i) { |
+ thread_pool_[i]->Join(); |
+ delete thread_pool_[i]; |
+ } |
+ delete[] thread_pool_; |
+ delete queue_; |
+ } |
+} |
+ |
+ |
+// static |
+void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) { |
+ ASSERT(thread_pool_size >= 0); |
+ // TODO(jochen): If this wasn't a static member, we should |
+ // ASSERT(!thread_pool_); |
+ thread_pool_size_ = thread_pool_size; |
+} |
void DefaultPlatform::CallOnBackgroundThread(Task *task, |
ExpectedRuntime expected_runtime) { |
- // TODO(jochen): implement. |
- task->Run(); |
- delete task; |
+ EnsureThreadPoolStarted(); |
+ queue_->Append(task); |
} |
@@ -53,4 +80,19 @@ void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) { |
} |
+void DefaultPlatform::EnsureThreadPoolStarted() { |
+ if (thread_pool_) return; |
+ queue_ = new TaskQueue; |
+ if (thread_pool_size_ < 1) |
+ thread_pool_size_ = CPU::NumberOfProcessorsOnline(); |
+ thread_pool_size_ = Max(Min(thread_pool_size_, 4), 1); |
Sven Panne
2013/12/05 10:04:11
Wouldn't it be better to do some sanity checks/cla
jochen (gone - plz use gerrit)
2013/12/05 12:58:53
We'd need to do the clamping and sanity check here
|
+ |
+ thread_pool_ = new WorkerThread*[thread_pool_size_]; |
+ |
+ for (int i = 0; i < thread_pool_size_; ++i) { |
+ thread_pool_[i] = new WorkerThread(queue_); |
+ thread_pool_[i]->Start(); |
+ } |
+} |
+ |
} } // namespace v8::internal |