deferred_sequenced_task_runner.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_
  5. #define BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_
  6. #include <vector>
  7. #include "base/base_export.h"
  8. #include "base/callback.h"
  9. #include "base/compiler_specific.h"
  10. #include "base/macros.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/sequenced_task_runner.h"
  13. #include "base/synchronization/lock.h"
  14. #include "base/threading/platform_thread.h"
  15. #include "base/time/time.h"
  16. namespace base {
  17. // A DeferredSequencedTaskRunner is a subclass of SequencedTaskRunner that
  18. // queues up all requests until the first call to Start() is issued.
  19. // DeferredSequencedTaskRunner may be created in two ways:
  20. // . with an explicit SequencedTaskRunner that the events are flushed to
  21. // . without a SequencedTaskRunner. In this configuration the
  22. // SequencedTaskRunner is supplied in StartWithTaskRunner().
  23. class BASE_EXPORT DeferredSequencedTaskRunner : public SequencedTaskRunner {
  24. public:
  25. explicit DeferredSequencedTaskRunner(
  26. scoped_refptr<SequencedTaskRunner> target_runner);
  27. // Use this constructor when you don't have the target SequencedTaskRunner.
  28. // When using this call StartWithTaskRunner().
  29. DeferredSequencedTaskRunner();
  30. // TaskRunner implementation
  31. bool PostDelayedTask(const Location& from_here,
  32. OnceClosure task,
  33. TimeDelta delay) override;
  34. bool RunsTasksInCurrentSequence() const override;
  35. // SequencedTaskRunner implementation
  36. bool PostNonNestableDelayedTask(const Location& from_here,
  37. OnceClosure task,
  38. TimeDelta delay) override;
  39. // Start the execution - posts all queued tasks to the target executor. The
  40. // deferred tasks are posted with their initial delay, meaning that the task
  41. // execution delay is actually measured from Start.
  42. // Fails when called a second time.
  43. void Start();
  44. // Same as Start(), but must be used with the no-arg constructor.
  45. void StartWithTaskRunner(
  46. scoped_refptr<SequencedTaskRunner> target_task_runner);
  47. private:
  48. struct DeferredTask {
  49. DeferredTask();
  50. DeferredTask(DeferredTask&& other);
  51. ~DeferredTask();
  52. DeferredTask& operator=(DeferredTask&& other);
  53. Location posted_from;
  54. OnceClosure task;
  55. // The delay this task was initially posted with.
  56. TimeDelta delay;
  57. bool is_non_nestable;
  58. };
  59. ~DeferredSequencedTaskRunner() override;
  60. // Both variants of Start() call into this.
  61. void StartImpl();
  62. // Creates a |Task| object and adds it to |deferred_tasks_queue_|.
  63. void QueueDeferredTask(const Location& from_here,
  64. OnceClosure task,
  65. TimeDelta delay,
  66. bool is_non_nestable);
  67. // // Protects |started_| and |deferred_tasks_queue_|.
  68. mutable Lock lock_;
  69. const PlatformThreadId created_thread_id_;
  70. bool started_ = false;
  71. scoped_refptr<SequencedTaskRunner> target_task_runner_;
  72. std::vector<DeferredTask> deferred_tasks_queue_;
  73. DISALLOW_COPY_AND_ASSIGN(DeferredSequencedTaskRunner);
  74. };
  75. } // namespace base
  76. #endif // BASE_DEFERRED_SEQUENCED_TASK_RUNNER_H_