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