17 #include <glog/logging.h> 31 : start_ms(clock->GetMonotonicTime()),
36 should_remove(false) {}
44 : mutex_(is_worker ?
"TaskRunner worker" :
"TaskRunner main"),
46 waiting_(
"TaskRunner wait until finished"),
49 is_worker_(is_worker),
50 worker_(is_worker ?
"JS Worker" :
"JS Main Thread",
60 std::unique_lock<Mutex> lock(mutex_);
61 for (
auto& task : tasks_) {
69 return running_ && std::this_thread::get_id() == worker_.
get_id();
75 std::unique_lock<Mutex> lock(mutex_);
89 std::unique_lock<Mutex> lock(mutex_);
95 std::unique_lock<Mutex> lock(mutex_);
96 for (
auto& task : tasks_) {
98 task->should_remove =
true;
104 void TaskRunner::Run(std::function<
void(
RunLoop)> wrapper) {
126 void TaskRunner::OnIdle() {
132 bool TaskRunner::HandleTask() {
142 std::unique_lock<Mutex> lock(mutex_);
143 uint64_t min_time = std::numeric_limits<uint64_t>::max();
145 for (
auto it = tasks_.begin(); it != tasks_.end();) {
146 if ((*it)->should_remove) {
147 it = tasks_.erase(it);
149 if ((*it)->priority > max_priority) {
150 max_priority = (*it)->priority;
153 const uint64_t it_time = (*it)->
start_ms + (*it)->delay_ms;
154 if (it_time <= now && it_time < min_time) {
PendingTaskBase(const util::Clock *clock, TaskPriority priority, uint64_t delay_ms, int id, bool loop)
std::function< void()> RunLoop
TaskRunner(std::function< void(RunLoop)> wrapper, const util::Clock *clock, bool is_worker)
virtual ~PendingTaskBase()
T ResetAndWaitWhileUnlocked(std::unique_lock< _Mutex > &lock)
std::thread::id get_id() const
virtual void SleepSeconds(double seconds) const
std::atomic< bool > should_remove
virtual uint64_t GetMonotonicTime() const
bool BelongsToCurrentThread() const
bool HasPendingWork() const
v8::Isolate * GetIsolate()
void SetProvider(Thread *thread)