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