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/atomic_sequence_num.h" | 10 #include "base/atomic_sequence_num.h" |
11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "base/threading/thread_local.h" | 13 #include "base/threading/thread_local.h" |
14 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
15 #include "third_party/WebKit/public/platform/WebThread.h" | |
16 #include "third_party/WebKit/public/platform/WebWorkerRunLoop.h" | 15 #include "third_party/WebKit/public/platform/WebWorkerRunLoop.h" |
17 | 16 |
18 namespace content { | 17 namespace content { |
19 | 18 |
20 class CONTENT_EXPORT WorkerTaskRunner { | 19 class CONTENT_EXPORT WorkerTaskRunner { |
21 public: | 20 public: |
22 WorkerTaskRunner(); | 21 WorkerTaskRunner(); |
23 | 22 |
24 bool PostTask(int id, const base::Closure& task); | 23 bool PostTask(int id, const base::Closure& task); |
25 int PostTaskToAllThreads(const base::Closure& task); | 24 int PostTaskToAllThreads(const base::Closure& task); |
26 int CurrentWorkerId(); | 25 int CurrentWorkerId(); |
27 static WorkerTaskRunner* Instance(); | 26 static WorkerTaskRunner* Instance(); |
28 | 27 |
29 class CONTENT_EXPORT Observer { | 28 class CONTENT_EXPORT Observer { |
30 public: | 29 public: |
31 virtual ~Observer() {} | 30 virtual ~Observer() {} |
32 virtual void OnWorkerRunLoopStopped() = 0; | 31 virtual void OnWorkerRunLoopStopped() = 0; |
33 }; | 32 }; |
34 // Add/Remove an observer that will get notified when the current worker run | 33 // Add/Remove an observer that will get notified when the current worker run |
35 // loop is stopping. This observer will not get notified when other threads | 34 // loop is stopping. This observer will not get notified when other threads |
36 // are stopping. It's only valid to call these on a worker thread. | 35 // are stopping. It's only valid to call these on a worker thread. |
37 void AddStopObserver(Observer* observer); | 36 void AddStopObserver(Observer* observer); |
38 void RemoveStopObserver(Observer* observer); | 37 void RemoveStopObserver(Observer* observer); |
39 | 38 |
40 void OnWorkerRunLoopStarted(const blink::WebWorkerRunLoop& loop); | 39 void OnWorkerRunLoopStarted(const blink::WebWorkerRunLoop& loop); |
41 void OnWorkerRunLoopStopped(const blink::WebWorkerRunLoop& loop); | 40 void OnWorkerRunLoopStopped(const blink::WebWorkerRunLoop& loop); |
42 | 41 |
43 void OnWorkerThreadStarted(blink::WebThread* thread); | |
44 void OnWorkerThreadStopped(blink::WebThread* thread); | |
45 | |
46 private: | 42 private: |
47 friend class WorkerTaskRunnerTest; | 43 friend class WorkerTaskRunnerTest; |
48 | 44 |
49 typedef std::map<int, blink::WebWorkerRunLoop> IDToLoopMap; | 45 typedef std::map<int, blink::WebWorkerRunLoop> IDToLoopMap; |
50 typedef std::map<int, blink::WebThread*> IDToThreadMap; | |
51 | 46 |
52 ~WorkerTaskRunner(); | 47 ~WorkerTaskRunner(); |
53 | 48 |
54 struct ThreadLocalState; | 49 struct ThreadLocalState; |
55 base::ThreadLocalPointer<ThreadLocalState> current_tls_; | 50 base::ThreadLocalPointer<ThreadLocalState> current_tls_; |
56 | 51 |
57 base::AtomicSequenceNumber id_sequence_; | 52 base::AtomicSequenceNumber id_sequence_; |
58 IDToLoopMap loop_map_; | 53 IDToLoopMap loop_map_; |
59 IDToThreadMap thread_map_; | |
60 base::Lock loop_map_lock_; | 54 base::Lock loop_map_lock_; |
61 }; | 55 }; |
62 | 56 |
63 } // namespace content | 57 } // namespace content |
64 | 58 |
65 #endif // CONTENT_CHILD_WORKER_TASK_RUNNER_H_ | 59 #endif // CONTENT_CHILD_WORKER_TASK_RUNNER_H_ |
OLD | NEW |