deferred_sequenced_task_runner.h 3.3 KB

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