Update activity log owners
[chromium-blink-merge.git] / content / child / worker_task_runner.h
blobc3797bc93ef702642b2e883b1f70dad2ddedb403
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
3 // found in the LICENSE file.
5 #ifndef CONTENT_CHILD_WORKER_TASK_RUNNER_H_
6 #define CONTENT_CHILD_WORKER_TASK_RUNNER_H_
8 #include <map>
10 #include "base/callback_forward.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/synchronization/lock.h"
13 #include "base/threading/platform_thread.h"
14 #include "base/threading/thread_local.h"
15 #include "content/common/content_export.h"
17 namespace base {
18 class TaskRunner;
21 namespace content {
23 class CONTENT_EXPORT WorkerTaskRunner {
24 public:
25 WorkerTaskRunner();
27 bool PostTask(int id, const base::Closure& task);
28 int PostTaskToAllThreads(const base::Closure& task);
29 int CurrentWorkerId();
30 static WorkerTaskRunner* Instance();
32 class CONTENT_EXPORT Observer {
33 public:
34 virtual ~Observer() {}
35 virtual void OnWorkerRunLoopStopped() = 0;
37 // Add/Remove an observer that will get notified when the current worker run
38 // loop is stopping. This observer will not get notified when other threads
39 // are stopping. It's only valid to call these on a worker thread.
40 void AddStopObserver(Observer* observer);
41 void RemoveStopObserver(Observer* observer);
43 void OnWorkerRunLoopStarted();
44 void OnWorkerRunLoopStopped();
46 base::TaskRunner* GetTaskRunnerFor(int worker_id);
48 private:
49 friend class WorkerTaskRunnerTest;
51 using IDToTaskRunnerMap = std::map<base::PlatformThreadId, base::TaskRunner*>;
53 ~WorkerTaskRunner();
55 // It is possible for an IPC message to arrive for a worker thread that has
56 // already gone away. In such cases, it is still necessary to provide a
57 // task-runner for that particular thread, because otherwise the message will
58 // end up being handled as per usual in the main-thread, causing incorrect
59 // results. |task_runner_for_dead_worker_| is used to handle such messages,
60 // which silently discards all the tasks it receives.
61 scoped_refptr<base::TaskRunner> task_runner_for_dead_worker_;
63 struct ThreadLocalState;
64 base::ThreadLocalPointer<ThreadLocalState> current_tls_;
66 IDToTaskRunnerMap task_runner_map_;
67 base::Lock task_runner_map_lock_;
70 } // namespace content
72 #endif // CONTENT_CHILD_WORKER_TASK_RUNNER_H_