blob: 0bc19d31f382170a1849013f43b424de593e24e7 [file] [log] [blame]
khushalsagarb64b360d2015-10-21 19:25:161// Copyright 2015 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 CC_TREES_TASK_RUNNER_PROVIDER_H_
6#define CC_TREES_TASK_RUNNER_PROVIDER_H_
7
danakj60bc3bc2016-04-09 00:24:488#include <memory>
khushalsagarb64b360d2015-10-21 19:25:169#include <string>
10
khushalsagarb64b360d2015-10-21 19:25:1611#include "base/logging.h"
danakj60bc3bc2016-04-09 00:24:4812#include "base/memory/ptr_util.h"
khushalsagarb64b360d2015-10-21 19:25:1613#include "base/memory/ref_counted.h"
khushalsagarb7db1fe2015-11-12 00:51:2714#include "base/single_thread_task_runner.h"
khushalsagarb64b360d2015-10-21 19:25:1615#include "base/threading/platform_thread.h"
16#include "base/time/time.h"
17#include "base/values.h"
chrishtrac41ff92017-03-17 05:07:3018#include "cc/cc_export.h"
khushalsagarb64b360d2015-10-21 19:25:1619
20namespace base {
khushalsagarb64b360d2015-10-21 19:25:1621class SingleThreadTaskRunner;
22}
23
24namespace cc {
khushalsagarb64b360d2015-10-21 19:25:1625
26// Class responsible for controlling access to the main and impl task runners.
27// Useful for assertion checks.
28class CC_EXPORT TaskRunnerProvider {
29 public:
danakj60bc3bc2016-04-09 00:24:4830 static std::unique_ptr<TaskRunnerProvider> Create(
khushalsagarb7db1fe2015-11-12 00:51:2731 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
32 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner) {
danakj60bc3bc2016-04-09 00:24:4833 return base::WrapUnique(
khushalsagarb7db1fe2015-11-12 00:51:2734 new TaskRunnerProvider(main_task_runner, impl_task_runner));
35 }
36
Vladimir Levinf06d1cd72019-03-13 18:24:1037 TaskRunnerProvider(const TaskRunnerProvider&) = delete;
38 TaskRunnerProvider& operator=(const TaskRunnerProvider&) = delete;
39
danakj833b2e52016-07-19 01:55:5540 // TODO(vmpstr): Should these return scoped_refptr to task runners? Many
41 // places turn them into scoped_refptrs. How many of them need to?
khushalsagarb64b360d2015-10-21 19:25:1642 base::SingleThreadTaskRunner* MainThreadTaskRunner() const;
43 bool HasImplThread() const;
44 base::SingleThreadTaskRunner* ImplThreadTaskRunner() const;
45
46 // Debug hooks.
47 bool IsMainThread() const;
48 bool IsImplThread() const;
49 bool IsMainThreadBlocked() const;
50#if DCHECK_IS_ON()
51 void SetMainThreadBlocked(bool is_main_thread_blocked);
52 void SetCurrentThreadIsImplThread(bool is_impl_thread);
53#endif
54
55 virtual ~TaskRunnerProvider();
56
khushalsagarb7db1fe2015-11-12 00:51:2757 protected:
khushalsagarb64b360d2015-10-21 19:25:1658 TaskRunnerProvider(
59 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
60 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner);
61
khushalsagarb64b360d2015-10-21 19:25:1662 friend class DebugScopedSetImplThread;
63 friend class DebugScopedSetMainThread;
64 friend class DebugScopedSetMainThreadBlocked;
65
66 private:
67 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
68 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner_;
khushalsagarb64b360d2015-10-21 19:25:1669
70#if DCHECK_IS_ON()
71 const base::PlatformThreadId main_thread_id_;
72 bool impl_thread_is_overridden_;
73 bool is_main_thread_blocked_;
74#endif
khushalsagarb64b360d2015-10-21 19:25:1675};
76
77#if DCHECK_IS_ON()
78class DebugScopedSetMainThreadBlocked {
79 public:
80 explicit DebugScopedSetMainThreadBlocked(
81 TaskRunnerProvider* task_runner_provider)
82 : task_runner_provider_(task_runner_provider) {
83 DCHECK(!task_runner_provider_->IsMainThreadBlocked());
84 task_runner_provider_->SetMainThreadBlocked(true);
85 }
Vladimir Levinf06d1cd72019-03-13 18:24:1086 DebugScopedSetMainThreadBlocked(const DebugScopedSetMainThreadBlocked&) =
87 delete;
khushalsagarb64b360d2015-10-21 19:25:1688 ~DebugScopedSetMainThreadBlocked() {
89 DCHECK(task_runner_provider_->IsMainThreadBlocked());
90 task_runner_provider_->SetMainThreadBlocked(false);
91 }
92
Vladimir Levinf06d1cd72019-03-13 18:24:1093 DebugScopedSetMainThreadBlocked& operator=(
94 const DebugScopedSetMainThreadBlocked&) = delete;
95
khushalsagarb64b360d2015-10-21 19:25:1696 private:
97 TaskRunnerProvider* task_runner_provider_;
khushalsagarb64b360d2015-10-21 19:25:1698};
99#else
100class DebugScopedSetMainThreadBlocked {
101 public:
102 explicit DebugScopedSetMainThreadBlocked(
103 TaskRunnerProvider* task_runner_provider) {}
Vladimir Levinf06d1cd72019-03-13 18:24:10104 DebugScopedSetMainThreadBlocked(const DebugScopedSetMainThreadBlocked&) =
105 delete;
khushalsagarb64b360d2015-10-21 19:25:16106 ~DebugScopedSetMainThreadBlocked() {}
107
Vladimir Levinf06d1cd72019-03-13 18:24:10108 DebugScopedSetMainThreadBlocked& operator=(
109 const DebugScopedSetMainThreadBlocked&) = delete;
khushalsagarb64b360d2015-10-21 19:25:16110};
111#endif
112
113} // namespace cc
114
115#endif // CC_TREES_TASK_RUNNER_PROVIDER_H_