| 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 20 matching lines...) Expand all  Loading... | 
|   31 //                                        MemoryChunk* page, |   31 //                                        MemoryChunk* page, | 
|   32 //                                        PerPageData page_data) |   32 //                                        PerPageData page_data) | 
|   33 template <typename JobTraits> |   33 template <typename JobTraits> | 
|   34 class PageParallelJob { |   34 class PageParallelJob { | 
|   35  public: |   35  public: | 
|   36   PageParallelJob(Heap* heap, CancelableTaskManager* cancelable_task_manager) |   36   PageParallelJob(Heap* heap, CancelableTaskManager* cancelable_task_manager) | 
|   37       : heap_(heap), |   37       : heap_(heap), | 
|   38         cancelable_task_manager_(cancelable_task_manager), |   38         cancelable_task_manager_(cancelable_task_manager), | 
|   39         items_(nullptr), |   39         items_(nullptr), | 
|   40         num_items_(0), |   40         num_items_(0), | 
|   41         pending_tasks_(0) {} |   41         num_tasks_(0), | 
 |   42         pending_tasks_(new base::Semaphore(0)) {} | 
|   42  |   43  | 
|   43   ~PageParallelJob() { |   44   ~PageParallelJob() { | 
|   44     Item* item = items_; |   45     Item* item = items_; | 
|   45     while (item != nullptr) { |   46     while (item != nullptr) { | 
|   46       Item* next = item->next; |   47       Item* next = item->next; | 
|   47       delete item; |   48       delete item; | 
|   48       item = next; |   49       item = next; | 
|   49     } |   50     } | 
 |   51     delete pending_tasks_; | 
|   50   } |   52   } | 
|   51  |   53  | 
|   52   void AddPage(MemoryChunk* chunk, typename JobTraits::PerPageData data) { |   54   void AddPage(MemoryChunk* chunk, typename JobTraits::PerPageData data) { | 
|   53     Item* item = new Item(chunk, data, items_); |   55     Item* item = new Item(chunk, data, items_); | 
|   54     items_ = item; |   56     items_ = item; | 
|   55     ++num_items_; |   57     ++num_items_; | 
|   56   } |   58   } | 
|   57  |   59  | 
|   58   int NumberOfPages() { return num_items_; } |   60   int NumberOfPages() { return num_items_; } | 
|   59  |   61  | 
| (...skipping 14 matching lines...) Expand all  Loading... | 
|   74             V8::GetCurrentPlatform()->NumberOfAvailableBackgroundThreads())); |   76             V8::GetCurrentPlatform()->NumberOfAvailableBackgroundThreads())); | 
|   75     num_tasks_ = Max(1, Min(num_tasks, max_num_tasks)); |   77     num_tasks_ = Max(1, Min(num_tasks, max_num_tasks)); | 
|   76     int items_per_task = (num_items_ + num_tasks_ - 1) / num_tasks_; |   78     int items_per_task = (num_items_ + num_tasks_ - 1) / num_tasks_; | 
|   77     int start_index = 0; |   79     int start_index = 0; | 
|   78     Task* main_task = nullptr; |   80     Task* main_task = nullptr; | 
|   79     for (int i = 0; i < num_tasks_; i++, start_index += items_per_task) { |   81     for (int i = 0; i < num_tasks_; i++, start_index += items_per_task) { | 
|   80       if (start_index >= num_items_) { |   82       if (start_index >= num_items_) { | 
|   81         start_index -= num_items_; |   83         start_index -= num_items_; | 
|   82       } |   84       } | 
|   83       Task* task = new Task(heap_, items_, num_items_, start_index, |   85       Task* task = new Task(heap_, items_, num_items_, start_index, | 
|   84                             &pending_tasks_, per_task_data_callback(i)); |   86                             pending_tasks_, per_task_data_callback(i)); | 
|   85       task_ids[i] = task->id(); |   87       task_ids[i] = task->id(); | 
|   86       if (i > 0) { |   88       if (i > 0) { | 
|   87         V8::GetCurrentPlatform()->CallOnBackgroundThread( |   89         V8::GetCurrentPlatform()->CallOnBackgroundThread( | 
|   88             task, v8::Platform::kShortRunningTask); |   90             task, v8::Platform::kShortRunningTask); | 
|   89       } else { |   91       } else { | 
|   90         main_task = task; |   92         main_task = task; | 
|   91       } |   93       } | 
|   92     } |   94     } | 
|   93     // Contribute on main thread. |   95     // Contribute on main thread. | 
|   94     main_task->Run(); |   96     main_task->Run(); | 
|   95     delete main_task; |   97     delete main_task; | 
|   96     // Wait for background tasks. |   98     // Wait for background tasks. | 
|   97     for (int i = 0; i < num_tasks_; i++) { |   99     for (int i = 0; i < num_tasks_; i++) { | 
|   98       if (!cancelable_task_manager_->TryAbort(task_ids[i])) { |  100       if (!cancelable_task_manager_->TryAbort(task_ids[i])) { | 
|   99         pending_tasks_.Wait(); |  101         pending_tasks_->Wait(); | 
|  100       } |  102       } | 
|  101     } |  103     } | 
|  102     if (JobTraits::NeedSequentialFinalization) { |  104     if (JobTraits::NeedSequentialFinalization) { | 
|  103       Item* item = items_; |  105       Item* item = items_; | 
|  104       while (item != nullptr) { |  106       while (item != nullptr) { | 
|  105         bool success = (item->state.Value() == kFinished); |  107         bool success = (item->state.Value() == kFinished); | 
|  106         JobTraits::FinalizePageSequentially(heap_, item->chunk, success, |  108         JobTraits::FinalizePageSequentially(heap_, item->chunk, success, | 
|  107                                             item->data); |  109                                             item->data); | 
|  108         item = item->next; |  110         item = item->next; | 
|  109       } |  111       } | 
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  169     base::Semaphore* on_finish_; |  171     base::Semaphore* on_finish_; | 
|  170     typename JobTraits::PerTaskData data_; |  172     typename JobTraits::PerTaskData data_; | 
|  171     DISALLOW_COPY_AND_ASSIGN(Task); |  173     DISALLOW_COPY_AND_ASSIGN(Task); | 
|  172   }; |  174   }; | 
|  173  |  175  | 
|  174   Heap* heap_; |  176   Heap* heap_; | 
|  175   CancelableTaskManager* cancelable_task_manager_; |  177   CancelableTaskManager* cancelable_task_manager_; | 
|  176   Item* items_; |  178   Item* items_; | 
|  177   int num_items_; |  179   int num_items_; | 
|  178   int num_tasks_; |  180   int num_tasks_; | 
|  179   base::Semaphore pending_tasks_; |  181   base::Semaphore* pending_tasks_; | 
|  180   DISALLOW_COPY_AND_ASSIGN(PageParallelJob); |  182   DISALLOW_COPY_AND_ASSIGN(PageParallelJob); | 
|  181 }; |  183 }; | 
|  182  |  184  | 
|  183 }  // namespace internal |  185 }  // namespace internal | 
|  184 }  // namespace v8 |  186 }  // namespace v8 | 
|  185  |  187  | 
|  186 #endif  // V8_HEAP_PAGE_PARALLEL_JOB_ |  188 #endif  // V8_HEAP_PAGE_PARALLEL_JOB_ | 
| OLD | NEW |