serial_runner.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) 2012 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 MEDIA_BASE_SERIAL_RUNNER_H_
  5. #define MEDIA_BASE_SERIAL_RUNNER_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/containers/circular_deque.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "media/base/media_export.h"
  12. #include "media/base/pipeline_status.h"
  13. namespace base {
  14. class SingleThreadTaskRunner;
  15. }
  16. namespace media {
  17. // Runs a series of bound functions accepting Closures or
  18. // PipelineStatusCallback. SerialRunner doesn't use regular
  19. // OnceClosure/PipelineStatusCallbacks as it late binds the completion callback
  20. // as the series progresses.
  21. class MEDIA_EXPORT SerialRunner {
  22. public:
  23. typedef base::OnceCallback<void(base::OnceClosure)> BoundClosure;
  24. typedef base::OnceCallback<void(PipelineStatusCallback)>
  25. BoundPipelineStatusCallback;
  26. // Serial queue of bound functions to run.
  27. class MEDIA_EXPORT Queue {
  28. public:
  29. Queue();
  30. Queue(Queue&& other);
  31. ~Queue();
  32. void Push(base::OnceClosure closure);
  33. void Push(BoundClosure bound_fn);
  34. void Push(BoundPipelineStatusCallback bound_fn);
  35. private:
  36. Queue(const Queue&) = delete;
  37. Queue& operator=(const Queue&) = delete;
  38. friend class SerialRunner;
  39. BoundPipelineStatusCallback Pop();
  40. bool empty();
  41. base::circular_deque<BoundPipelineStatusCallback> bound_fns_;
  42. };
  43. // Executes the bound functions in series, executing |done_cb| when finished.
  44. //
  45. // All bound functions are executed on the thread that Run() is called on,
  46. // including |done_cb|.
  47. //
  48. // To eliminate an unnecessary posted task, the first function is executed
  49. // immediately on the caller's stack. It is *strongly advised* to ensure
  50. // the calling code does no more work after the call to Run().
  51. //
  52. // In all cases, |done_cb| is guaranteed to execute on a separate calling
  53. // stack.
  54. //
  55. // Deleting the object will prevent execution of any unstarted bound
  56. // functions, including |done_cb|.
  57. static std::unique_ptr<SerialRunner> Run(Queue&& bound_fns,
  58. PipelineStatusCallback done_cb);
  59. SerialRunner(const SerialRunner&) = delete;
  60. SerialRunner& operator=(const SerialRunner&) = delete;
  61. private:
  62. friend std::default_delete<SerialRunner>;
  63. SerialRunner(Queue&& bound_fns, PipelineStatusCallback done_cb);
  64. ~SerialRunner();
  65. void RunNextInSeries(PipelineStatus last_status);
  66. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  67. Queue bound_fns_;
  68. PipelineStatusCallback done_cb_;
  69. // NOTE: Weak pointers must be invalidated before all other member variables.
  70. base::WeakPtrFactory<SerialRunner> weak_factory_{this};
  71. };
  72. } // namespace media
  73. #endif // MEDIA_BASE_SERIAL_RUNNER_H_