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.

289 lines
7.3 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 "talk/base/task.h"
  28. #include "talk/base/common.h"
  29. #include "talk/base/taskrunner.h"
  30. namespace talk_base {
  31. int32 Task::unique_id_seed_ = 0;
  32. Task::Task(TaskParent *parent)
  33. : TaskParent(this, parent),
  34. state_(STATE_INIT),
  35. blocked_(false),
  36. done_(false),
  37. aborted_(false),
  38. busy_(false),
  39. error_(false),
  40. start_time_(0),
  41. timeout_time_(0),
  42. timeout_seconds_(0),
  43. timeout_suspended_(false) {
  44. unique_id_ = unique_id_seed_++;
  45. // sanity check that we didn't roll-over our id seed
  46. ASSERT(unique_id_ < unique_id_seed_);
  47. }
  48. Task::~Task() {
  49. // Is this task being deleted in the correct manner?
  50. ASSERT(!done_ || GetRunner()->is_ok_to_delete(this));
  51. ASSERT(state_ == STATE_INIT || done_);
  52. ASSERT(state_ == STATE_INIT || blocked_);
  53. // If the task is being deleted without being done, it
  54. // means that it hasn't been removed from its parent.
  55. // This happens if a task is deleted outside of TaskRunner.
  56. if (!done_) {
  57. Stop();
  58. }
  59. }
  60. int64 Task::CurrentTime() {
  61. return GetRunner()->CurrentTime();
  62. }
  63. int64 Task::ElapsedTime() {
  64. return CurrentTime() - start_time_;
  65. }
  66. void Task::Start() {
  67. if (state_ != STATE_INIT)
  68. return;
  69. // Set the start time before starting the task. Otherwise if the task
  70. // finishes quickly and deletes the Task object, setting start_time_
  71. // will crash.
  72. start_time_ = CurrentTime();
  73. GetRunner()->StartTask(this);
  74. }
  75. void Task::Step() {
  76. if (done_) {
  77. #ifdef _DEBUG
  78. // we do not know how !blocked_ happens when done_ - should be impossible.
  79. // But it causes problems, so in retail build, we force blocked_, and
  80. // under debug we assert.
  81. ASSERT(blocked_);
  82. #else
  83. blocked_ = true;
  84. #endif
  85. return;
  86. }
  87. // Async Error() was called
  88. if (error_) {
  89. done_ = true;
  90. state_ = STATE_ERROR;
  91. blocked_ = true;
  92. // obsolete - an errored task is not considered done now
  93. // SignalDone();
  94. Stop();
  95. #ifdef _DEBUG
  96. // verify that stop removed this from its parent
  97. ASSERT(!parent()->IsChildTask(this));
  98. #endif
  99. return;
  100. }
  101. busy_ = true;
  102. int new_state = Process(state_);
  103. busy_ = false;
  104. if (aborted_) {
  105. Abort(true); // no need to wake because we're awake
  106. return;
  107. }
  108. if (new_state == STATE_BLOCKED) {
  109. blocked_ = true;
  110. // Let the timeout continue
  111. } else {
  112. state_ = new_state;
  113. blocked_ = false;
  114. ResetTimeout();
  115. }
  116. if (new_state == STATE_DONE) {
  117. done_ = true;
  118. } else if (new_state == STATE_ERROR) {
  119. done_ = true;
  120. error_ = true;
  121. }
  122. if (done_) {
  123. // obsolete - call this yourself
  124. // SignalDone();
  125. Stop();
  126. #if _DEBUG
  127. // verify that stop removed this from its parent
  128. ASSERT(!parent()->IsChildTask(this));
  129. #endif
  130. blocked_ = true;
  131. }
  132. }
  133. void Task::Abort(bool nowake) {
  134. // Why only check for done_ (instead of "aborted_ || done_")?
  135. //
  136. // If aborted_ && !done_, it means the logic for aborting still
  137. // needs to be executed (because busy_ must have been true when
  138. // Abort() was previously called).
  139. if (done_)
  140. return;
  141. aborted_ = true;
  142. if (!busy_) {
  143. done_ = true;
  144. blocked_ = true;
  145. error_ = true;
  146. // "done_" is set before calling "Stop()" to ensure that this code
  147. // doesn't execute more than once (recursively) for the same task.
  148. Stop();
  149. #ifdef _DEBUG
  150. // verify that stop removed this from its parent
  151. ASSERT(!parent()->IsChildTask(this));
  152. #endif
  153. if (!nowake) {
  154. // WakeTasks to self-delete.
  155. // Don't call Wake() because it is a no-op after "done_" is set.
  156. // Even if Wake() did run, it clears "blocked_" which isn't desireable.
  157. GetRunner()->WakeTasks();
  158. }
  159. }
  160. }
  161. void Task::Wake() {
  162. if (done_)
  163. return;
  164. if (blocked_) {
  165. blocked_ = false;
  166. GetRunner()->WakeTasks();
  167. }
  168. }
  169. void Task::Error() {
  170. if (error_ || done_)
  171. return;
  172. error_ = true;
  173. Wake();
  174. }
  175. std::string Task::GetStateName(int state) const {
  176. switch (state) {
  177. case STATE_BLOCKED: return "BLOCKED";
  178. case STATE_INIT: return "INIT";
  179. case STATE_START: return "START";
  180. case STATE_DONE: return "DONE";
  181. case STATE_ERROR: return "ERROR";
  182. case STATE_RESPONSE: return "RESPONSE";
  183. }
  184. return "??";
  185. }
  186. int Task::Process(int state) {
  187. int newstate = STATE_ERROR;
  188. if (TimedOut()) {
  189. ClearTimeout();
  190. newstate = OnTimeout();
  191. SignalTimeout();
  192. } else {
  193. switch (state) {
  194. case STATE_INIT:
  195. newstate = STATE_START;
  196. break;
  197. case STATE_START:
  198. newstate = ProcessStart();
  199. break;
  200. case STATE_RESPONSE:
  201. newstate = ProcessResponse();
  202. break;
  203. case STATE_DONE:
  204. case STATE_ERROR:
  205. newstate = STATE_BLOCKED;
  206. break;
  207. }
  208. }
  209. return newstate;
  210. }
  211. void Task::Stop() {
  212. // No need to wake because we're either awake or in abort
  213. TaskParent::OnStopped(this);
  214. }
  215. void Task::set_timeout_seconds(const int timeout_seconds) {
  216. timeout_seconds_ = timeout_seconds;
  217. ResetTimeout();
  218. }
  219. bool Task::TimedOut() {
  220. return timeout_seconds_ &&
  221. timeout_time_ &&
  222. CurrentTime() >= timeout_time_;
  223. }
  224. void Task::ResetTimeout() {
  225. int64 previous_timeout_time = timeout_time_;
  226. bool timeout_allowed = (state_ != STATE_INIT)
  227. && (state_ != STATE_DONE)
  228. && (state_ != STATE_ERROR);
  229. if (timeout_seconds_ && timeout_allowed && !timeout_suspended_)
  230. timeout_time_ = CurrentTime() +
  231. (timeout_seconds_ * kSecToMsec * kMsecTo100ns);
  232. else
  233. timeout_time_ = 0;
  234. GetRunner()->UpdateTaskTimeout(this, previous_timeout_time);
  235. }
  236. void Task::ClearTimeout() {
  237. int64 previous_timeout_time = timeout_time_;
  238. timeout_time_ = 0;
  239. GetRunner()->UpdateTaskTimeout(this, previous_timeout_time);
  240. }
  241. void Task::SuspendTimeout() {
  242. if (!timeout_suspended_) {
  243. timeout_suspended_ = true;
  244. ResetTimeout();
  245. }
  246. }
  247. void Task::ResumeTimeout() {
  248. if (timeout_suspended_) {
  249. timeout_suspended_ = false;
  250. ResetTimeout();
  251. }
  252. }
  253. } // namespace talk_base