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 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 } | 70 } |
71 } | 71 } |
72 } | 72 } |
73 // Tasks are deleted when running has paused | 73 // Tasks are deleted when running has paused |
74 bool need_timeout_recalc = false; | 74 bool need_timeout_recalc = false; |
75 for (size_t i = 0; i < tasks_.size(); ++i) { | 75 for (size_t i = 0; i < tasks_.size(); ++i) { |
76 if (tasks_[i]->IsDone()) { | 76 if (tasks_[i]->IsDone()) { |
77 Task* task = tasks_[i]; | 77 Task* task = tasks_[i]; |
78 if (next_timeout_task_ && | 78 if (next_timeout_task_ && |
79 task->unique_id() == next_timeout_task_->unique_id()) { | 79 task->unique_id() == next_timeout_task_->unique_id()) { |
80 next_timeout_task_ = NULL; | 80 next_timeout_task_ = nullptr; |
81 need_timeout_recalc = true; | 81 need_timeout_recalc = true; |
82 } | 82 } |
83 | 83 |
84 #if RTC_DCHECK_IS_ON | 84 #if RTC_DCHECK_IS_ON |
85 deleting_task_ = task; | 85 deleting_task_ = task; |
86 #endif | 86 #endif |
87 delete task; | 87 delete task; |
88 #if RTC_DCHECK_IS_ON | 88 #if RTC_DCHECK_IS_ON |
89 deleting_task_ = NULL; | 89 deleting_task_ = nullptr; |
90 #endif | 90 #endif |
91 tasks_[i] = NULL; | 91 tasks_[i] = nullptr; |
92 } | 92 } |
93 } | 93 } |
94 // Finally, remove nulls | 94 // Finally, remove nulls. |
95 std::vector<Task *>::iterator it; | 95 std::vector<Task *>::iterator it; |
96 it = std::remove(tasks_.begin(), | 96 it = std::remove(tasks_.begin(), tasks_.end(), nullptr); |
97 tasks_.end(), | |
98 reinterpret_cast<Task *>(NULL)); | |
99 | 97 |
100 tasks_.erase(it, tasks_.end()); | 98 tasks_.erase(it, tasks_.end()); |
101 | 99 |
102 if (need_timeout_recalc) | 100 if (need_timeout_recalc) |
103 RecalcNextTimeout(NULL); | 101 RecalcNextTimeout(nullptr); |
104 | 102 |
105 // Make sure that adjustments are done to account | 103 // Make sure that adjustments are done to account |
106 // for any timeout changes (but don't call this | 104 // for any timeout changes (but don't call this |
107 // while being destroyed since it calls a pure virtual function). | 105 // while being destroyed since it calls a pure virtual function). |
108 if (!in_destructor) | 106 if (!in_destructor) |
109 CheckForTimeoutChange(previous_timeout_time); | 107 CheckForTimeoutChange(previous_timeout_time); |
110 | 108 |
111 tasks_running_ = false; | 109 tasks_running_ = false; |
112 } | 110 } |
113 | 111 |
114 void TaskRunner::PollTasks() { | 112 void TaskRunner::PollTasks() { |
115 // see if our "next potentially timed-out task" has indeed timed out. | 113 // see if our "next potentially timed-out task" has indeed timed out. |
116 // If it has, wake it up, then queue up the next task in line | 114 // If it has, wake it up, then queue up the next task in line |
117 // Repeat while we have new timed-out tasks. | 115 // Repeat while we have new timed-out tasks. |
118 // TODO: We need to guard against WakeTasks not updating | 116 // TODO: We need to guard against WakeTasks not updating |
119 // next_timeout_task_. Maybe also add documentation in the header file once | 117 // next_timeout_task_. Maybe also add documentation in the header file once |
120 // we understand this code better. | 118 // we understand this code better. |
121 Task* old_timeout_task = NULL; | 119 Task* old_timeout_task = nullptr; |
122 while (next_timeout_task_ && | 120 while (next_timeout_task_ && |
123 old_timeout_task != next_timeout_task_ && | 121 old_timeout_task != next_timeout_task_ && |
124 next_timeout_task_->TimedOut()) { | 122 next_timeout_task_->TimedOut()) { |
125 old_timeout_task = next_timeout_task_; | 123 old_timeout_task = next_timeout_task_; |
126 next_timeout_task_->Wake(); | 124 next_timeout_task_->Wake(); |
127 WakeTasks(); | 125 WakeTasks(); |
128 } | 126 } |
129 } | 127 } |
130 | 128 |
131 int64_t TaskRunner::next_task_timeout() const { | 129 int64_t TaskRunner::next_task_timeout() const { |
132 if (next_timeout_task_) { | 130 if (next_timeout_task_) { |
133 return next_timeout_task_->timeout_time(); | 131 return next_timeout_task_->timeout_time(); |
134 } | 132 } |
135 return 0; | 133 return 0; |
136 } | 134 } |
137 | 135 |
138 // this function gets called frequently -- when each task changes | 136 // this function gets called frequently -- when each task changes |
139 // state to something other than DONE, ERROR or BLOCKED, it calls | 137 // state to something other than DONE, ERROR or BLOCKED, it calls |
140 // ResetTimeout(), which will call this function to make sure that | 138 // ResetTimeout(), which will call this function to make sure that |
141 // the next timeout-able task hasn't changed. The logic in this function | 139 // the next timeout-able task hasn't changed. The logic in this function |
142 // prevents RecalcNextTimeout() from getting called in most cases, | 140 // prevents RecalcNextTimeout() from getting called in most cases, |
143 // effectively making the task scheduler O-1 instead of O-N | 141 // effectively making the task scheduler O-1 instead of O-N |
144 | 142 |
145 void TaskRunner::UpdateTaskTimeout(Task* task, | 143 void TaskRunner::UpdateTaskTimeout(Task* task, |
146 int64_t previous_task_timeout_time) { | 144 int64_t previous_task_timeout_time) { |
147 RTC_DCHECK(task != NULL); | 145 RTC_DCHECK(task != nullptr); |
148 int64_t previous_timeout_time = next_task_timeout(); | 146 int64_t previous_timeout_time = next_task_timeout(); |
149 bool task_is_timeout_task = next_timeout_task_ != NULL && | 147 bool task_is_timeout_task = |
| 148 next_timeout_task_ != nullptr && |
150 task->unique_id() == next_timeout_task_->unique_id(); | 149 task->unique_id() == next_timeout_task_->unique_id(); |
151 if (task_is_timeout_task) { | 150 if (task_is_timeout_task) { |
152 previous_timeout_time = previous_task_timeout_time; | 151 previous_timeout_time = previous_task_timeout_time; |
153 } | 152 } |
154 | 153 |
155 // if the relevant task has a timeout, then | 154 // if the relevant task has a timeout, then |
156 // check to see if it's closer than the current | 155 // check to see if it's closer than the current |
157 // "about to timeout" task | 156 // "about to timeout" task |
158 if (task->timeout_time()) { | 157 if (task->timeout_time()) { |
159 if (next_timeout_task_ == NULL || | 158 if (next_timeout_task_ == nullptr || |
160 (task->timeout_time() <= next_timeout_task_->timeout_time())) { | 159 (task->timeout_time() <= next_timeout_task_->timeout_time())) { |
161 next_timeout_task_ = task; | 160 next_timeout_task_ = task; |
162 } | 161 } |
163 } else if (task_is_timeout_task) { | 162 } else if (task_is_timeout_task) { |
164 // otherwise, if the task doesn't have a timeout, | 163 // otherwise, if the task doesn't have a timeout, |
165 // and it used to be our "about to timeout" task, | 164 // and it used to be our "about to timeout" task, |
166 // walk through all the tasks looking for the real | 165 // walk through all the tasks looking for the real |
167 // "about to timeout" task | 166 // "about to timeout" task |
168 RecalcNextTimeout(task); | 167 RecalcNextTimeout(task); |
169 } | 168 } |
170 | 169 |
171 // Note when task_running_, then the running routine | 170 // Note when task_running_, then the running routine |
172 // (TaskRunner::InternalRunTasks) is responsible for calling | 171 // (TaskRunner::InternalRunTasks) is responsible for calling |
173 // CheckForTimeoutChange. | 172 // CheckForTimeoutChange. |
174 if (!tasks_running_) { | 173 if (!tasks_running_) { |
175 CheckForTimeoutChange(previous_timeout_time); | 174 CheckForTimeoutChange(previous_timeout_time); |
176 } | 175 } |
177 } | 176 } |
178 | 177 |
179 void TaskRunner::RecalcNextTimeout(Task *exclude_task) { | 178 void TaskRunner::RecalcNextTimeout(Task *exclude_task) { |
180 // walk through all the tasks looking for the one | 179 // walk through all the tasks looking for the one |
181 // which satisfies the following: | 180 // which satisfies the following: |
182 // it's not finished already | 181 // it's not finished already |
183 // we're not excluding it | 182 // we're not excluding it |
184 // it has the closest timeout time | 183 // it has the closest timeout time |
185 | 184 |
186 int64_t next_timeout_time = 0; | 185 int64_t next_timeout_time = 0; |
187 next_timeout_task_ = NULL; | 186 next_timeout_task_ = nullptr; |
188 | 187 |
189 for (size_t i = 0; i < tasks_.size(); ++i) { | 188 for (size_t i = 0; i < tasks_.size(); ++i) { |
190 Task *task = tasks_[i]; | 189 Task *task = tasks_[i]; |
191 // if the task isn't complete, and it actually has a timeout time | 190 // if the task isn't complete, and it actually has a timeout time |
192 if (!task->IsDone() && (task->timeout_time() > 0)) | 191 if (!task->IsDone() && (task->timeout_time() > 0)) |
193 // if it doesn't match our "exclude" task | 192 // if it doesn't match our "exclude" task |
194 if (exclude_task == NULL || | 193 if (exclude_task == nullptr || |
195 exclude_task->unique_id() != task->unique_id()) | 194 exclude_task->unique_id() != task->unique_id()) |
196 // if its timeout time is sooner than our current timeout time | 195 // if its timeout time is sooner than our current timeout time |
197 if (next_timeout_time == 0 || | 196 if (next_timeout_time == 0 || |
198 task->timeout_time() <= next_timeout_time) { | 197 task->timeout_time() <= next_timeout_time) { |
199 // set this task as our next-to-timeout | 198 // set this task as our next-to-timeout |
200 next_timeout_time = task->timeout_time(); | 199 next_timeout_time = task->timeout_time(); |
201 next_timeout_task_ = task; | 200 next_timeout_task_ = task; |
202 } | 201 } |
203 } | 202 } |
204 } | 203 } |
205 | 204 |
206 void TaskRunner::CheckForTimeoutChange(int64_t previous_timeout_time) { | 205 void TaskRunner::CheckForTimeoutChange(int64_t previous_timeout_time) { |
207 int64_t next_timeout = next_task_timeout(); | 206 int64_t next_timeout = next_task_timeout(); |
208 bool timeout_change = (previous_timeout_time == 0 && next_timeout != 0) || | 207 bool timeout_change = (previous_timeout_time == 0 && next_timeout != 0) || |
209 next_timeout < previous_timeout_time || | 208 next_timeout < previous_timeout_time || |
210 (previous_timeout_time <= CurrentTime() && | 209 (previous_timeout_time <= CurrentTime() && |
211 previous_timeout_time != next_timeout); | 210 previous_timeout_time != next_timeout); |
212 if (timeout_change) { | 211 if (timeout_change) { |
213 OnTimeoutChange(); | 212 OnTimeoutChange(); |
214 } | 213 } |
215 } | 214 } |
216 | 215 |
217 } // namespace rtc | 216 } // namespace rtc |
OLD | NEW |