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(); | |
yhirano
2017/06/08 09:03:38
In https://codereview.chromium.org/2923163003/ the
Takashi Toyoshima
2017/06/08 10:30:11
Yeah, I should fix caller side to check this retur
| |
34 bool UnbindInstance(); | |
35 | |
36 // Posts a task to run on a specified TaskRunner. | |
37 void PostStaticTask(RunnerId runner, base::OnceClosure task); | |
38 | |
39 // Post a task to run on a specificed TaskRunner, and ensures that the bound | |
yhirano
2017/06/08 09:03:38
Post"s"
Takashi Toyoshima
2017/06/08 10:30:11
Done.
| |
40 // instance should not quit UnbindInstance() while a bound task is running. | |
41 void PostBoundTask(RunnerId runner, base::OnceClosure task); | |
yhirano
2017/06/08 09:03:38
You should add a comment (and DCHECK) that |runner
Takashi Toyoshima
2017/06/08 10:30:11
That would make code a little confusing due to "in
| |
42 void PostBoundDelayedTask(RunnerId runner, | |
43 base::OnceClosure 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 | |
yhirano
2017/06/08 09:03:38
s/thee/the/
Takashi Toyoshima
2017/06/08 10:30:11
Done.
| |
48 // running. | |
49 void PostBoundReplyTask(base::OnceClosure 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, RunnerId runner, base::OnceClosure task); | |
58 | |
59 // Keeps a TaskRunner for the thread that calls BindInstance() to post reply | |
60 // tasks. | |
61 scoped_refptr<base::SingleThreadTaskRunner> reply_task_runner_; | |
62 | |
63 // Holds threads to host SingleThreadTaskRunners. | |
64 std::vector<std::unique_ptr<base::Thread>> threads_; | |
65 | |
66 // Holds lock objects to ensure that tasks run while the instance is bound. | |
67 std::vector<std::unique_ptr<base::Lock>> thread_task_locks_; | |
68 | |
69 // Holds a lock object to ensure that a reply task runs while the instance is | |
70 // bound. | |
71 base::Lock reply_task_lock_; | |
72 | |
73 // Holds InstanceId for the next bound instance. | |
74 InstanceId next_instance_id_; | |
75 | |
76 // Holds InstanceId for the current bound instance. | |
77 InstanceId bound_instance_id_; | |
78 | |
79 // Protects |next_instance_id_| and |bound_instance_id_|. | |
80 base::Lock instance_lock_; | |
81 | |
82 // Protects all other members. | |
83 base::Lock lock_; | |
84 | |
85 // If multiple locks should be obtained simultaneously, we should acquire them | |
86 // in the order below so to avoid deadklocks. | |
87 // instance_lock_ -> lock_ -> (one of) thread_task_locks_. | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(TaskService); | |
90 }; | |
91 | |
92 }; // namespace midi | |
93 | |
94 #endif // MEDIA_MIDI_TASK_SERVICE_H_ | |
OLD | NEW |