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 tasks_running_(false) | |
25 #if !defined(NDEBUG) | |
26 , abort_count_(0), | |
27 deleting_task_(NULL) | |
28 #endif | |
29 { | |
30 } | |
31 | 24 |
32 TaskRunner::~TaskRunner() { | 25 TaskRunner::~TaskRunner() { |
33 // this kills and deletes children silently! | 26 // this kills and deletes children silently! |
34 AbortAllChildren(); | 27 AbortAllChildren(); |
35 InternalRunTasks(true); | 28 InternalRunTasks(true); |
36 } | 29 } |
37 | 30 |
38 void TaskRunner::StartTask(Task * task) { | 31 void TaskRunner::StartTask(Task * task) { |
39 tasks_.push_back(task); | 32 tasks_.push_back(task); |
40 | 33 |
41 // the task we just started could be about to timeout -- | 34 // the task we just started could be about to timeout -- |
42 // make sure our "next timeout task" is correct | 35 // make sure our "next timeout task" is correct |
43 UpdateTaskTimeout(task, 0); | 36 UpdateTaskTimeout(task, 0); |
44 | 37 |
45 WakeTasks(); | 38 WakeTasks(); |
46 } | 39 } |
47 | 40 |
48 void TaskRunner::RunTasks() { | 41 void TaskRunner::RunTasks() { |
49 InternalRunTasks(false); | 42 InternalRunTasks(false); |
50 } | 43 } |
51 | 44 |
52 void TaskRunner::InternalRunTasks(bool in_destructor) { | 45 void TaskRunner::InternalRunTasks(bool in_destructor) { |
53 // This shouldn't run while an abort is happening. | 46 // This shouldn't run while an abort is happening. |
54 // If that occurs, then tasks may be deleted in this method, | 47 // If that occurs, then tasks may be deleted in this method, |
55 // but pointers to them will still be in the | 48 // but pointers to them will still be in the |
56 // "ChildSet copy" in TaskParent::AbortAllChildren. | 49 // "ChildSet copy" in TaskParent::AbortAllChildren. |
57 // Subsequent use of those task may cause data corruption or crashes. | 50 // Subsequent use of those task may cause data corruption or crashes. |
58 ASSERT(!abort_count_); | 51 #if RTC_DCHECK_IS_ON |
| 52 RTC_DCHECK(!abort_count_); |
| 53 #endif |
59 // Running continues until all tasks are Blocked (ok for a small # of tasks) | 54 // Running continues until all tasks are Blocked (ok for a small # of tasks) |
60 if (tasks_running_) { | 55 if (tasks_running_) { |
61 return; // don't reenter | 56 return; // don't reenter |
62 } | 57 } |
63 | 58 |
64 tasks_running_ = true; | 59 tasks_running_ = true; |
65 | 60 |
66 int64_t previous_timeout_time = next_task_timeout(); | 61 int64_t previous_timeout_time = next_task_timeout(); |
67 | 62 |
68 int did_run = true; | 63 int did_run = true; |
(...skipping 11 matching lines...) Expand all Loading... |
80 bool need_timeout_recalc = false; | 75 bool need_timeout_recalc = false; |
81 for (size_t i = 0; i < tasks_.size(); ++i) { | 76 for (size_t i = 0; i < tasks_.size(); ++i) { |
82 if (tasks_[i]->IsDone()) { | 77 if (tasks_[i]->IsDone()) { |
83 Task* task = tasks_[i]; | 78 Task* task = tasks_[i]; |
84 if (next_timeout_task_ && | 79 if (next_timeout_task_ && |
85 task->unique_id() == next_timeout_task_->unique_id()) { | 80 task->unique_id() == next_timeout_task_->unique_id()) { |
86 next_timeout_task_ = NULL; | 81 next_timeout_task_ = NULL; |
87 need_timeout_recalc = true; | 82 need_timeout_recalc = true; |
88 } | 83 } |
89 | 84 |
90 #if !defined(NDEBUG) | 85 #if RTC_DCHECK_IS_ON |
91 deleting_task_ = task; | 86 deleting_task_ = task; |
92 #endif | 87 #endif |
93 delete task; | 88 delete task; |
94 #if !defined(NDEBUG) | 89 #if RTC_DCHECK_IS_ON |
95 deleting_task_ = NULL; | 90 deleting_task_ = NULL; |
96 #endif | 91 #endif |
97 tasks_[i] = NULL; | 92 tasks_[i] = NULL; |
98 } | 93 } |
99 } | 94 } |
100 // Finally, remove nulls | 95 // Finally, remove nulls |
101 std::vector<Task *>::iterator it; | 96 std::vector<Task *>::iterator it; |
102 it = std::remove(tasks_.begin(), | 97 it = std::remove(tasks_.begin(), |
103 tasks_.end(), | 98 tasks_.end(), |
104 reinterpret_cast<Task *>(NULL)); | 99 reinterpret_cast<Task *>(NULL)); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 138 |
144 // this function gets called frequently -- when each task changes | 139 // this function gets called frequently -- when each task changes |
145 // state to something other than DONE, ERROR or BLOCKED, it calls | 140 // state to something other than DONE, ERROR or BLOCKED, it calls |
146 // ResetTimeout(), which will call this function to make sure that | 141 // ResetTimeout(), which will call this function to make sure that |
147 // the next timeout-able task hasn't changed. The logic in this function | 142 // the next timeout-able task hasn't changed. The logic in this function |
148 // prevents RecalcNextTimeout() from getting called in most cases, | 143 // prevents RecalcNextTimeout() from getting called in most cases, |
149 // effectively making the task scheduler O-1 instead of O-N | 144 // effectively making the task scheduler O-1 instead of O-N |
150 | 145 |
151 void TaskRunner::UpdateTaskTimeout(Task* task, | 146 void TaskRunner::UpdateTaskTimeout(Task* task, |
152 int64_t previous_task_timeout_time) { | 147 int64_t previous_task_timeout_time) { |
153 ASSERT(task != NULL); | 148 RTC_DCHECK(task != NULL); |
154 int64_t previous_timeout_time = next_task_timeout(); | 149 int64_t previous_timeout_time = next_task_timeout(); |
155 bool task_is_timeout_task = next_timeout_task_ != NULL && | 150 bool task_is_timeout_task = next_timeout_task_ != NULL && |
156 task->unique_id() == next_timeout_task_->unique_id(); | 151 task->unique_id() == next_timeout_task_->unique_id(); |
157 if (task_is_timeout_task) { | 152 if (task_is_timeout_task) { |
158 previous_timeout_time = previous_task_timeout_time; | 153 previous_timeout_time = previous_task_timeout_time; |
159 } | 154 } |
160 | 155 |
161 // if the relevant task has a timeout, then | 156 // if the relevant task has a timeout, then |
162 // check to see if it's closer than the current | 157 // check to see if it's closer than the current |
163 // "about to timeout" task | 158 // "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) || | 209 bool timeout_change = (previous_timeout_time == 0 && next_timeout != 0) || |
215 next_timeout < previous_timeout_time || | 210 next_timeout < previous_timeout_time || |
216 (previous_timeout_time <= CurrentTime() && | 211 (previous_timeout_time <= CurrentTime() && |
217 previous_timeout_time != next_timeout); | 212 previous_timeout_time != next_timeout); |
218 if (timeout_change) { | 213 if (timeout_change) { |
219 OnTimeoutChange(); | 214 OnTimeoutChange(); |
220 } | 215 } |
221 } | 216 } |
222 | 217 |
223 } // namespace rtc | 218 } // namespace rtc |
OLD | NEW |