taskrunner.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  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
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef THIRD_PARTY_LIBJINGLE_XMPP_TASK_RUNNER_TASKRUNNER_H_
  11. #define THIRD_PARTY_LIBJINGLE_XMPP_TASK_RUNNER_TASKRUNNER_H_
  12. #include <stdint.h>
  13. #include <vector>
  14. #include "base/check_op.h"
  15. #include "third_party/libjingle_xmpp/task_runner/taskparent.h"
  16. #include "third_party/webrtc/rtc_base/third_party/sigslot/sigslot.h"
  17. namespace jingle_xmpp {
  18. class Task;
  19. const int64_t kSecToMsec = 1000;
  20. const int64_t kMsecTo100ns = 10000;
  21. const int64_t kSecTo100ns = kSecToMsec * kMsecTo100ns;
  22. class TaskRunner : public TaskParent, public sigslot::has_slots<> {
  23. public:
  24. TaskRunner();
  25. ~TaskRunner() override;
  26. virtual void WakeTasks() = 0;
  27. void StartTask(Task *task);
  28. void RunTasks();
  29. #if DCHECK_IS_ON
  30. bool is_ok_to_delete(Task* task) {
  31. return task == deleting_task_;
  32. }
  33. void IncrementAbortCount() {
  34. ++abort_count_;
  35. }
  36. void DecrementAbortCount() {
  37. --abort_count_;
  38. }
  39. #endif
  40. // Returns the next absolute time when a task times out
  41. // OR "0" if there is no next timeout.
  42. int64_t next_task_timeout() const;
  43. protected:
  44. // The primary usage of this method is to know if
  45. // a callback timer needs to be set-up or adjusted.
  46. // This method will be called
  47. // * when the next_task_timeout() becomes a smaller value OR
  48. // * when next_task_timeout() has changed values and the previous
  49. // value is in the past.
  50. //
  51. // If the next_task_timeout moves to the future, this method will *not*
  52. // get called (because it subclass should check next_task_timeout()
  53. // when its timer goes off up to see if it needs to set-up a new timer).
  54. //
  55. // Note that this maybe called conservatively. In that it may be
  56. // called when no time change has happened.
  57. virtual void OnTimeoutChange() {
  58. // by default, do nothing.
  59. }
  60. private:
  61. void InternalRunTasks(bool in_destructor);
  62. std::vector<Task *> tasks_;
  63. bool tasks_running_ = false;
  64. #if DCHECK_IS_ON
  65. int abort_count_ = 0;
  66. Task* deleting_task_ = nullptr;
  67. #endif
  68. };
  69. } // namespace jingle_xmpp
  70. #endif // THIRD_PARTY_LIBJINGLE_XMPP_TASK_RUNNER_TASKRUNNER_H_