Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 #ifndef MEDIA_MIDI_TASK_SERVICE_H_ | |
| 6 #define MEDIA_MIDI_TASK_SERVICE_H_ | |
| 7 | |
| 8 #include "base/callback_forward.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/single_thread_task_runner.h" | |
| 12 #include "base/synchronization/lock.h" | |
| 13 #include "base/threading/thread.h" | |
| 14 #include "base/time/time.h" | |
| 15 #include "media/midi/midi_export.h" | |
| 16 | |
| 17 namespace midi { | |
| 18 | |
| 19 // TaskService manages TaskRunners that can be used in midi and provides | |
| 20 // functionalities to ensure thread safety. | |
| 21 class MIDI_EXPORT TaskService final { | |
| 22 public: | |
| 23 using RunnerId = int; | |
| 24 using InstanceId = int; | |
| 25 | |
| 26 TaskService(); | |
| 27 ~TaskService(); | |
| 28 | |
| 29 // Issues an InstanceId internally to post tasks via PostBoundTask() and | |
| 30 // PostDelayedBoundTask() with the InstanceId. Once UnbindInstance() is | |
| 31 // called, tasks posted via these methods with unbind InstanceId won't be | |
| 32 // invoked any more. | |
| 33 bool BindInstance(); | |
| 34 bool UnbindInstance(); | |
| 35 | |
| 36 // Posts a task to run on a specified TaskRunner. | |
| 37 void PostStaticTask(RunnerId runner, const base::Closure& task); | |
|
yhirano
2017/06/07 09:21:03
I think OnceClosure and BindOnce are generally pre
Takashi Toyoshima
2017/06/07 13:00:07
Done.
| |
| 38 | |
| 39 // Post a task to run on a specificed TaskRunner, and ensures that the bound | |
| 40 // instance should not quit UnbindInstance() while a bound task is running. | |
| 41 void PostBoundTask(RunnerId runner, const base::Closure& task); | |
| 42 void PostBoundDelayedTask(RunnerId runner, | |
| 43 const base::Closure& task, | |
| 44 base::TimeDelta delay); | |
| 45 | |
| 46 // Posts a task to run on a thread that called BindInstance(), and ensures | |
| 47 // that the bound instance should not quit UnbindInstance() while thee task is | |
| 48 // running. | |
| 49 void PostBoundReplyTask(const base::Closure& task); | |
| 50 | |
| 51 private: | |
| 52 // Returns a SingleThreadTaskRunner reference. Each TaskRunner will be | |
| 53 // constructed on demand. | |
| 54 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(RunnerId id); | |
| 55 | |
| 56 // Helps to run a posted bound task on TaskRunner safely. | |
| 57 void RunTask(InstanceId instance_id, | |
| 58 RunnerId runner, | |
| 59 const base::Closure& task); | |
| 60 | |
| 61 // Keeps a TaskRunner for the thread that calls BindInstance() to post reply | |
| 62 // tasks. | |
| 63 scoped_refptr<base::SingleThreadTaskRunner> reply_task_runner_; | |
| 64 | |
| 65 // Holds threads to host SingleThreadTaskRunners. | |
| 66 std::vector<std::unique_ptr<base::Thread>> threads_; | |
| 67 | |
| 68 // Holds lock objects to ensure that tasks run while the instance is bound. | |
| 69 std::vector<std::unique_ptr<base::Lock>> thread_task_locks_; | |
| 70 | |
| 71 // Holds a lock object to ensure that a reply task runs while the instance is | |
| 72 // bound. | |
| 73 base::Lock reply_task_lock_; | |
| 74 | |
| 75 // Holds InstanceId for the next bound instance. | |
| 76 InstanceId next_instance_id_; | |
| 77 | |
| 78 // Holds InstanceId for the current bound instance. | |
| 79 InstanceId bound_instance_id_; | |
| 80 | |
| 81 // Protects |next_instance_id_| and |bound_instance_id_|. | |
| 82 base::Lock instance_lock_; | |
| 83 | |
| 84 // Protects all other members. | |
| 85 base::Lock lock_; | |
| 86 | |
| 87 // If multiple locks should be obtained simultaneously, we should acquire them | |
| 88 // in the order below so to avoid deadklocks. | |
| 89 // instance_lock_ -> lock_ -> (one of) thread_task_locks_. | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(TaskService); | |
| 92 }; | |
| 93 | |
| 94 }; // namespace midi | |
| 95 | |
| 96 #endif // MEDIA_MIDI_TASK_SERVICE_H_ | |
| OLD | NEW |