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() |
| 19 | : next_instance_id_(0), bound_instance_id_(kInvalidInstanceId) { |
| 20 | thread_task_locks_.resize(1); |
| 21 | thread_task_locks_[0] = base::MakeUnique<base::Lock>(); |
| 22 | |
| 23 | // We do not need a dedicated thread for the default runner since it uses |
| 24 | // the thread that calls BindInstance(), but we allocates threads_ and keeps |
| 25 | // threads_[0] empty to use the same index between |thread_task_locks_| and |
| 26 | // |threads_| for readability. |
| 27 | threads_.resize(1); |
| 28 | } |
| 29 | |
| 30 | TaskService::~TaskService() { |
| 31 | base::AutoLock lock(lock_); |
| 32 | threads_.clear(); |
| 33 | } |
| 34 | |
| 35 | bool TaskService::BindInstance() { |
| 36 | { |
| 37 | base::AutoLock instance_lock(instance_lock_); |
| 38 | if (bound_instance_id_ != kInvalidInstanceId) |
| 39 | return false; |
| 40 | bound_instance_id_ = next_instance_id_++; |
| 41 | } |
| 42 | base::AutoLock lock(lock_); |
| 43 | DCHECK(!default_task_runner_); |
| 44 | default_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | bool TaskService::UnbindInstance() { |
| 49 | { |
| 50 | base::AutoLock instance_lock(instance_lock_); |
| 51 | if (bound_instance_id_ == kInvalidInstanceId) |
| 52 | return false; |
| 53 | bound_instance_id_ = kInvalidInstanceId; |
| 54 | } |
| 55 | base::AutoLock lock(lock_); |
| 56 | DCHECK(default_task_runner_); |
| 57 | default_task_runner_ = nullptr; |
| 58 | // From now on RunTask will never run any task bound to the instance id. |
| 59 | // But invoked tasks might be still running here. To ensure no task run before |
| 60 | // quitting this method, take all |thread_task_locks_| once. |
| 61 | for (auto& task_lock : thread_task_locks_) |
| 62 | base::AutoLock auto_task_lock(*task_lock); |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | void TaskService::PostStaticTask(RunnerId runner_id, base::OnceClosure task) { |
| 67 | scoped_refptr<base::SingleThreadTaskRunner> runner; |
| 68 | { |
| 69 | base::AutoLock lock(lock_); |
| 70 | runner = GetTaskRunner(runner_id); |
| 71 | } |
| 72 | runner->PostTask(FROM_HERE, std::move(task)); |
| 73 | } |
| 74 | |
| 75 | void TaskService::PostBoundTask(RunnerId runner_id, base::OnceClosure task) { |
| 76 | base::AutoLock instance_lock(instance_lock_); |
| 77 | if (bound_instance_id_ == kInvalidInstanceId) |
| 78 | return; |
| 79 | scoped_refptr<base::SingleThreadTaskRunner> runner; |
| 80 | InstanceId instance_id; |
| 81 | { |
| 82 | base::AutoLock lock(lock_); |
| 83 | runner = GetTaskRunner(runner_id); |
| 84 | instance_id = bound_instance_id_; |
| 85 | } |
| 86 | runner->PostTask(FROM_HERE, |
| 87 | base::BindOnce(&TaskService::RunTask, base::Unretained(this), |
| 88 | instance_id, runner_id, std::move(task))); |
| 89 | } |
| 90 | |
| 91 | void TaskService::PostBoundDelayedTask(RunnerId runner_id, |
| 92 | base::OnceClosure task, |
| 93 | base::TimeDelta delay) { |
| 94 | base::AutoLock instance_lock(instance_lock_); |
| 95 | if (bound_instance_id_ == kInvalidInstanceId) |
| 96 | return; |
| 97 | scoped_refptr<base::SingleThreadTaskRunner> runner; |
| 98 | InstanceId instance_id; |
| 99 | { |
| 100 | base::AutoLock lock(lock_); |
| 101 | runner = GetTaskRunner(runner_id); |
| 102 | instance_id = bound_instance_id_; |
| 103 | } |
| 104 | runner->PostDelayedTask( |
| 105 | FROM_HERE, |
| 106 | base::BindOnce(&TaskService::RunTask, base::Unretained(this), instance_id, |
| 107 | runner_id, std::move(task)), |
| 108 | delay); |
| 109 | } |
| 110 | |
| 111 | scoped_refptr<base::SingleThreadTaskRunner> TaskService::GetTaskRunner( |
| 112 | RunnerId runner_id) { |
| 113 | lock_.AssertAcquired(); |
| 114 | if (runner_id == kDefaultRunnerId) |
| 115 | return default_task_runner_; |
| 116 | |
| 117 | DCHECK_EQ(threads_.size(), thread_task_locks_.size()); |
| 118 | |
| 119 | if (threads_.size() <= runner_id) { |
| 120 | threads_.resize(runner_id + 1); |
| 121 | thread_task_locks_.resize(runner_id + 1); |
| 122 | } |
| 123 | if (!threads_[runner_id]) { |
| 124 | threads_[runner_id] = base::MakeUnique<base::Thread>( |
| 125 | base::StringPrintf("MidiService_TaskService_Thread(%zu)", runner_id)); |
| 126 | #if defined(OS_WIN) |
| 127 | threads_[runner_id]->init_com_with_mta(true); |
| 128 | #endif |
| 129 | threads_[runner_id]->Start(); |
| 130 | |
| 131 | DCHECK(!thread_task_locks_[runner_id]); |
| 132 | thread_task_locks_[runner_id] = base::MakeUnique<base::Lock>(); |
| 133 | } |
| 134 | return threads_[runner_id]->task_runner(); |
| 135 | } |
| 136 | |
| 137 | void TaskService::RunTask(InstanceId instance_id, |
| 138 | RunnerId runner_id, |
| 139 | base::OnceClosure task) { |
| 140 | std::unique_ptr<base::AutoLock> task_lock; |
| 141 | { |
| 142 | base::AutoLock instance_lock(instance_lock_); |
| 143 | // If UnbindInstance() is already called, do nothing. |
| 144 | if (instance_id != bound_instance_id_) |
| 145 | return; |
| 146 | |
| 147 | // Obtains task lock to ensure that the instance should not complete |
| 148 | // UnbindInstance() while running the |task|. |
| 149 | base::AutoLock lock(lock_); |
| 150 | task_lock = |
| 151 | base::MakeUnique<base::AutoLock>(*thread_task_locks_[runner_id]); |
| 152 | } |
| 153 | std::move(task).Run(); |
| 154 | } |
| 155 | |
| 156 | } // namespace midi |