OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_CHILD_WORKER_THREAD_REGISTRY_H_ | 5 #ifndef CONTENT_CHILD_WORKER_THREAD_REGISTRY_H_ |
6 #define CONTENT_CHILD_WORKER_THREAD_REGISTRY_H_ | 6 #define CONTENT_CHILD_WORKER_THREAD_REGISTRY_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/callback_forward.h" | 10 #include "base/callback.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
14 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
15 #include "content/public/child/worker_thread.h" | 15 #include "content/public/child/worker_thread.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 class TaskRunner; | 18 class TaskRunner; |
19 } | 19 } |
20 | 20 |
21 namespace content { | 21 namespace content { |
22 | 22 |
23 class CONTENT_EXPORT WorkerThreadRegistry { | 23 class CONTENT_EXPORT WorkerThreadRegistry { |
24 public: | 24 public: |
25 WorkerThreadRegistry(); | 25 WorkerThreadRegistry(); |
26 | 26 |
27 int PostTaskToAllThreads(const base::Closure& task); | 27 int PostTaskToAllThreads(base::Closure task); |
28 static WorkerThreadRegistry* Instance(); | 28 static WorkerThreadRegistry* Instance(); |
29 | 29 |
30 void DidStartCurrentWorkerThread(); | 30 void DidStartCurrentWorkerThread(); |
31 void WillStopCurrentWorkerThread(); | 31 void WillStopCurrentWorkerThread(); |
32 | 32 |
33 // Always returns a non-null task runner regardless of whether the | 33 // Always returns a non-null task runner regardless of whether the |
34 // corresponding worker thread is gone or not. If the thread is already gone | 34 // corresponding worker thread is gone or not. If the thread is already gone |
35 // the tasks posted onto the task runner will be silently discarded. | 35 // the tasks posted onto the task runner will be silently discarded. |
36 base::TaskRunner* GetTaskRunnerFor(int worker_id); | 36 base::TaskRunner* GetTaskRunnerFor(int worker_id); |
37 | 37 |
38 private: | 38 private: |
39 friend class WorkerThread; | 39 friend class WorkerThread; |
40 friend class WorkerThreadRegistryTest; | 40 friend class WorkerThreadRegistryTest; |
41 | 41 |
42 bool PostTask(int id, const base::Closure& task); | 42 bool PostTask(int id, base::Closure task); |
43 | 43 |
44 using IDToTaskRunnerMap = std::map<base::PlatformThreadId, base::TaskRunner*>; | 44 using IDToTaskRunnerMap = std::map<base::PlatformThreadId, base::TaskRunner*>; |
45 | 45 |
46 ~WorkerThreadRegistry(); | 46 ~WorkerThreadRegistry(); |
47 | 47 |
48 // It is possible for an IPC message to arrive for a worker thread that has | 48 // It is possible for an IPC message to arrive for a worker thread that has |
49 // already gone away. In such cases, it is still necessary to provide a | 49 // already gone away. In such cases, it is still necessary to provide a |
50 // task-runner for that particular thread, because otherwise the message will | 50 // task-runner for that particular thread, because otherwise the message will |
51 // end up being handled as per usual in the main-thread, causing incorrect | 51 // end up being handled as per usual in the main-thread, causing incorrect |
52 // results. |task_runner_for_dead_worker_| is used to handle such messages, | 52 // results. |task_runner_for_dead_worker_| is used to handle such messages, |
53 // which silently discards all the tasks it receives. | 53 // which silently discards all the tasks it receives. |
54 scoped_refptr<base::TaskRunner> task_runner_for_dead_worker_; | 54 scoped_refptr<base::TaskRunner> task_runner_for_dead_worker_; |
55 | 55 |
56 IDToTaskRunnerMap task_runner_map_; | 56 IDToTaskRunnerMap task_runner_map_; |
57 base::Lock task_runner_map_lock_; | 57 base::Lock task_runner_map_lock_; |
58 }; | 58 }; |
59 | 59 |
60 } // namespace content | 60 } // namespace content |
61 | 61 |
62 #endif // CONTENT_CHILD_WORKER_THREAD_REGISTRY_H_ | 62 #endif // CONTENT_CHILD_WORKER_THREAD_REGISTRY_H_ |
OLD | NEW |