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_TASK_RUNNER_H_ | 5 #ifndef CONTENT_CHILD_WORKER_TASK_RUNNER_H_ |
6 #define CONTENT_CHILD_WORKER_TASK_RUNNER_H_ | 6 #define CONTENT_CHILD_WORKER_TASK_RUNNER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.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 "base/threading/thread_local.h" | 14 #include "base/threading/thread_local.h" |
15 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
16 #include "third_party/WebKit/public/platform/WebWorkerRunLoop.h" | |
17 | 16 |
18 namespace base { | 17 namespace base { |
19 class TaskRunner; | 18 class TaskRunner; |
20 } | 19 } |
21 | 20 |
22 namespace content { | 21 namespace content { |
23 | 22 |
24 class CONTENT_EXPORT WorkerTaskRunner { | 23 class CONTENT_EXPORT WorkerTaskRunner { |
25 public: | 24 public: |
26 WorkerTaskRunner(); | 25 WorkerTaskRunner(); |
27 | 26 |
28 bool PostTask(int id, const base::Closure& task); | 27 bool PostTask(int id, const base::Closure& task); |
29 int PostTaskToAllThreads(const base::Closure& task); | 28 int PostTaskToAllThreads(const base::Closure& task); |
30 int CurrentWorkerId(); | 29 int CurrentWorkerId(); |
31 static WorkerTaskRunner* Instance(); | 30 static WorkerTaskRunner* Instance(); |
32 | 31 |
33 class CONTENT_EXPORT Observer { | 32 class CONTENT_EXPORT Observer { |
34 public: | 33 public: |
35 virtual ~Observer() {} | 34 virtual ~Observer() {} |
36 virtual void OnWorkerRunLoopStopped() = 0; | 35 virtual void OnWorkerRunLoopStopped() = 0; |
37 }; | 36 }; |
38 // Add/Remove an observer that will get notified when the current worker run | 37 // Add/Remove an observer that will get notified when the current worker run |
39 // loop is stopping. This observer will not get notified when other threads | 38 // loop is stopping. This observer will not get notified when other threads |
40 // are stopping. It's only valid to call these on a worker thread. | 39 // are stopping. It's only valid to call these on a worker thread. |
41 void AddStopObserver(Observer* observer); | 40 void AddStopObserver(Observer* observer); |
42 void RemoveStopObserver(Observer* observer); | 41 void RemoveStopObserver(Observer* observer); |
43 | 42 |
44 void OnWorkerRunLoopStarted(const blink::WebWorkerRunLoop& loop); | 43 void OnWorkerRunLoopStarted(); |
45 void OnWorkerRunLoopStopped(const blink::WebWorkerRunLoop& loop); | 44 void OnWorkerRunLoopStopped(); |
46 | 45 |
47 base::TaskRunner* GetTaskRunnerFor(int worker_id); | 46 base::TaskRunner* GetTaskRunnerFor(int worker_id); |
48 | 47 |
49 private: | 48 private: |
50 friend class WorkerTaskRunnerTest; | 49 friend class WorkerTaskRunnerTest; |
51 | 50 |
52 using IDToTaskRunnerMap = std::map<base::PlatformThreadId, base::TaskRunner*>; | 51 using IDToTaskRunnerMap = std::map<base::PlatformThreadId, base::TaskRunner*>; |
53 | 52 |
54 ~WorkerTaskRunner(); | 53 ~WorkerTaskRunner(); |
55 | 54 |
56 // It is possible for an IPC message to arrive for a worker thread that has | 55 // It is possible for an IPC message to arrive for a worker thread that has |
57 // already gone away. In such cases, it is still necessary to provide a | 56 // already gone away. In such cases, it is still necessary to provide a |
58 // task-runner for that particular thread, because otherwise the message will | 57 // task-runner for that particular thread, because otherwise the message will |
59 // end up being handled as per usual in the main-thread, causing incorrect | 58 // end up being handled as per usual in the main-thread, causing incorrect |
60 // results. |task_runner_for_dead_worker_| is used to handle such messages, | 59 // results. |task_runner_for_dead_worker_| is used to handle such messages, |
61 // which silently discards all the tasks it receives. | 60 // which silently discards all the tasks it receives. |
62 scoped_refptr<base::TaskRunner> task_runner_for_dead_worker_; | 61 scoped_refptr<base::TaskRunner> task_runner_for_dead_worker_; |
63 | 62 |
64 struct ThreadLocalState; | 63 struct ThreadLocalState; |
65 base::ThreadLocalPointer<ThreadLocalState> current_tls_; | 64 base::ThreadLocalPointer<ThreadLocalState> current_tls_; |
66 | 65 |
67 IDToTaskRunnerMap task_runner_map_; | 66 IDToTaskRunnerMap task_runner_map_; |
68 base::Lock task_runner_map_lock_; | 67 base::Lock task_runner_map_lock_; |
69 }; | 68 }; |
70 | 69 |
71 } // namespace content | 70 } // namespace content |
72 | 71 |
73 #endif // CONTENT_CHILD_WORKER_TASK_RUNNER_H_ | 72 #endif // CONTENT_CHILD_WORKER_TASK_RUNNER_H_ |
OLD | NEW |