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 = size_t; |
| 24 using InstanceId = int; |
| 25 |
| 26 static constexpr RunnerId kReplyRunnerId = 0; |
| 27 static constexpr RunnerId kDefaultRunnerId = 1; |
| 28 |
| 29 TaskService(); |
| 30 ~TaskService(); |
| 31 |
| 32 // Issues an InstanceId internally to post tasks via PostBoundTask() and |
| 33 // PostDelayedBoundTask() with the InstanceId. Once UnbindInstance() is |
| 34 // called, tasks posted via these methods with unbind InstanceId won't be |
| 35 // invoked any more. |
| 36 // Returns true if call is bound or unbound correctly. Otherwise returns |
| 37 // false, that happens when the BindInstance() is called twice without |
| 38 // unbinding the previous instance. |
| 39 bool BindInstance(); |
| 40 bool UnbindInstance(); |
| 41 |
| 42 // Posts a task to run on a specified TaskRunner. |runner_id| should be a |
| 43 // positive number that is larger than 0. Caller may want to use |
| 44 // kDefaultRunnerId as a default value. |
| 45 void PostStaticTask(RunnerId runner_id, base::OnceClosure task); |
| 46 |
| 47 // Posts a task to run on a specificed TaskRunner, and ensures that the bound |
| 48 // instance should not quit UnbindInstance() while a bound task is running. |
| 49 void PostBoundTask(RunnerId runner, base::OnceClosure task); |
| 50 void PostBoundDelayedTask(RunnerId runner_id, |
| 51 base::OnceClosure task, |
| 52 base::TimeDelta delay); |
| 53 |
| 54 // Posts a task to run on a thread that called BindInstance(), and ensures |
| 55 // that the bound instance should not quit UnbindInstance() while the task is |
| 56 // running. |
| 57 void PostBoundReplyTask(base::OnceClosure task); |
| 58 |
| 59 private: |
| 60 // Returns a SingleThreadTaskRunner reference. Each TaskRunner will be |
| 61 // constructed on demand. |
| 62 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(RunnerId runner_id); |
| 63 |
| 64 // Helps to run a posted bound task on TaskRunner safely. |
| 65 void RunTask(InstanceId instance_id, |
| 66 RunnerId runner_id, |
| 67 base::OnceClosure task); |
| 68 |
| 69 // Keeps a TaskRunner for the thread that calls BindInstance() to post reply |
| 70 // tasks. |
| 71 scoped_refptr<base::SingleThreadTaskRunner> reply_task_runner_; |
| 72 |
| 73 // Holds threads to host SingleThreadTaskRunners. |
| 74 std::vector<std::unique_ptr<base::Thread>> threads_; |
| 75 |
| 76 // Holds lock objects to ensure that tasks run while the instance is bound. |
| 77 std::vector<std::unique_ptr<base::Lock>> thread_task_locks_; |
| 78 |
| 79 // Holds InstanceId for the next bound instance. |
| 80 InstanceId next_instance_id_; |
| 81 |
| 82 // Holds InstanceId for the current bound instance. |
| 83 InstanceId bound_instance_id_; |
| 84 |
| 85 // Protects |next_instance_id_| and |bound_instance_id_|. |
| 86 base::Lock instance_lock_; |
| 87 |
| 88 // Protects all other members. |
| 89 base::Lock lock_; |
| 90 |
| 91 // If multiple locks should be obtained simultaneously, we should acquire them |
| 92 // in the order below so to avoid deadklocks. |
| 93 // |instance_lock_| -> |lock_| -> |(one of) thread_task_locks_|. |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(TaskService); |
| 96 }; |
| 97 |
| 98 }; // namespace midi |
| 99 |
| 100 #endif // MEDIA_MIDI_TASK_SERVICE_H_ |
OLD | NEW |