toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 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 | |
| 5 | #ifndef MEDIA_MIDI_TASK_SERVICE_H_ |
| 6 | #define MEDIA_MIDI_TASK_SERVICE_H_ |
| 7 | |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 8 | #include <memory> |
| 9 | #include <vector> |
| 10 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 11 | #include "base/callback_forward.h" |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
Robert Liao | a4512b6 | 2017-08-29 16:38:28 +0000 | [diff] [blame] | 13 | #include "base/sequence_checker.h" |
Robert Liao | f66a9cf | 2017-08-26 01:40:35 +0000 | [diff] [blame] | 14 | #include "base/synchronization/condition_variable.h" |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
Patrick Monette | 3f9156f | 2021-10-15 19:13:42 +0000 | [diff] [blame] | 16 | #include "base/task/single_thread_task_runner.h" |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 17 | #include "base/thread_annotations.h" |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 18 | #include "base/threading/thread.h" |
| 19 | #include "base/time/time.h" |
| 20 | #include "media/midi/midi_export.h" |
| 21 | |
| 22 | namespace midi { |
| 23 | |
| 24 | // TaskService manages TaskRunners that can be used in midi and provides |
| 25 | // functionalities to ensure thread safety. |
| 26 | class MIDI_EXPORT TaskService final { |
| 27 | public: |
| 28 | using RunnerId = size_t; |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 29 | using InstanceId = int64_t; |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 30 | |
| 31 | static constexpr RunnerId kDefaultRunnerId = 0; |
| 32 | |
| 33 | TaskService(); |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 34 | |
| 35 | TaskService(const TaskService&) = delete; |
| 36 | TaskService& operator=(const TaskService&) = delete; |
| 37 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 38 | ~TaskService(); |
| 39 | |
| 40 | // Issues an InstanceId internally to post tasks via PostBoundTask() and |
| 41 | // PostDelayedBoundTask() with the InstanceId. Once UnbindInstance() is |
| 42 | // called, tasks posted via these methods with unbind InstanceId won't be |
| 43 | // invoked any more. |
| 44 | // Returns true if call is bound or unbound correctly. Otherwise returns |
| 45 | // false, that happens when the BindInstance() is called twice without |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 46 | // unbinding the previous instance, or the UnbindInstance() is called without |
| 47 | // any successful BindInstance() call. |
Daniel Cheng | ba689ba | 2022-01-14 00:14:33 +0000 | [diff] [blame] | 48 | [[nodiscard]] bool BindInstance(); |
| 49 | [[nodiscard]] bool UnbindInstance(); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 50 | |
Takashi Toyoshima | 143f0fd | 2017-08-01 16:15:33 +0000 | [diff] [blame] | 51 | // Checks if the current thread belongs to the specified runner. |
| 52 | bool IsOnTaskRunner(RunnerId runner_id); |
| 53 | |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 54 | // Posts a task to run on a specified TaskRunner. |runner_id| should be a |
| 55 | // positive number that represents a dedicated thread on that |task| will run. |
| 56 | // |task| will run even without a bound instance. |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 57 | void PostStaticTask(RunnerId runner_id, base::OnceClosure task); |
| 58 | |
| 59 | // Posts a task to run on a specified TaskRunner, and ensures that the bound |
| 60 | // instance should not quit UnbindInstance() while a bound task is running. |
Takashi Toyoshima | 3f0ea8f | 2018-01-17 09:19:59 +0000 | [diff] [blame] | 61 | // |runner_id| should be |kDefaultRunnerId| or a positive number. If |
| 62 | // |kDefaultRunnerId| is specified, the task runs on the thread on which |
| 63 | // BindInstance() was called. |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 64 | void PostBoundTask(RunnerId runner, base::OnceClosure task); |
| 65 | void PostBoundDelayedTask(RunnerId runner_id, |
| 66 | base::OnceClosure task, |
| 67 | base::TimeDelta delay); |
| 68 | |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 69 | void OverflowInstanceIdForTesting(); |
| 70 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 71 | private: |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 72 | static constexpr TaskService::InstanceId kInvalidInstanceId = -1; |
| 73 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 74 | // Returns a SingleThreadTaskRunner reference. Each TaskRunner will be |
| 75 | // constructed on demand. |
| 76 | scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(RunnerId runner_id); |
| 77 | |
| 78 | // Helps to run a posted bound task on TaskRunner safely. |
| 79 | void RunTask(InstanceId instance_id, |
| 80 | RunnerId runner_id, |
| 81 | base::OnceClosure task); |
| 82 | |
Robert Liao | f66a9cf | 2017-08-26 01:40:35 +0000 | [diff] [blame] | 83 | // Returns true if |instance_id| is equal to |bound_instance_id_|. |
| 84 | bool IsInstanceIdStillBound(InstanceId instance_id); |
| 85 | |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 86 | // Holds InstanceId for the next bound instance, accessed on the BindInstance |
| 87 | // call thread without any protection. |
| 88 | InstanceId next_instance_id_ = kInvalidInstanceId; |
| 89 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 90 | // Keeps a TaskRunner for the thread that calls BindInstance() as a default |
| 91 | // task runner to run posted tasks. |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 92 | scoped_refptr<base::SingleThreadTaskRunner> default_task_runner_ |
| 93 | GUARDED_BY(lock_); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 94 | |
| 95 | // Holds threads to host SingleThreadTaskRunners. |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 96 | std::vector<std::unique_ptr<base::Thread>> threads_ GUARDED_BY(lock_); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 97 | |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 98 | // Holds InstanceId for the current bound instance. |
| 99 | InstanceId bound_instance_id_ GUARDED_BY(lock_) = kInvalidInstanceId; |
| 100 | |
| 101 | base::Lock lock_; |
Robert Liao | f66a9cf | 2017-08-26 01:40:35 +0000 | [diff] [blame] | 102 | |
| 103 | // Signalled when the number of tasks in flight is 0 and ensures that |
| 104 | // UnbindInstance() does not return until all tasks have completed. |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 105 | base::ConditionVariable no_tasks_in_flight_cv_ |
| 106 | GUARDED_BY(tasks_in_flight_lock_); |
Robert Liao | f66a9cf | 2017-08-26 01:40:35 +0000 | [diff] [blame] | 107 | |
| 108 | // Number of tasks in flight. |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 109 | int tasks_in_flight_ GUARDED_BY(tasks_in_flight_lock_) = 0; |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 110 | |
Takashi Toyoshima | 88b4ac0 | 2019-02-12 11:25:56 +0000 | [diff] [blame] | 111 | base::Lock tasks_in_flight_lock_; |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 112 | |
Robert Liao | a4512b6 | 2017-08-29 16:38:28 +0000 | [diff] [blame] | 113 | // Verifies all UnbindInstance() calls occur on the same sequence as |
Takashi Toyoshima | d2bdc59 | 2017-09-13 10:02:54 +0000 | [diff] [blame] | 114 | // BindInstance(). |
Robert Liao | a4512b6 | 2017-08-29 16:38:28 +0000 | [diff] [blame] | 115 | SEQUENCE_CHECKER(instance_binding_sequence_checker_); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Nico Weber | a0faaf7 | 2019-02-07 19:07:54 +0000 | [diff] [blame] | 118 | } // namespace midi |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 119 | |
| 120 | #endif // MEDIA_MIDI_TASK_SERVICE_H_ |