deferred_sequenced_task_runner.h 3.3 KB

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