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