| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef V8_HEAP_PAGE_PARALLEL_JOB_ | 5 #ifndef V8_HEAP_PAGE_PARALLEL_JOB_ |
| 6 #define V8_HEAP_PAGE_PARALLEL_JOB_ | 6 #define V8_HEAP_PAGE_PARALLEL_JOB_ |
| 7 | 7 |
| 8 #include "src/allocation.h" | 8 #include "src/allocation.h" |
| 9 #include "src/cancelable-task.h" | 9 #include "src/cancelable-task.h" |
| 10 #include "src/utils.h" | 10 #include "src/utils.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 } | 50 } |
| 51 | 51 |
| 52 void AddPage(MemoryChunk* chunk, typename JobTraits::PerPageData data) { | 52 void AddPage(MemoryChunk* chunk, typename JobTraits::PerPageData data) { |
| 53 Item* item = new Item(chunk, data, items_); | 53 Item* item = new Item(chunk, data, items_); |
| 54 items_ = item; | 54 items_ = item; |
| 55 ++num_items_; | 55 ++num_items_; |
| 56 } | 56 } |
| 57 | 57 |
| 58 int NumberOfPages() { return num_items_; } | 58 int NumberOfPages() { return num_items_; } |
| 59 | 59 |
| 60 // Runs the given number of tasks in parallel and processes the previosly | 60 // Returns the number of tasks that were spawned when running the job. |
| 61 int NumberOfTasks() { return num_tasks_; } |
| 62 |
| 63 // Runs the given number of tasks in parallel and processes the previously |
| 61 // added pages. This function blocks until all tasks finish. | 64 // added pages. This function blocks until all tasks finish. |
| 62 // The callback takes the index of a task and returns data for that task. | 65 // The callback takes the index of a task and returns data for that task. |
| 63 template <typename Callback> | 66 template <typename Callback> |
| 64 void Run(int num_tasks, Callback per_task_data_callback) { | 67 void Run(int num_tasks, Callback per_task_data_callback) { |
| 65 if (num_items_ == 0) return; | 68 if (num_items_ == 0) return; |
| 66 DCHECK_GE(num_tasks, 1); | 69 DCHECK_GE(num_tasks, 1); |
| 67 uint32_t task_ids[kMaxNumberOfTasks]; | 70 uint32_t task_ids[kMaxNumberOfTasks]; |
| 68 const int max_num_tasks = Min( | 71 const int max_num_tasks = Min( |
| 69 kMaxNumberOfTasks, | 72 kMaxNumberOfTasks, |
| 70 static_cast<int>( | 73 static_cast<int>( |
| 71 V8::GetCurrentPlatform()->NumberOfAvailableBackgroundThreads())); | 74 V8::GetCurrentPlatform()->NumberOfAvailableBackgroundThreads())); |
| 72 num_tasks = Max(1, Min(num_tasks, max_num_tasks)); | 75 num_tasks_ = Max(1, Min(num_tasks, max_num_tasks)); |
| 73 int items_per_task = (num_items_ + num_tasks - 1) / num_tasks; | 76 int items_per_task = (num_items_ + num_tasks_ - 1) / num_tasks_; |
| 74 int start_index = 0; | 77 int start_index = 0; |
| 75 Task* main_task = nullptr; | 78 Task* main_task = nullptr; |
| 76 for (int i = 0; i < num_tasks; i++, start_index += items_per_task) { | 79 for (int i = 0; i < num_tasks_; i++, start_index += items_per_task) { |
| 77 if (start_index >= num_items_) { | 80 if (start_index >= num_items_) { |
| 78 start_index -= num_items_; | 81 start_index -= num_items_; |
| 79 } | 82 } |
| 80 Task* task = new Task(heap_, items_, num_items_, start_index, | 83 Task* task = new Task(heap_, items_, num_items_, start_index, |
| 81 &pending_tasks_, per_task_data_callback(i)); | 84 &pending_tasks_, per_task_data_callback(i)); |
| 82 task_ids[i] = task->id(); | 85 task_ids[i] = task->id(); |
| 83 if (i > 0) { | 86 if (i > 0) { |
| 84 V8::GetCurrentPlatform()->CallOnBackgroundThread( | 87 V8::GetCurrentPlatform()->CallOnBackgroundThread( |
| 85 task, v8::Platform::kShortRunningTask); | 88 task, v8::Platform::kShortRunningTask); |
| 86 } else { | 89 } else { |
| 87 main_task = task; | 90 main_task = task; |
| 88 } | 91 } |
| 89 } | 92 } |
| 90 // Contribute on main thread. | 93 // Contribute on main thread. |
| 91 main_task->Run(); | 94 main_task->Run(); |
| 92 delete main_task; | 95 delete main_task; |
| 93 // Wait for background tasks. | 96 // Wait for background tasks. |
| 94 for (int i = 0; i < num_tasks; i++) { | 97 for (int i = 0; i < num_tasks_; i++) { |
| 95 if (!cancelable_task_manager_->TryAbort(task_ids[i])) { | 98 if (!cancelable_task_manager_->TryAbort(task_ids[i])) { |
| 96 pending_tasks_.Wait(); | 99 pending_tasks_.Wait(); |
| 97 } | 100 } |
| 98 } | 101 } |
| 99 if (JobTraits::NeedSequentialFinalization) { | 102 if (JobTraits::NeedSequentialFinalization) { |
| 100 Item* item = items_; | 103 Item* item = items_; |
| 101 while (item != nullptr) { | 104 while (item != nullptr) { |
| 102 bool success = (item->state.Value() == kFinished); | 105 bool success = (item->state.Value() == kFinished); |
| 103 JobTraits::FinalizePageSequentially(heap_, item->chunk, success, | 106 JobTraits::FinalizePageSequentially(heap_, item->chunk, success, |
| 104 item->data); | 107 item->data); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 int start_index_; | 168 int start_index_; |
| 166 base::Semaphore* on_finish_; | 169 base::Semaphore* on_finish_; |
| 167 typename JobTraits::PerTaskData data_; | 170 typename JobTraits::PerTaskData data_; |
| 168 DISALLOW_COPY_AND_ASSIGN(Task); | 171 DISALLOW_COPY_AND_ASSIGN(Task); |
| 169 }; | 172 }; |
| 170 | 173 |
| 171 Heap* heap_; | 174 Heap* heap_; |
| 172 CancelableTaskManager* cancelable_task_manager_; | 175 CancelableTaskManager* cancelable_task_manager_; |
| 173 Item* items_; | 176 Item* items_; |
| 174 int num_items_; | 177 int num_items_; |
| 178 int num_tasks_; |
| 175 base::Semaphore pending_tasks_; | 179 base::Semaphore pending_tasks_; |
| 176 DISALLOW_COPY_AND_ASSIGN(PageParallelJob); | 180 DISALLOW_COPY_AND_ASSIGN(PageParallelJob); |
| 177 }; | 181 }; |
| 178 | 182 |
| 179 } // namespace internal | 183 } // namespace internal |
| 180 } // namespace v8 | 184 } // namespace v8 |
| 181 | 185 |
| 182 #endif // V8_HEAP_PAGE_PARALLEL_JOB_ | 186 #endif // V8_HEAP_PAGE_PARALLEL_JOB_ |
| OLD | NEW |