| OLD | NEW |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/libplatform/default-platform.h" | 5 #include "src/libplatform/default-platform.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <queue> | 8 #include <queue> |
| 9 | 9 |
| 10 #include "include/libplatform/libplatform.h" | 10 #include "include/libplatform/libplatform.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 platform->SetThreadPoolSize(thread_pool_size); | 23 platform->SetThreadPoolSize(thread_pool_size); |
| 24 platform->EnsureInitialized(); | 24 platform->EnsureInitialized(); |
| 25 return platform; | 25 return platform; |
| 26 } | 26 } |
| 27 | 27 |
| 28 | 28 |
| 29 bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate) { | 29 bool PumpMessageLoop(v8::Platform* platform, v8::Isolate* isolate) { |
| 30 return reinterpret_cast<DefaultPlatform*>(platform)->PumpMessageLoop(isolate); | 30 return reinterpret_cast<DefaultPlatform*>(platform)->PumpMessageLoop(isolate); |
| 31 } | 31 } |
| 32 | 32 |
| 33 void RunIdleTasks(v8::Platform* platform, v8::Isolate* isolate, |
| 34 double idle_time_in_seconds) { |
| 35 reinterpret_cast<DefaultPlatform*>(platform)->RunIdleTasks( |
| 36 isolate, idle_time_in_seconds); |
| 37 } |
| 38 |
| 33 void SetTracingController( | 39 void SetTracingController( |
| 34 v8::Platform* platform, | 40 v8::Platform* platform, |
| 35 v8::platform::tracing::TracingController* tracing_controller) { | 41 v8::platform::tracing::TracingController* tracing_controller) { |
| 36 return reinterpret_cast<DefaultPlatform*>(platform)->SetTracingController( | 42 return reinterpret_cast<DefaultPlatform*>(platform)->SetTracingController( |
| 37 tracing_controller); | 43 tracing_controller); |
| 38 } | 44 } |
| 39 | 45 |
| 40 const int DefaultPlatform::kMaxThreadPoolSize = 8; | 46 const int DefaultPlatform::kMaxThreadPoolSize = 8; |
| 41 | 47 |
| 42 DefaultPlatform::DefaultPlatform() | 48 DefaultPlatform::DefaultPlatform() |
| (...skipping 19 matching lines...) Expand all Loading... |
| 62 i->second.pop(); | 68 i->second.pop(); |
| 63 } | 69 } |
| 64 } | 70 } |
| 65 for (auto i = main_thread_delayed_queue_.begin(); | 71 for (auto i = main_thread_delayed_queue_.begin(); |
| 66 i != main_thread_delayed_queue_.end(); ++i) { | 72 i != main_thread_delayed_queue_.end(); ++i) { |
| 67 while (!i->second.empty()) { | 73 while (!i->second.empty()) { |
| 68 delete i->second.top().second; | 74 delete i->second.top().second; |
| 69 i->second.pop(); | 75 i->second.pop(); |
| 70 } | 76 } |
| 71 } | 77 } |
| 78 for (auto& i : main_thread_idle_queue_) { |
| 79 while (!i.second.empty()) { |
| 80 delete i.second.front(); |
| 81 i.second.pop(); |
| 82 } |
| 83 } |
| 72 } | 84 } |
| 73 | 85 |
| 74 | 86 |
| 75 void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) { | 87 void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) { |
| 76 base::LockGuard<base::Mutex> guard(&lock_); | 88 base::LockGuard<base::Mutex> guard(&lock_); |
| 77 DCHECK(thread_pool_size >= 0); | 89 DCHECK(thread_pool_size >= 0); |
| 78 if (thread_pool_size < 1) { | 90 if (thread_pool_size < 1) { |
| 79 thread_pool_size = base::SysInfo::NumberOfProcessors() - 1; | 91 thread_pool_size = base::SysInfo::NumberOfProcessors() - 1; |
| 80 } | 92 } |
| 81 thread_pool_size_ = | 93 thread_pool_size_ = |
| (...skipping 29 matching lines...) Expand all Loading... |
| 111 } | 123 } |
| 112 double now = MonotonicallyIncreasingTime(); | 124 double now = MonotonicallyIncreasingTime(); |
| 113 std::pair<double, Task*> deadline_and_task = it->second.top(); | 125 std::pair<double, Task*> deadline_and_task = it->second.top(); |
| 114 if (deadline_and_task.first > now) { | 126 if (deadline_and_task.first > now) { |
| 115 return NULL; | 127 return NULL; |
| 116 } | 128 } |
| 117 it->second.pop(); | 129 it->second.pop(); |
| 118 return deadline_and_task.second; | 130 return deadline_and_task.second; |
| 119 } | 131 } |
| 120 | 132 |
| 133 IdleTask* DefaultPlatform::PopTaskInMainThreadIdleQueue(v8::Isolate* isolate) { |
| 134 auto it = main_thread_idle_queue_.find(isolate); |
| 135 if (it == main_thread_idle_queue_.end() || it->second.empty()) { |
| 136 return nullptr; |
| 137 } |
| 138 IdleTask* task = it->second.front(); |
| 139 it->second.pop(); |
| 140 return task; |
| 141 } |
| 121 | 142 |
| 122 bool DefaultPlatform::PumpMessageLoop(v8::Isolate* isolate) { | 143 bool DefaultPlatform::PumpMessageLoop(v8::Isolate* isolate) { |
| 123 Task* task = NULL; | 144 Task* task = NULL; |
| 124 { | 145 { |
| 125 base::LockGuard<base::Mutex> guard(&lock_); | 146 base::LockGuard<base::Mutex> guard(&lock_); |
| 126 | 147 |
| 127 // Move delayed tasks that hit their deadline to the main queue. | 148 // Move delayed tasks that hit their deadline to the main queue. |
| 128 task = PopTaskInMainThreadDelayedQueue(isolate); | 149 task = PopTaskInMainThreadDelayedQueue(isolate); |
| 129 while (task != NULL) { | 150 while (task != NULL) { |
| 130 main_thread_queue_[isolate].push(task); | 151 main_thread_queue_[isolate].push(task); |
| 131 task = PopTaskInMainThreadDelayedQueue(isolate); | 152 task = PopTaskInMainThreadDelayedQueue(isolate); |
| 132 } | 153 } |
| 133 | 154 |
| 134 task = PopTaskInMainThreadQueue(isolate); | 155 task = PopTaskInMainThreadQueue(isolate); |
| 135 | 156 |
| 136 if (task == NULL) { | 157 if (task == NULL) { |
| 137 return false; | 158 return false; |
| 138 } | 159 } |
| 139 } | 160 } |
| 140 task->Run(); | 161 task->Run(); |
| 141 delete task; | 162 delete task; |
| 142 return true; | 163 return true; |
| 143 } | 164 } |
| 144 | 165 |
| 166 void DefaultPlatform::RunIdleTasks(v8::Isolate* isolate, |
| 167 double idle_time_in_seconds) { |
| 168 double deadline_in_seconds = |
| 169 MonotonicallyIncreasingTime() + idle_time_in_seconds; |
| 170 while (deadline_in_seconds > MonotonicallyIncreasingTime()) { |
| 171 { |
| 172 IdleTask* task; |
| 173 { |
| 174 base::LockGuard<base::Mutex> guard(&lock_); |
| 175 task = PopTaskInMainThreadIdleQueue(isolate); |
| 176 } |
| 177 if (task == nullptr) return; |
| 178 task->Run(deadline_in_seconds); |
| 179 delete task; |
| 180 } |
| 181 } |
| 182 } |
| 145 | 183 |
| 146 void DefaultPlatform::CallOnBackgroundThread(Task *task, | 184 void DefaultPlatform::CallOnBackgroundThread(Task* task, |
| 147 ExpectedRuntime expected_runtime) { | 185 ExpectedRuntime expected_runtime) { |
| 148 EnsureInitialized(); | 186 EnsureInitialized(); |
| 149 queue_.Append(task); | 187 queue_.Append(task); |
| 150 } | 188 } |
| 151 | 189 |
| 152 | 190 |
| 153 void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) { | 191 void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) { |
| 154 base::LockGuard<base::Mutex> guard(&lock_); | 192 base::LockGuard<base::Mutex> guard(&lock_); |
| 155 main_thread_queue_[isolate].push(task); | 193 main_thread_queue_[isolate].push(task); |
| 156 } | 194 } |
| 157 | 195 |
| 158 | 196 |
| 159 void DefaultPlatform::CallDelayedOnForegroundThread(Isolate* isolate, | 197 void DefaultPlatform::CallDelayedOnForegroundThread(Isolate* isolate, |
| 160 Task* task, | 198 Task* task, |
| 161 double delay_in_seconds) { | 199 double delay_in_seconds) { |
| 162 base::LockGuard<base::Mutex> guard(&lock_); | 200 base::LockGuard<base::Mutex> guard(&lock_); |
| 163 double deadline = MonotonicallyIncreasingTime() + delay_in_seconds; | 201 double deadline = MonotonicallyIncreasingTime() + delay_in_seconds; |
| 164 main_thread_delayed_queue_[isolate].push(std::make_pair(deadline, task)); | 202 main_thread_delayed_queue_[isolate].push(std::make_pair(deadline, task)); |
| 165 } | 203 } |
| 166 | 204 |
| 167 | |
| 168 void DefaultPlatform::CallIdleOnForegroundThread(Isolate* isolate, | 205 void DefaultPlatform::CallIdleOnForegroundThread(Isolate* isolate, |
| 169 IdleTask* task) { | 206 IdleTask* task) { |
| 170 UNREACHABLE(); | 207 base::LockGuard<base::Mutex> guard(&lock_); |
| 208 main_thread_idle_queue_[isolate].push(task); |
| 171 } | 209 } |
| 172 | 210 |
| 173 | 211 bool DefaultPlatform::IdleTasksEnabled(Isolate* isolate) { return true; } |
| 174 bool DefaultPlatform::IdleTasksEnabled(Isolate* isolate) { return false; } | |
| 175 | |
| 176 | 212 |
| 177 double DefaultPlatform::MonotonicallyIncreasingTime() { | 213 double DefaultPlatform::MonotonicallyIncreasingTime() { |
| 178 return base::TimeTicks::HighResolutionNow().ToInternalValue() / | 214 return base::TimeTicks::HighResolutionNow().ToInternalValue() / |
| 179 static_cast<double>(base::Time::kMicrosecondsPerSecond); | 215 static_cast<double>(base::Time::kMicrosecondsPerSecond); |
| 180 } | 216 } |
| 181 | 217 |
| 182 uint64_t DefaultPlatform::AddTraceEvent( | 218 uint64_t DefaultPlatform::AddTraceEvent( |
| 183 char phase, const uint8_t* category_enabled_flag, const char* name, | 219 char phase, const uint8_t* category_enabled_flag, const char* name, |
| 184 const char* scope, uint64_t id, uint64_t bind_id, int num_args, | 220 const char* scope, uint64_t id, uint64_t bind_id, int num_args, |
| 185 const char** arg_names, const uint8_t* arg_types, | 221 const char** arg_names, const uint8_t* arg_types, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 tracing_controller_->AddTraceStateObserver(observer); | 268 tracing_controller_->AddTraceStateObserver(observer); |
| 233 } | 269 } |
| 234 | 270 |
| 235 void DefaultPlatform::RemoveTraceStateObserver(TraceStateObserver* observer) { | 271 void DefaultPlatform::RemoveTraceStateObserver(TraceStateObserver* observer) { |
| 236 if (!tracing_controller_) return; | 272 if (!tracing_controller_) return; |
| 237 tracing_controller_->RemoveTraceStateObserver(observer); | 273 tracing_controller_->RemoveTraceStateObserver(observer); |
| 238 } | 274 } |
| 239 | 275 |
| 240 } // namespace platform | 276 } // namespace platform |
| 241 } // namespace v8 | 277 } // namespace v8 |
| OLD | NEW |