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 "src/base/logging.h" | 10 #include "src/base/logging.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 void DefaultPlatform::EnsureInitialized() { | 71 void DefaultPlatform::EnsureInitialized() { |
72 base::LockGuard<base::Mutex> guard(&lock_); | 72 base::LockGuard<base::Mutex> guard(&lock_); |
73 if (initialized_) return; | 73 if (initialized_) return; |
74 initialized_ = true; | 74 initialized_ = true; |
75 | 75 |
76 for (int i = 0; i < thread_pool_size_; ++i) | 76 for (int i = 0; i < thread_pool_size_; ++i) |
77 thread_pool_.push_back(new WorkerThread(&queue_)); | 77 thread_pool_.push_back(new WorkerThread(&queue_)); |
78 } | 78 } |
79 | 79 |
80 | 80 |
| 81 Task* DefaultPlatform::PopTaskInMainThreadQueue(v8::Isolate* isolate) { |
| 82 auto it = main_thread_queue_.find(isolate); |
| 83 if (it == main_thread_queue_.end() || it->second.empty()) { |
| 84 return NULL; |
| 85 } |
| 86 Task* task = it->second.front(); |
| 87 it->second.pop(); |
| 88 return task; |
| 89 } |
| 90 |
| 91 |
| 92 Task* DefaultPlatform::PopTaskInMainThreadDelayedQueue(v8::Isolate* isolate) { |
| 93 auto it = main_thread_delayed_queue_.find(isolate); |
| 94 if (it == main_thread_delayed_queue_.end() || it->second.empty()) { |
| 95 return NULL; |
| 96 } |
| 97 double now = MonotonicallyIncreasingTime(); |
| 98 std::pair<double, Task*> deadline_and_task = it->second.top(); |
| 99 if (deadline_and_task.first > now) { |
| 100 return NULL; |
| 101 } |
| 102 it->second.pop(); |
| 103 return deadline_and_task.second; |
| 104 } |
| 105 |
| 106 |
81 bool DefaultPlatform::PumpMessageLoop(v8::Isolate* isolate) { | 107 bool DefaultPlatform::PumpMessageLoop(v8::Isolate* isolate) { |
82 Task* task = NULL; | 108 Task* task = NULL; |
83 { | 109 { |
84 base::LockGuard<base::Mutex> guard(&lock_); | 110 base::LockGuard<base::Mutex> guard(&lock_); |
85 std::map<v8::Isolate*, std::queue<Task*> >::iterator it = | 111 |
86 main_thread_queue_.find(isolate); | 112 // Move delayed tasks that hit their deadline to the main queue. |
87 if (it == main_thread_queue_.end() || it->second.empty()) { | 113 task = PopTaskInMainThreadDelayedQueue(isolate); |
| 114 while (task != NULL) { |
| 115 main_thread_queue_[isolate].push(task); |
| 116 task = PopTaskInMainThreadDelayedQueue(isolate); |
| 117 } |
| 118 |
| 119 task = PopTaskInMainThreadQueue(isolate); |
| 120 |
| 121 if (task == NULL) { |
88 return false; | 122 return false; |
89 } | 123 } |
90 task = it->second.front(); | |
91 it->second.pop(); | |
92 } | 124 } |
93 task->Run(); | 125 task->Run(); |
94 delete task; | 126 delete task; |
95 return true; | 127 return true; |
96 } | 128 } |
97 | 129 |
| 130 |
98 void DefaultPlatform::CallOnBackgroundThread(Task *task, | 131 void DefaultPlatform::CallOnBackgroundThread(Task *task, |
99 ExpectedRuntime expected_runtime) { | 132 ExpectedRuntime expected_runtime) { |
100 EnsureInitialized(); | 133 EnsureInitialized(); |
101 queue_.Append(task); | 134 queue_.Append(task); |
102 } | 135 } |
103 | 136 |
104 | 137 |
105 void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) { | 138 void DefaultPlatform::CallOnForegroundThread(v8::Isolate* isolate, Task* task) { |
106 base::LockGuard<base::Mutex> guard(&lock_); | 139 base::LockGuard<base::Mutex> guard(&lock_); |
107 main_thread_queue_[isolate].push(task); | 140 main_thread_queue_[isolate].push(task); |
108 } | 141 } |
109 | 142 |
110 | 143 |
| 144 void DefaultPlatform::CallDelayedOnForegroundThread(Isolate* isolate, |
| 145 Task* task, |
| 146 double delay_in_seconds) { |
| 147 double deadline = MonotonicallyIncreasingTime() + delay_in_seconds; |
| 148 main_thread_delayed_queue_[isolate].push(std::make_pair(deadline, task)); |
| 149 } |
| 150 |
| 151 |
111 double DefaultPlatform::MonotonicallyIncreasingTime() { | 152 double DefaultPlatform::MonotonicallyIncreasingTime() { |
112 return base::TimeTicks::HighResolutionNow().ToInternalValue() / | 153 return base::TimeTicks::HighResolutionNow().ToInternalValue() / |
113 static_cast<double>(base::Time::kMicrosecondsPerSecond); | 154 static_cast<double>(base::Time::kMicrosecondsPerSecond); |
114 } | 155 } |
115 } } // namespace v8::platform | 156 } } // namespace v8::platform |
OLD | NEW |