1 // Copyright 2017 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_LIBPLATFORM_DEFAULT_FOREGROUND_TASK_RUNNER_H_ 6 #define V8_LIBPLATFORM_DEFAULT_FOREGROUND_TASK_RUNNER_H_ 7 8 #include <queue> 9 10 #include "include/libplatform/libplatform.h" 11 #include "include/v8-platform.h" 12 #include "src/base/platform/condition-variable.h" 13 #include "src/base/platform/mutex.h" 14 15 namespace v8 { 16 namespace platform { 17 18 class V8_PLATFORM_EXPORT DefaultForegroundTaskRunner NON_EXPORTED_BASE(TaskRunner)19 : public NON_EXPORTED_BASE(TaskRunner) { 20 public: 21 using TimeFunction = double (*)(); 22 23 DefaultForegroundTaskRunner(IdleTaskSupport idle_task_support, 24 TimeFunction time_function); 25 26 void Terminate(); 27 28 std::unique_ptr<Task> PopTaskFromQueue(MessageLoopBehavior wait_for_work); 29 30 std::unique_ptr<IdleTask> PopTaskFromIdleQueue(); 31 32 void WaitForTaskLocked(const base::LockGuard<base::Mutex>&); 33 34 double MonotonicallyIncreasingTime(); 35 36 // v8::TaskRunner implementation. 37 void PostTask(std::unique_ptr<Task> task) override; 38 39 void PostDelayedTask(std::unique_ptr<Task> task, 40 double delay_in_seconds) override; 41 42 void PostIdleTask(std::unique_ptr<IdleTask> task) override; 43 44 bool IdleTasksEnabled() override; 45 46 private: 47 // The same as PostTask, but the lock is already held by the caller. The 48 // {guard} parameter should make sure that the caller is holding the lock. 49 void PostTaskLocked(std::unique_ptr<Task> task, 50 const base::LockGuard<base::Mutex>&); 51 52 // A caller of this function has to hold {lock_}. The {guard} parameter should 53 // make sure that the caller is holding the lock. 54 std::unique_ptr<Task> PopTaskFromDelayedQueueLocked( 55 const base::LockGuard<base::Mutex>&); 56 57 bool terminated_ = false; 58 base::Mutex lock_; 59 base::ConditionVariable event_loop_control_; 60 std::queue<std::unique_ptr<Task>> task_queue_; 61 IdleTaskSupport idle_task_support_; 62 std::queue<std::unique_ptr<IdleTask>> idle_task_queue_; 63 64 // Some helper constructs for the {delayed_task_queue_}. 65 using DelayedEntry = std::pair<double, std::unique_ptr<Task>>; 66 // Define a comparison operator for the delayed_task_queue_ to make sure 67 // that the unique_ptr in the DelayedEntry is not accessed in the priority 68 // queue. This is necessary because we have to reset the unique_ptr when we 69 // remove a DelayedEntry from the priority queue. 70 struct DelayedEntryCompare { 71 bool operator()(DelayedEntry& left, DelayedEntry& right) { 72 return left.first > right.first; 73 } 74 }; 75 std::priority_queue<DelayedEntry, std::vector<DelayedEntry>, 76 DelayedEntryCompare> 77 delayed_task_queue_; 78 79 TimeFunction time_function_; 80 }; 81 82 } // namespace platform 83 } // namespace v8 84 #endif // V8_LIBPLATFORM_DEFAULT_FOREGROUND_TASK_RUNNER_H_ 85