Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 | 12 |
| 13 #include "webrtc/base/taskrunner.h" | 13 #include "webrtc/base/taskrunner.h" |
| 14 | 14 |
| 15 #include "webrtc/base/checks.h" | |
| 15 #include "webrtc/base/common.h" | 16 #include "webrtc/base/common.h" |
| 16 #include "webrtc/base/task.h" | 17 #include "webrtc/base/task.h" |
| 17 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
| 18 | 19 |
| 19 namespace rtc { | 20 namespace rtc { |
| 20 | 21 |
| 21 TaskRunner::TaskRunner() | 22 TaskRunner::TaskRunner() |
| 22 : TaskParent(this), | 23 : TaskParent(this), |
| 23 next_timeout_task_(NULL), | 24 next_timeout_task_(NULL), |
| 24 tasks_running_(false) | 25 tasks_running_(false) |
| 25 #if !defined(NDEBUG) | 26 #if RTC_DCHECK_IS_ON |
| 26 , abort_count_(0), | 27 , |
|
kwiberg-webrtc
2017/01/12 02:10:41
You may want to use default member initializers to
nisse-webrtc
2017/01/12 10:53:24
Could make sense, but not in this cl.
Not sure if
kwiberg-webrtc
2017/01/12 11:00:38
I believe you can use either (but preferably not a
nisse-webrtc
2017/01/12 11:09:42
I'll change to using initializers, to make it less
kwiberg-webrtc
2017/01/12 12:54:30
Ah. Good that we're finally about to get serious w
| |
| 27 deleting_task_(NULL) | 28 abort_count_(0), |
| 29 deleting_task_(NULL) | |
| 28 #endif | 30 #endif |
| 29 { | 31 { |
| 30 } | 32 } |
| 31 | 33 |
| 32 TaskRunner::~TaskRunner() { | 34 TaskRunner::~TaskRunner() { |
| 33 // this kills and deletes children silently! | 35 // this kills and deletes children silently! |
| 34 AbortAllChildren(); | 36 AbortAllChildren(); |
| 35 InternalRunTasks(true); | 37 InternalRunTasks(true); |
| 36 } | 38 } |
| 37 | 39 |
| 38 void TaskRunner::StartTask(Task * task) { | 40 void TaskRunner::StartTask(Task * task) { |
| 39 tasks_.push_back(task); | 41 tasks_.push_back(task); |
| 40 | 42 |
| 41 // the task we just started could be about to timeout -- | 43 // the task we just started could be about to timeout -- |
| 42 // make sure our "next timeout task" is correct | 44 // make sure our "next timeout task" is correct |
| 43 UpdateTaskTimeout(task, 0); | 45 UpdateTaskTimeout(task, 0); |
| 44 | 46 |
| 45 WakeTasks(); | 47 WakeTasks(); |
| 46 } | 48 } |
| 47 | 49 |
| 48 void TaskRunner::RunTasks() { | 50 void TaskRunner::RunTasks() { |
| 49 InternalRunTasks(false); | 51 InternalRunTasks(false); |
| 50 } | 52 } |
| 51 | 53 |
| 52 void TaskRunner::InternalRunTasks(bool in_destructor) { | 54 void TaskRunner::InternalRunTasks(bool in_destructor) { |
| 53 // This shouldn't run while an abort is happening. | 55 // This shouldn't run while an abort is happening. |
| 54 // If that occurs, then tasks may be deleted in this method, | 56 // If that occurs, then tasks may be deleted in this method, |
| 55 // but pointers to them will still be in the | 57 // but pointers to them will still be in the |
| 56 // "ChildSet copy" in TaskParent::AbortAllChildren. | 58 // "ChildSet copy" in TaskParent::AbortAllChildren. |
| 57 // Subsequent use of those task may cause data corruption or crashes. | 59 // Subsequent use of those task may cause data corruption or crashes. |
| 58 ASSERT(!abort_count_); | 60 #if RTC_DCHECK_IS_ON |
| 61 RTC_DCHECK(!abort_count_); | |
| 62 #endif | |
| 59 // Running continues until all tasks are Blocked (ok for a small # of tasks) | 63 // Running continues until all tasks are Blocked (ok for a small # of tasks) |
| 60 if (tasks_running_) { | 64 if (tasks_running_) { |
| 61 return; // don't reenter | 65 return; // don't reenter |
| 62 } | 66 } |
| 63 | 67 |
| 64 tasks_running_ = true; | 68 tasks_running_ = true; |
| 65 | 69 |
| 66 int64_t previous_timeout_time = next_task_timeout(); | 70 int64_t previous_timeout_time = next_task_timeout(); |
| 67 | 71 |
| 68 int did_run = true; | 72 int did_run = true; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 80 bool need_timeout_recalc = false; | 84 bool need_timeout_recalc = false; |
| 81 for (size_t i = 0; i < tasks_.size(); ++i) { | 85 for (size_t i = 0; i < tasks_.size(); ++i) { |
| 82 if (tasks_[i]->IsDone()) { | 86 if (tasks_[i]->IsDone()) { |
| 83 Task* task = tasks_[i]; | 87 Task* task = tasks_[i]; |
| 84 if (next_timeout_task_ && | 88 if (next_timeout_task_ && |
| 85 task->unique_id() == next_timeout_task_->unique_id()) { | 89 task->unique_id() == next_timeout_task_->unique_id()) { |
| 86 next_timeout_task_ = NULL; | 90 next_timeout_task_ = NULL; |
| 87 need_timeout_recalc = true; | 91 need_timeout_recalc = true; |
| 88 } | 92 } |
| 89 | 93 |
| 90 #if !defined(NDEBUG) | 94 #if RTC_DCHECK_IS_ON |
| 91 deleting_task_ = task; | 95 deleting_task_ = task; |
| 92 #endif | 96 #endif |
| 93 delete task; | 97 delete task; |
| 94 #if !defined(NDEBUG) | 98 #if RTC_DCHECK_IS_ON |
| 95 deleting_task_ = NULL; | 99 deleting_task_ = NULL; |
| 96 #endif | 100 #endif |
| 97 tasks_[i] = NULL; | 101 tasks_[i] = NULL; |
| 98 } | 102 } |
| 99 } | 103 } |
| 100 // Finally, remove nulls | 104 // Finally, remove nulls |
| 101 std::vector<Task *>::iterator it; | 105 std::vector<Task *>::iterator it; |
| 102 it = std::remove(tasks_.begin(), | 106 it = std::remove(tasks_.begin(), |
| 103 tasks_.end(), | 107 tasks_.end(), |
| 104 reinterpret_cast<Task *>(NULL)); | 108 reinterpret_cast<Task *>(NULL)); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 | 147 |
| 144 // this function gets called frequently -- when each task changes | 148 // this function gets called frequently -- when each task changes |
| 145 // state to something other than DONE, ERROR or BLOCKED, it calls | 149 // state to something other than DONE, ERROR or BLOCKED, it calls |
| 146 // ResetTimeout(), which will call this function to make sure that | 150 // ResetTimeout(), which will call this function to make sure that |
| 147 // the next timeout-able task hasn't changed. The logic in this function | 151 // the next timeout-able task hasn't changed. The logic in this function |
| 148 // prevents RecalcNextTimeout() from getting called in most cases, | 152 // prevents RecalcNextTimeout() from getting called in most cases, |
| 149 // effectively making the task scheduler O-1 instead of O-N | 153 // effectively making the task scheduler O-1 instead of O-N |
| 150 | 154 |
| 151 void TaskRunner::UpdateTaskTimeout(Task* task, | 155 void TaskRunner::UpdateTaskTimeout(Task* task, |
| 152 int64_t previous_task_timeout_time) { | 156 int64_t previous_task_timeout_time) { |
| 153 ASSERT(task != NULL); | 157 RTC_DCHECK(task != NULL); |
| 154 int64_t previous_timeout_time = next_task_timeout(); | 158 int64_t previous_timeout_time = next_task_timeout(); |
| 155 bool task_is_timeout_task = next_timeout_task_ != NULL && | 159 bool task_is_timeout_task = next_timeout_task_ != NULL && |
| 156 task->unique_id() == next_timeout_task_->unique_id(); | 160 task->unique_id() == next_timeout_task_->unique_id(); |
| 157 if (task_is_timeout_task) { | 161 if (task_is_timeout_task) { |
| 158 previous_timeout_time = previous_task_timeout_time; | 162 previous_timeout_time = previous_task_timeout_time; |
| 159 } | 163 } |
| 160 | 164 |
| 161 // if the relevant task has a timeout, then | 165 // if the relevant task has a timeout, then |
| 162 // check to see if it's closer than the current | 166 // check to see if it's closer than the current |
| 163 // "about to timeout" task | 167 // "about to timeout" task |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 bool timeout_change = (previous_timeout_time == 0 && next_timeout != 0) || | 218 bool timeout_change = (previous_timeout_time == 0 && next_timeout != 0) || |
| 215 next_timeout < previous_timeout_time || | 219 next_timeout < previous_timeout_time || |
| 216 (previous_timeout_time <= CurrentTime() && | 220 (previous_timeout_time <= CurrentTime() && |
| 217 previous_timeout_time != next_timeout); | 221 previous_timeout_time != next_timeout); |
| 218 if (timeout_change) { | 222 if (timeout_change) { |
| 219 OnTimeoutChange(); | 223 OnTimeoutChange(); |
| 220 } | 224 } |
| 221 } | 225 } |
| 222 | 226 |
| 223 } // namespace rtc | 227 } // namespace rtc |
| OLD | NEW |