blob: 1ac52f919f3f4889037512e332a5b07fba25146c [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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#include "src/base/logging.h"
11#include "src/base/platform/platform.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040012#include "src/base/platform/time.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013#include "src/base/sys-info.h"
14#include "src/libplatform/worker-thread.h"
15
16namespace v8 {
17namespace platform {
18
19
20v8::Platform* CreateDefaultPlatform(int thread_pool_size) {
21 DefaultPlatform* platform = new DefaultPlatform();
22 platform->SetThreadPoolSize(thread_pool_size);
23 platform->EnsureInitialized();
24 return platform;
25}
26
27
28bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate) {
29 return reinterpret_cast<DefaultPlatform*>(platform)->PumpMessageLoop(isolate);
30}
31
32
33const int DefaultPlatform::kMaxThreadPoolSize = 4;
34
35
36DefaultPlatform::DefaultPlatform()
37 : initialized_(false), thread_pool_size_(0) {}
38
39
40DefaultPlatform::~DefaultPlatform() {
41 base::LockGuard<base::Mutex> guard(&lock_);
42 queue_.Terminate();
43 if (initialized_) {
44 for (std::vector<WorkerThread*>::iterator i = thread_pool_.begin();
45 i != thread_pool_.end(); ++i) {
46 delete *i;
47 }
48 }
49 for (std::map<v8::Isolate*, std::queue<Task*> >::iterator i =
50 main_thread_queue_.begin();
51 i != main_thread_queue_.end(); ++i) {
52 while (!i->second.empty()) {
53 delete i->second.front();
54 i->second.pop();
55 }
56 }
57}
58
59
60void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) {
61 base::LockGuard<base::Mutex> guard(&lock_);
62 DCHECK(thread_pool_size >= 0);
63 if (thread_pool_size < 1) {
64 thread_pool_size = base::SysInfo::NumberOfProcessors();
65 }
66 thread_pool_size_ =
67 std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1);
68}
69
70
71void DefaultPlatform::EnsureInitialized() {
72 base::LockGuard<base::Mutex> guard(&lock_);
73 if (initialized_) return;
74 initialized_ = true;
75
76 for (int i = 0; i < thread_pool_size_; ++i)
77 thread_pool_.push_back(new WorkerThread(&queue_));
78}
79
80
81bool DefaultPlatform::PumpMessageLoop(v8::Isolate* isolate) {
82 Task* task = NULL;
83 {
84 base::LockGuard<base::Mutex> guard(&lock_);
85 std::map<v8::Isolate*, std::queue<Task*> >::iterator it =
86 main_thread_queue_.find(isolate);
87 if (it == main_thread_queue_.end() || it->second.empty()) {
88 return false;
89 }
90 task = it->second.front();
91 it->second.pop();
92 }
93 task->Run();
94 delete task;
95 return true;
96}
97
98void DefaultPlatform::CallOnBackgroundThread(Task *task,
99 ExpectedRuntime expected_runtime) {
100 EnsureInitialized();
101 queue_.Append(task);
102}
103
104
105void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) {
106 base::LockGuard<base::Mutex> guard(&lock_);
107 main_thread_queue_[isolate].push(task);
108}
109
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400110
111double DefaultPlatform::MonotonicallyIncreasingTime() {
112 return base::TimeTicks::HighResolutionNow().ToInternalValue() /
113 static_cast<double>(base::Time::kMicrosecondsPerSecond);
114}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115} } // namespace v8::platform