• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/libplatform/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 "src/checks.h"
12 #include "src/platform.h"
13 #include "src/libplatform/worker-thread.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 
19 const int DefaultPlatform::kMaxThreadPoolSize = 4;
20 
21 
DefaultPlatform()22 DefaultPlatform::DefaultPlatform()
23     : initialized_(false), thread_pool_size_(0) {}
24 
25 
~DefaultPlatform()26 DefaultPlatform::~DefaultPlatform() {
27   LockGuard<Mutex> guard(&lock_);
28   queue_.Terminate();
29   if (initialized_) {
30     for (std::vector<WorkerThread*>::iterator i = thread_pool_.begin();
31          i != thread_pool_.end(); ++i) {
32       delete *i;
33     }
34   }
35 }
36 
37 
SetThreadPoolSize(int thread_pool_size)38 void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) {
39   LockGuard<Mutex> guard(&lock_);
40   ASSERT(thread_pool_size >= 0);
41   if (thread_pool_size < 1)
42     thread_pool_size = OS::NumberOfProcessorsOnline();
43   thread_pool_size_ =
44       std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1);
45 }
46 
47 
EnsureInitialized()48 void DefaultPlatform::EnsureInitialized() {
49   LockGuard<Mutex> guard(&lock_);
50   if (initialized_) return;
51   initialized_ = true;
52 
53   for (int i = 0; i < thread_pool_size_; ++i)
54     thread_pool_.push_back(new WorkerThread(&queue_));
55 }
56 
CallOnBackgroundThread(Task * task,ExpectedRuntime expected_runtime)57 void DefaultPlatform::CallOnBackgroundThread(Task *task,
58                                              ExpectedRuntime expected_runtime) {
59   EnsureInitialized();
60   queue_.Append(task);
61 }
62 
63 
CallOnForegroundThread(v8::Isolate * isolate,Task * task)64 void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) {
65   // TODO(jochen): implement.
66   task->Run();
67   delete task;
68 }
69 
70 } }  // namespace v8::internal
71