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