Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / v8 / src / libplatform / default-platform.cc
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "default-platform.h"
6
7 #include <algorithm>
8 #include <queue>
9
10 // TODO(jochen): We should have our own version of checks.h.
11 #include "../checks.h"
12 // TODO(jochen): Why is cpu.h not in platform/?
13 #include "../cpu.h"
14 #include "worker-thread.h"
15
16 namespace v8 {
17 namespace internal {
18
19
20 const int DefaultPlatform::kMaxThreadPoolSize = 4;
21
22
23 DefaultPlatform::DefaultPlatform()
24     : initialized_(false), thread_pool_size_(0) {}
25
26
27 DefaultPlatform::~DefaultPlatform() {
28   LockGuard<Mutex> guard(&lock_);
29   queue_.Terminate();
30   if (initialized_) {
31     for (std::vector<WorkerThread*>::iterator i = thread_pool_.begin();
32          i != thread_pool_.end(); ++i) {
33       delete *i;
34     }
35   }
36 }
37
38
39 void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) {
40   LockGuard<Mutex> guard(&lock_);
41   ASSERT(thread_pool_size >= 0);
42   if (thread_pool_size < 1)
43     thread_pool_size = CPU::NumberOfProcessorsOnline();
44   thread_pool_size_ =
45       std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1);
46 }
47
48
49 void DefaultPlatform::EnsureInitialized() {
50   LockGuard<Mutex> guard(&lock_);
51   if (initialized_) return;
52   initialized_ = true;
53
54   for (int i = 0; i < thread_pool_size_; ++i)
55     thread_pool_.push_back(new WorkerThread(&queue_));
56 }
57
58 void DefaultPlatform::CallOnBackgroundThread(Task *task,
59                                              ExpectedRuntime expected_runtime) {
60   EnsureInitialized();
61   queue_.Append(task);
62 }
63
64
65 void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) {
66   // TODO(jochen): implement.
67   task->Run();
68   delete task;
69 }
70
71 } }  // namespace v8::internal