5 #ifndef V8_HEAP_ITEM_PARALLEL_JOB_H_ 6 #define V8_HEAP_ITEM_PARALLEL_JOB_H_ 11 #include "src/base/atomic-utils.h" 12 #include "src/base/logging.h" 13 #include "src/base/macros.h" 14 #include "src/base/optional.h" 15 #include "src/cancelable-task.h" 16 #include "src/counters.h" 17 #include "src/globals.h" 46 class V8_EXPORT_PRIVATE
Item {
49 virtual ~
Item() =
default;
52 void MarkFinished() { CHECK_EQ(kProcessing, state_.exchange(kFinished)); }
55 enum ProcessingState :
uintptr_t { kAvailable, kProcessing, kFinished };
57 bool TryMarkingAsProcessing() {
58 ProcessingState available = kAvailable;
59 return state_.compare_exchange_strong(available, kProcessing);
61 bool IsFinished() {
return state_ == kFinished; }
63 std::atomic<ProcessingState> state_{kAvailable};
68 DISALLOW_COPY_AND_ASSIGN(
Item);
76 virtual void RunInParallel() = 0;
82 template <
class ItemType>
84 while (items_considered_++ != items_->size()) {
86 if (cur_index_ == items_->size()) {
89 Item* item = (*items_)[cur_index_++];
90 if (item->TryMarkingAsProcessing()) {
91 return static_cast<ItemType*
>(item);
113 void RunInternal()
final;
115 std::vector<Item*>* items_ =
nullptr;
116 size_t cur_index_ = 0;
117 size_t items_considered_ = 0;
121 DISALLOW_COPY_AND_ASSIGN(
Task);
130 void AddTask(
Task* task) { tasks_.push_back(std::unique_ptr<Task>(task)); }
133 void AddItem(Item* item) { items_.push_back(item); }
135 int NumberOfItems()
const {
return static_cast<int>(items_.size()); }
136 int NumberOfTasks()
const {
return static_cast<int>(tasks_.size()); }
140 void Run(
const std::shared_ptr<Counters>& async_counters);
143 std::vector<Item*> items_;
144 std::vector<std::unique_ptr<Task>> tasks_;
145 CancelableTaskManager* cancelable_task_manager_;
146 base::Semaphore* pending_tasks_;
147 DISALLOW_COPY_AND_ASSIGN(ItemParallelJob);
153 #endif // V8_HEAP_ITEM_PARALLEL_JOB_H_