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.

117 lines
3.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_TASKRUNNER_H__
  28. #define TALK_BASE_TASKRUNNER_H__
  29. #include <vector>
  30. #include "talk/base/basictypes.h"
  31. #include "talk/base/sigslot.h"
  32. #include "talk/base/taskparent.h"
  33. namespace talk_base {
  34. class Task;
  35. const int64 kSecToMsec = 1000;
  36. const int64 kMsecTo100ns = 10000;
  37. const int64 kSecTo100ns = kSecToMsec * kMsecTo100ns;
  38. class TaskRunner : public TaskParent, public sigslot::has_slots<> {
  39. public:
  40. TaskRunner();
  41. virtual ~TaskRunner();
  42. virtual void WakeTasks() = 0;
  43. // Returns the current time in 100ns units. It is used for
  44. // determining timeouts. The origin is not important, only
  45. // the units and that rollover while the computer is running.
  46. //
  47. // On Windows, GetSystemTimeAsFileTime is the typical implementation.
  48. virtual int64 CurrentTime() = 0 ;
  49. void StartTask(Task *task);
  50. void RunTasks();
  51. void PollTasks();
  52. void UpdateTaskTimeout(Task *task, int64 previous_task_timeout_time);
  53. #ifdef _DEBUG
  54. bool is_ok_to_delete(Task* task) {
  55. return task == deleting_task_;
  56. }
  57. void IncrementAbortCount() {
  58. ++abort_count_;
  59. }
  60. void DecrementAbortCount() {
  61. --abort_count_;
  62. }
  63. #endif
  64. // Returns the next absolute time when a task times out
  65. // OR "0" if there is no next timeout.
  66. int64 next_task_timeout() const;
  67. protected:
  68. // The primary usage of this method is to know if
  69. // a callback timer needs to be set-up or adjusted.
  70. // This method will be called
  71. // * when the next_task_timeout() becomes a smaller value OR
  72. // * when next_task_timeout() has changed values and the previous
  73. // value is in the past.
  74. //
  75. // If the next_task_timeout moves to the future, this method will *not*
  76. // get called (because it subclass should check next_task_timeout()
  77. // when its timer goes off up to see if it needs to set-up a new timer).
  78. //
  79. // Note that this maybe called conservatively. In that it may be
  80. // called when no time change has happened.
  81. virtual void OnTimeoutChange() {
  82. // by default, do nothing.
  83. }
  84. private:
  85. void InternalRunTasks(bool in_destructor);
  86. void CheckForTimeoutChange(int64 previous_timeout_time);
  87. std::vector<Task *> tasks_;
  88. Task *next_timeout_task_;
  89. bool tasks_running_;
  90. #ifdef _DEBUG
  91. int abort_count_;
  92. Task* deleting_task_;
  93. #endif
  94. void RecalcNextTimeout(Task *exclude_task);
  95. };
  96. } // namespace talk_base
  97. #endif // TASK_BASE_TASKRUNNER_H__