task_service.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2017 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_MIDI_TASK_SERVICE_H_
  5. #define MEDIA_MIDI_TASK_SERVICE_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/callback_forward.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/sequence_checker.h"
  11. #include "base/synchronization/condition_variable.h"
  12. #include "base/synchronization/lock.h"
  13. #include "base/task/single_thread_task_runner.h"
  14. #include "base/thread_annotations.h"
  15. #include "base/threading/thread.h"
  16. #include "base/time/time.h"
  17. #include "media/midi/midi_export.h"
  18. namespace midi {
  19. // TaskService manages TaskRunners that can be used in midi and provides
  20. // functionalities to ensure thread safety.
  21. class MIDI_EXPORT TaskService final {
  22. public:
  23. using RunnerId = size_t;
  24. using InstanceId = int64_t;
  25. static constexpr RunnerId kDefaultRunnerId = 0;
  26. TaskService();
  27. TaskService(const TaskService&) = delete;
  28. TaskService& operator=(const TaskService&) = delete;
  29. ~TaskService();
  30. // Issues an InstanceId internally to post tasks via PostBoundTask() and
  31. // PostDelayedBoundTask() with the InstanceId. Once UnbindInstance() is
  32. // called, tasks posted via these methods with unbind InstanceId won't be
  33. // invoked any more.
  34. // Returns true if call is bound or unbound correctly. Otherwise returns
  35. // false, that happens when the BindInstance() is called twice without
  36. // unbinding the previous instance, or the UnbindInstance() is called without
  37. // any successful BindInstance() call.
  38. [[nodiscard]] bool BindInstance();
  39. [[nodiscard]] bool UnbindInstance();
  40. // Checks if the current thread belongs to the specified runner.
  41. bool IsOnTaskRunner(RunnerId runner_id);
  42. // Posts a task to run on a specified TaskRunner. |runner_id| should be a
  43. // positive number that represents a dedicated thread on that |task| will run.
  44. // |task| will run even without a bound instance.
  45. void PostStaticTask(RunnerId runner_id, base::OnceClosure task);
  46. // Posts a task to run on a specified TaskRunner, and ensures that the bound
  47. // instance should not quit UnbindInstance() while a bound task is running.
  48. // |runner_id| should be |kDefaultRunnerId| or a positive number. If
  49. // |kDefaultRunnerId| is specified, the task runs on the thread on which
  50. // BindInstance() was called.
  51. void PostBoundTask(RunnerId runner, base::OnceClosure task);
  52. void PostBoundDelayedTask(RunnerId runner_id,
  53. base::OnceClosure task,
  54. base::TimeDelta delay);
  55. void OverflowInstanceIdForTesting();
  56. private:
  57. static constexpr TaskService::InstanceId kInvalidInstanceId = -1;
  58. // Returns a SingleThreadTaskRunner reference. Each TaskRunner will be
  59. // constructed on demand.
  60. scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(RunnerId runner_id);
  61. // Helps to run a posted bound task on TaskRunner safely.
  62. void RunTask(InstanceId instance_id,
  63. RunnerId runner_id,
  64. base::OnceClosure task);
  65. // Returns true if |instance_id| is equal to |bound_instance_id_|.
  66. bool IsInstanceIdStillBound(InstanceId instance_id);
  67. // Holds InstanceId for the next bound instance, accessed on the BindInstance
  68. // call thread without any protection.
  69. InstanceId next_instance_id_ = kInvalidInstanceId;
  70. // Keeps a TaskRunner for the thread that calls BindInstance() as a default
  71. // task runner to run posted tasks.
  72. scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_
  73. GUARDED_BY(lock_);
  74. // Holds threads to host SingleThreadTaskRunners.
  75. std::vector<std::unique_ptr<base::Thread>> threads_ GUARDED_BY(lock_);
  76. // Holds InstanceId for the current bound instance.
  77. InstanceId bound_instance_id_ GUARDED_BY(lock_) = kInvalidInstanceId;
  78. base::Lock lock_;
  79. // Signalled when the number of tasks in flight is 0 and ensures that
  80. // UnbindInstance() does not return until all tasks have completed.
  81. base::ConditionVariable no_tasks_in_flight_cv_
  82. GUARDED_BY(tasks_in_flight_lock_);
  83. // Number of tasks in flight.
  84. int tasks_in_flight_ GUARDED_BY(tasks_in_flight_lock_) = 0;
  85. base::Lock tasks_in_flight_lock_;
  86. // Verifies all UnbindInstance() calls occur on the same sequence as
  87. // BindInstance().
  88. SEQUENCE_CHECKER(instance_binding_sequence_checker_);
  89. };
  90. } // namespace midi
  91. #endif // MEDIA_MIDI_TASK_SERVICE_H_