You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
7.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2006, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <algorithm>
  28. #include "talk/base/taskrunner.h"
  29. #include "talk/base/common.h"
  30. #include "talk/base/scoped_ptr.h"
  31. #include "talk/base/task.h"
  32. #include "talk/base/logging.h"
  33. namespace talk_base {
  34. TaskRunner::TaskRunner()
  35. : TaskParent(this),
  36. next_timeout_task_(NULL),
  37. tasks_running_(false)
  38. #ifdef _DEBUG
  39. , abort_count_(0),
  40. deleting_task_(NULL)
  41. #endif
  42. {
  43. }
  44. TaskRunner::~TaskRunner() {
  45. // this kills and deletes children silently!
  46. AbortAllChildren();
  47. InternalRunTasks(true);
  48. }
  49. void TaskRunner::StartTask(Task * task) {
  50. tasks_.push_back(task);
  51. // the task we just started could be about to timeout --
  52. // make sure our "next timeout task" is correct
  53. UpdateTaskTimeout(task, 0);
  54. WakeTasks();
  55. }
  56. void TaskRunner::RunTasks() {
  57. InternalRunTasks(false);
  58. }
  59. void TaskRunner::InternalRunTasks(bool in_destructor) {
  60. // This shouldn't run while an abort is happening.
  61. // If that occurs, then tasks may be deleted in this method,
  62. // but pointers to them will still be in the
  63. // "ChildSet copy" in TaskParent::AbortAllChildren.
  64. // Subsequent use of those task may cause data corruption or crashes.
  65. ASSERT(!abort_count_);
  66. // Running continues until all tasks are Blocked (ok for a small # of tasks)
  67. if (tasks_running_) {
  68. return; // don't reenter
  69. }
  70. tasks_running_ = true;
  71. int64 previous_timeout_time = next_task_timeout();
  72. int did_run = true;
  73. while (did_run) {
  74. did_run = false;
  75. // use indexing instead of iterators because tasks_ may grow
  76. for (size_t i = 0; i < tasks_.size(); ++i) {
  77. while (!tasks_[i]->Blocked()) {
  78. tasks_[i]->Step();
  79. did_run = true;
  80. }
  81. }
  82. }
  83. // Tasks are deleted when running has paused
  84. bool need_timeout_recalc = false;
  85. for (size_t i = 0; i < tasks_.size(); ++i) {
  86. if (tasks_[i]->IsDone()) {
  87. Task* task = tasks_[i];
  88. if (next_timeout_task_ &&
  89. task->unique_id() == next_timeout_task_->unique_id()) {
  90. next_timeout_task_ = NULL;
  91. need_timeout_recalc = true;
  92. }
  93. #ifdef _DEBUG
  94. deleting_task_ = task;
  95. #endif
  96. delete task;
  97. #ifdef _DEBUG
  98. deleting_task_ = NULL;
  99. #endif
  100. tasks_[i] = NULL;
  101. }
  102. }
  103. // Finally, remove nulls
  104. std::vector<Task *>::iterator it;
  105. it = std::remove(tasks_.begin(),
  106. tasks_.end(),
  107. reinterpret_cast<Task *>(NULL));
  108. tasks_.erase(it, tasks_.end());
  109. if (need_timeout_recalc)
  110. RecalcNextTimeout(NULL);
  111. // Make sure that adjustments are done to account
  112. // for any timeout changes (but don't call this
  113. // while being destroyed since it calls a pure virtual function).
  114. if (!in_destructor)
  115. CheckForTimeoutChange(previous_timeout_time);
  116. tasks_running_ = false;
  117. }
  118. void TaskRunner::PollTasks() {
  119. // see if our "next potentially timed-out task" has indeed timed out.
  120. // If it has, wake it up, then queue up the next task in line
  121. // Repeat while we have new timed-out tasks.
  122. // TODO: We need to guard against WakeTasks not updating
  123. // next_timeout_task_. Maybe also add documentation in the header file once
  124. // we understand this code better.
  125. Task* old_timeout_task = NULL;
  126. while (next_timeout_task_ &&
  127. old_timeout_task != next_timeout_task_ &&
  128. next_timeout_task_->TimedOut()) {
  129. old_timeout_task = next_timeout_task_;
  130. next_timeout_task_->Wake();
  131. WakeTasks();
  132. }
  133. }
  134. int64 TaskRunner::next_task_timeout() const {
  135. if (next_timeout_task_) {
  136. return next_timeout_task_->timeout_time();
  137. }
  138. return 0;
  139. }
  140. // this function gets called frequently -- when each task changes
  141. // state to something other than DONE, ERROR or BLOCKED, it calls
  142. // ResetTimeout(), which will call this function to make sure that
  143. // the next timeout-able task hasn't changed. The logic in this function
  144. // prevents RecalcNextTimeout() from getting called in most cases,
  145. // effectively making the task scheduler O-1 instead of O-N
  146. void TaskRunner::UpdateTaskTimeout(Task* task,
  147. int64 previous_task_timeout_time) {
  148. ASSERT(task != NULL);
  149. int64 previous_timeout_time = next_task_timeout();
  150. bool task_is_timeout_task = next_timeout_task_ != NULL &&
  151. task->unique_id() == next_timeout_task_->unique_id();
  152. if (task_is_timeout_task) {
  153. previous_timeout_time = previous_task_timeout_time;
  154. }
  155. // if the relevant task has a timeout, then
  156. // check to see if it's closer than the current
  157. // "about to timeout" task
  158. if (task->timeout_time()) {
  159. if (next_timeout_task_ == NULL ||
  160. (task->timeout_time() <= next_timeout_task_->timeout_time())) {
  161. next_timeout_task_ = task;
  162. }
  163. } else if (task_is_timeout_task) {
  164. // otherwise, if the task doesn't have a timeout,
  165. // and it used to be our "about to timeout" task,
  166. // walk through all the tasks looking for the real
  167. // "about to timeout" task
  168. RecalcNextTimeout(task);
  169. }
  170. // Note when task_running_, then the running routine
  171. // (TaskRunner::InternalRunTasks) is responsible for calling
  172. // CheckForTimeoutChange.
  173. if (!tasks_running_) {
  174. CheckForTimeoutChange(previous_timeout_time);
  175. }
  176. }
  177. void TaskRunner::RecalcNextTimeout(Task *exclude_task) {
  178. // walk through all the tasks looking for the one
  179. // which satisfies the following:
  180. // it's not finished already
  181. // we're not excluding it
  182. // it has the closest timeout time
  183. int64 next_timeout_time = 0;
  184. next_timeout_task_ = NULL;
  185. for (size_t i = 0; i < tasks_.size(); ++i) {
  186. Task *task = tasks_[i];
  187. // if the task isn't complete, and it actually has a timeout time
  188. if (!task->IsDone() && (task->timeout_time() > 0))
  189. // if it doesn't match our "exclude" task
  190. if (exclude_task == NULL ||
  191. exclude_task->unique_id() != task->unique_id())
  192. // if its timeout time is sooner than our current timeout time
  193. if (next_timeout_time == 0 ||
  194. task->timeout_time() <= next_timeout_time) {
  195. // set this task as our next-to-timeout
  196. next_timeout_time = task->timeout_time();
  197. next_timeout_task_ = task;
  198. }
  199. }
  200. }
  201. void TaskRunner::CheckForTimeoutChange(int64 previous_timeout_time) {
  202. int64 next_timeout = next_task_timeout();
  203. bool timeout_change = (previous_timeout_time == 0 && next_timeout != 0) ||
  204. next_timeout < previous_timeout_time ||
  205. (previous_timeout_time <= CurrentTime() &&
  206. previous_timeout_time != next_timeout);
  207. if (timeout_change) {
  208. OnTimeoutChange();
  209. }
  210. }
  211. } // namespace talk_base