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.

194 lines
6.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. #ifndef TALK_BASE_TASK_H__
  28. #define TALK_BASE_TASK_H__
  29. #include <string>
  30. #include "talk/base/basictypes.h"
  31. #include "talk/base/scoped_ptr.h"
  32. #include "talk/base/sigslot.h"
  33. #include "talk/base/taskparent.h"
  34. /////////////////////////////////////////////////////////////////////
  35. //
  36. // TASK
  37. //
  38. /////////////////////////////////////////////////////////////////////
  39. //
  40. // Task is a state machine infrastructure. States are pushed forward by
  41. // pushing forwards a TaskRunner that holds on to all Tasks. The purpose
  42. // of Task is threefold:
  43. //
  44. // (1) It manages ongoing work on the UI thread. Multitasking without
  45. // threads, keeping it easy, keeping it real. :-) It does this by
  46. // organizing a set of states for each task. When you return from your
  47. // Process*() function, you return an integer for the next state. You do
  48. // not go onto the next state yourself. Every time you enter a state,
  49. // you check to see if you can do anything yet. If not, you return
  50. // STATE_BLOCKED. If you _could_ do anything, do not return
  51. // STATE_BLOCKED - even if you end up in the same state, return
  52. // STATE_mysamestate. When you are done, return STATE_DONE and then the
  53. // task will self-delete sometime afterwards.
  54. //
  55. // (2) It helps you avoid all those reentrancy problems when you chain
  56. // too many triggers on one thread. Basically if you want to tell a task
  57. // to process something for you, you feed your task some information and
  58. // then you Wake() it. Don't tell it to process it right away. If it
  59. // might be working on something as you send it information, you may want
  60. // to have a queue in the task.
  61. //
  62. // (3) Finally it helps manage parent tasks and children. If a parent
  63. // task gets aborted, all the children tasks are too. The nice thing
  64. // about this, for example, is if you have one parent task that
  65. // represents, say, and Xmpp connection, then you can spawn a whole bunch
  66. // of infinite lifetime child tasks and now worry about cleaning them up.
  67. // When the parent task goes to STATE_DONE, the task engine will make
  68. // sure all those children are aborted and get deleted.
  69. //
  70. // Notice that Task has a few built-in states, e.g.,
  71. //
  72. // STATE_INIT - the task isn't running yet
  73. // STATE_START - the task is in its first state
  74. // STATE_RESPONSE - the task is in its second state
  75. // STATE_DONE - the task is done
  76. //
  77. // STATE_ERROR - indicates an error - we should audit the error code in
  78. // light of any usage of it to see if it should be improved. When I
  79. // first put down the task stuff I didn't have a good sense of what was
  80. // needed for Abort and Error, and now the subclasses of Task will ground
  81. // the design in a stronger way.
  82. //
  83. // STATE_NEXT - the first undefined state number. (like WM_USER) - you
  84. // can start defining more task states there.
  85. //
  86. // When you define more task states, just override Process(int state) and
  87. // add your own switch statement. If you want to delegate to
  88. // Task::Process, you can effectively delegate to its switch statement.
  89. // No fancy method pointers or such - this is all just pretty low tech,
  90. // easy to debug, and fast.
  91. //
  92. // Also notice that Task has some primitive built-in timeout functionality.
  93. //
  94. // A timeout is defined as "the task stays in STATE_BLOCKED longer than
  95. // timeout_seconds_."
  96. //
  97. // Descendant classes can override this behavior by calling the
  98. // various protected methods to change the timeout behavior. For
  99. // instance, a descendand might call SuspendTimeout() when it knows
  100. // that it isn't waiting for anything that might timeout, but isn't
  101. // yet in the STATE_DONE state.
  102. //
  103. namespace talk_base {
  104. // Executes a sequence of steps
  105. class Task : public TaskParent {
  106. public:
  107. Task(TaskParent *parent);
  108. virtual ~Task();
  109. int32 unique_id() { return unique_id_; }
  110. void Start();
  111. void Step();
  112. int GetState() const { return state_; }
  113. bool HasError() const { return (GetState() == STATE_ERROR); }
  114. bool Blocked() const { return blocked_; }
  115. bool IsDone() const { return done_; }
  116. int64 ElapsedTime();
  117. // Called from outside to stop task without any more callbacks
  118. void Abort(bool nowake = false);
  119. bool TimedOut();
  120. int64 timeout_time() const { return timeout_time_; }
  121. int timeout_seconds() const { return timeout_seconds_; }
  122. void set_timeout_seconds(int timeout_seconds);
  123. sigslot::signal0<> SignalTimeout;
  124. // Called inside the task to signal that the task may be unblocked
  125. void Wake();
  126. protected:
  127. enum {
  128. STATE_BLOCKED = -1,
  129. STATE_INIT = 0,
  130. STATE_START = 1,
  131. STATE_DONE = 2,
  132. STATE_ERROR = 3,
  133. STATE_RESPONSE = 4,
  134. STATE_NEXT = 5, // Subclasses which need more states start here and higher
  135. };
  136. // Called inside to advise that the task should wake and signal an error
  137. void Error();
  138. int64 CurrentTime();
  139. virtual std::string GetStateName(int state) const;
  140. virtual int Process(int state);
  141. virtual void Stop();
  142. virtual int ProcessStart() = 0;
  143. virtual int ProcessResponse() { return STATE_DONE; }
  144. void ResetTimeout();
  145. void ClearTimeout();
  146. void SuspendTimeout();
  147. void ResumeTimeout();
  148. protected:
  149. virtual int OnTimeout() {
  150. // by default, we are finished after timing out
  151. return STATE_DONE;
  152. }
  153. private:
  154. void Done();
  155. int state_;
  156. bool blocked_;
  157. bool done_;
  158. bool aborted_;
  159. bool busy_;
  160. bool error_;
  161. int64 start_time_;
  162. int64 timeout_time_;
  163. int timeout_seconds_;
  164. bool timeout_suspended_;
  165. int32 unique_id_;
  166. static int32 unique_id_seed_;
  167. };
  168. } // namespace talk_base
  169. #endif // TALK_BASE_TASK_H__