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 | #include "media/midi/task_service.h" |
| 6 | |
| 7 | #include "base/strings/stringprintf.h" |
| 8 | #include "base/threading/thread_task_runner_handle.h" |
| 9 | |
| 10 | namespace midi { |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | constexpr TaskService::InstanceId kInvalidInstanceId = -1; |
| 15 | |
| 16 | } // namespace |
| 17 | |
| 18 | TaskService::TaskService() |
Robert Liao | f66a9cf | 2017-08-26 01:40:35 +0000 | [diff] [blame] | 19 | : no_tasks_in_flight_cv_(&tasks_in_flight_lock_), |
| 20 | tasks_in_flight_(0), |
| 21 | next_instance_id_(0), |
Robert Liao | a4512b6 | 2017-08-29 16:38:28 +0000 | [diff] [blame^] | 22 | bound_instance_id_(kInvalidInstanceId) { |
| 23 | DETACH_FROM_SEQUENCE(instance_binding_sequence_checker_); |
| 24 | } |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 25 | |
| 26 | TaskService::~TaskService() { |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 27 | std::vector<std::unique_ptr<base::Thread>> threads; |
| 28 | { |
| 29 | base::AutoLock lock(lock_); |
| 30 | threads = std::move(threads_); |
| 31 | DCHECK_EQ(kInvalidInstanceId, bound_instance_id_); |
| 32 | } |
| 33 | // Should not have any lock to perform thread joins on thread destruction. |
| 34 | // All posted tasks should run before quitting the thread message loop. |
| 35 | threads.clear(); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | bool TaskService::BindInstance() { |
Robert Liao | a4512b6 | 2017-08-29 16:38:28 +0000 | [diff] [blame^] | 39 | DCHECK_CALLED_ON_VALID_SEQUENCE(instance_binding_sequence_checker_); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 40 | base::AutoLock lock(lock_); |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 41 | if (bound_instance_id_ != kInvalidInstanceId) |
| 42 | return false; |
| 43 | bound_instance_id_ = next_instance_id_++; |
| 44 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 45 | DCHECK(!default_task_runner_); |
| 46 | default_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | bool TaskService::UnbindInstance() { |
Robert Liao | a4512b6 | 2017-08-29 16:38:28 +0000 | [diff] [blame^] | 51 | DCHECK_CALLED_ON_VALID_SEQUENCE(instance_binding_sequence_checker_); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 52 | { |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 53 | base::AutoLock lock(lock_); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 54 | if (bound_instance_id_ == kInvalidInstanceId) |
| 55 | return false; |
| 56 | bound_instance_id_ = kInvalidInstanceId; |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 57 | |
| 58 | DCHECK(default_task_runner_); |
| 59 | default_task_runner_ = nullptr; |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 60 | } |
Robert Liao | f66a9cf | 2017-08-26 01:40:35 +0000 | [diff] [blame] | 61 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 62 | // From now on RunTask will never run any task bound to the instance id. |
Robert Liao | f66a9cf | 2017-08-26 01:40:35 +0000 | [diff] [blame] | 63 | // But invoked tasks might be still running here. To ensure no task runs on |
| 64 | // quitting this method, wait for all tasks to complete. |
| 65 | base::AutoLock tasks_in_flight_auto_lock(tasks_in_flight_lock_); |
| 66 | while (tasks_in_flight_ > 0) |
| 67 | no_tasks_in_flight_cv_.Wait(); |
| 68 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 69 | return true; |
| 70 | } |
| 71 | |
Takashi Toyoshima | 143f0fd | 2017-08-01 16:15:33 +0000 | [diff] [blame] | 72 | bool TaskService::IsOnTaskRunner(RunnerId runner_id) { |
| 73 | base::AutoLock lock(lock_); |
| 74 | if (bound_instance_id_ == kInvalidInstanceId) |
| 75 | return false; |
| 76 | |
| 77 | if (runner_id == kDefaultRunnerId) |
| 78 | return default_task_runner_->BelongsToCurrentThread(); |
| 79 | |
| 80 | size_t thread = runner_id - 1; |
| 81 | if (threads_.size() <= thread || !threads_[thread]) |
| 82 | return false; |
| 83 | |
| 84 | return threads_[thread]->task_runner()->BelongsToCurrentThread(); |
| 85 | } |
| 86 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 87 | void TaskService::PostStaticTask(RunnerId runner_id, base::OnceClosure task) { |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 88 | { |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 89 | // Disallow to post a task when no instance is bound, so that new threads |
| 90 | // can not be created after the thread finalization in the destructor. |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 91 | base::AutoLock lock(lock_); |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 92 | if (bound_instance_id_ == kInvalidInstanceId) |
| 93 | return; |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 94 | } |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 95 | scoped_refptr<base::SingleThreadTaskRunner> runner; |
| 96 | GetTaskRunner(runner_id)->PostTask(FROM_HERE, std::move(task)); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void TaskService::PostBoundTask(RunnerId runner_id, base::OnceClosure task) { |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 100 | InstanceId instance_id; |
| 101 | { |
| 102 | base::AutoLock lock(lock_); |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 103 | if (bound_instance_id_ == kInvalidInstanceId) |
| 104 | return; |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 105 | instance_id = bound_instance_id_; |
| 106 | } |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 107 | GetTaskRunner(runner_id)->PostTask( |
| 108 | FROM_HERE, base::BindOnce(&TaskService::RunTask, base::Unretained(this), |
| 109 | instance_id, runner_id, std::move(task))); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void TaskService::PostBoundDelayedTask(RunnerId runner_id, |
| 113 | base::OnceClosure task, |
| 114 | base::TimeDelta delay) { |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 115 | InstanceId instance_id; |
| 116 | { |
| 117 | base::AutoLock lock(lock_); |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 118 | if (bound_instance_id_ == kInvalidInstanceId) |
| 119 | return; |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 120 | instance_id = bound_instance_id_; |
| 121 | } |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 122 | GetTaskRunner(runner_id)->PostDelayedTask( |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 123 | FROM_HERE, |
| 124 | base::BindOnce(&TaskService::RunTask, base::Unretained(this), instance_id, |
| 125 | runner_id, std::move(task)), |
| 126 | delay); |
| 127 | } |
| 128 | |
| 129 | scoped_refptr<base::SingleThreadTaskRunner> TaskService::GetTaskRunner( |
| 130 | RunnerId runner_id) { |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 131 | base::AutoLock lock(lock_); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 132 | if (runner_id == kDefaultRunnerId) |
| 133 | return default_task_runner_; |
| 134 | |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 135 | if (threads_.size() < runner_id) |
| 136 | threads_.resize(runner_id); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 137 | |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 138 | size_t thread = runner_id - 1; |
| 139 | if (!threads_[thread]) { |
| 140 | threads_[thread] = base::MakeUnique<base::Thread>( |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 141 | base::StringPrintf("MidiService_TaskService_Thread(%zu)", runner_id)); |
| 142 | #if defined(OS_WIN) |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 143 | threads_[thread]->init_com_with_mta(true); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 144 | #endif |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 145 | threads_[thread]->Start(); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 146 | } |
Takashi Toyoshima | 5bbd0f8 | 2017-07-04 17:05:09 +0000 | [diff] [blame] | 147 | return threads_[thread]->task_runner(); |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void TaskService::RunTask(InstanceId instance_id, |
| 151 | RunnerId runner_id, |
| 152 | base::OnceClosure task) { |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 153 | { |
Robert Liao | f66a9cf | 2017-08-26 01:40:35 +0000 | [diff] [blame] | 154 | base::AutoLock tasks_in_flight_auto_lock(tasks_in_flight_lock_); |
| 155 | ++tasks_in_flight_; |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 156 | } |
Robert Liao | f66a9cf | 2017-08-26 01:40:35 +0000 | [diff] [blame] | 157 | |
| 158 | if (IsInstanceIdStillBound(instance_id)) |
| 159 | std::move(task).Run(); |
| 160 | |
| 161 | { |
| 162 | base::AutoLock tasks_in_flight_auto_lock(tasks_in_flight_lock_); |
| 163 | --tasks_in_flight_; |
| 164 | DCHECK_GE(tasks_in_flight_, 0); |
| 165 | if (tasks_in_flight_ == 0) |
| 166 | no_tasks_in_flight_cv_.Signal(); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | bool TaskService::IsInstanceIdStillBound(InstanceId instance_id) { |
| 171 | base::AutoLock lock(lock_); |
| 172 | return instance_id == bound_instance_id_; |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | } // namespace midi |