1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/base/build_config.h"
18
19 #include "perfetto/ext/base/thread_task_runner.h"
20
21 #include <condition_variable>
22 #include <functional>
23 #include <mutex>
24 #include <thread>
25
26 #include "perfetto/base/logging.h"
27 #include "perfetto/ext/base/thread_utils.h"
28 #include "perfetto/ext/base/unix_task_runner.h"
29
30 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
31 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
32 #include <sys/prctl.h>
33 #endif
34
35 namespace perfetto {
36 namespace base {
37
ThreadTaskRunner(ThreadTaskRunner && other)38 ThreadTaskRunner::ThreadTaskRunner(ThreadTaskRunner&& other) noexcept
39 : thread_(std::move(other.thread_)), task_runner_(other.task_runner_) {
40 other.task_runner_ = nullptr;
41 }
42
operator =(ThreadTaskRunner && other)43 ThreadTaskRunner& ThreadTaskRunner::operator=(ThreadTaskRunner&& other) {
44 this->~ThreadTaskRunner();
45 new (this) ThreadTaskRunner(std::move(other));
46 return *this;
47 }
48
~ThreadTaskRunner()49 ThreadTaskRunner::~ThreadTaskRunner() {
50 if (task_runner_) {
51 PERFETTO_CHECK(!task_runner_->QuitCalled());
52 task_runner_->Quit();
53
54 PERFETTO_DCHECK(thread_.joinable());
55 }
56 if (thread_.joinable())
57 thread_.join();
58 }
59
ThreadTaskRunner(const std::string & name)60 ThreadTaskRunner::ThreadTaskRunner(const std::string& name) : name_(name) {
61 std::mutex init_lock;
62 std::condition_variable init_cv;
63
64 std::function<void(UnixTaskRunner*)> initializer =
65 [this, &init_lock, &init_cv](UnixTaskRunner* task_runner) {
66 std::lock_guard<std::mutex> lock(init_lock);
67 task_runner_ = task_runner;
68 // Notify while still holding the lock, as init_cv ceases to exist as
69 // soon as the main thread observes a non-null task_runner_, and it can
70 // wake up spuriously (i.e. before the notify if we had unlocked before
71 // notifying).
72 init_cv.notify_one();
73 };
74
75 thread_ = std::thread(&ThreadTaskRunner::RunTaskThread, this,
76 std::move(initializer));
77
78 std::unique_lock<std::mutex> lock(init_lock);
79 init_cv.wait(lock, [this] { return !!task_runner_; });
80 }
81
RunTaskThread(std::function<void (UnixTaskRunner *)> initializer)82 void ThreadTaskRunner::RunTaskThread(
83 std::function<void(UnixTaskRunner*)> initializer) {
84 if (!name_.empty()) {
85 base::MaybeSetThreadName(name_);
86 }
87
88 UnixTaskRunner task_runner;
89 task_runner.PostTask(std::bind(std::move(initializer), &task_runner));
90 task_runner.Run();
91 }
92
PostTaskAndWaitForTesting(std::function<void ()> fn)93 void ThreadTaskRunner::PostTaskAndWaitForTesting(std::function<void()> fn) {
94 std::mutex mutex;
95 std::condition_variable cv;
96
97 std::unique_lock<std::mutex> lock(mutex);
98 bool done = false;
99 task_runner_->PostTask([&mutex, &cv, &done, &fn] {
100 fn();
101
102 std::lock_guard<std::mutex> inner_lock(mutex);
103 done = true;
104 cv.notify_one();
105 });
106 cv.wait(lock, [&done] { return done; });
107 }
108
GetThreadCPUTimeNsForTesting()109 uint64_t ThreadTaskRunner::GetThreadCPUTimeNsForTesting() {
110 uint64_t thread_time_ns = 0;
111 PostTaskAndWaitForTesting([&thread_time_ns] {
112 thread_time_ns = static_cast<uint64_t>(base::GetThreadCPUTimeNs().count());
113 });
114 return thread_time_ns;
115 }
116
PostTask(std::function<void ()> task)117 void ThreadTaskRunner::PostTask(std::function<void()> task) {
118 task_runner_->PostTask(std::move(task));
119 }
120
PostDelayedTask(std::function<void ()> task,uint32_t delay_ms)121 void ThreadTaskRunner::PostDelayedTask(std::function<void()> task,
122 uint32_t delay_ms) {
123 task_runner_->PostDelayedTask(std::move(task), delay_ms);
124 }
125
AddFileDescriptorWatch(PlatformHandle handle,std::function<void ()> watch_task)126 void ThreadTaskRunner::AddFileDescriptorWatch(
127 PlatformHandle handle,
128 std::function<void()> watch_task) {
129 task_runner_->AddFileDescriptorWatch(handle, std::move(watch_task));
130 }
131
RemoveFileDescriptorWatch(PlatformHandle handle)132 void ThreadTaskRunner::RemoveFileDescriptorWatch(PlatformHandle handle) {
133 task_runner_->RemoveFileDescriptorWatch(handle);
134 }
135
RunsTasksOnCurrentThread() const136 bool ThreadTaskRunner::RunsTasksOnCurrentThread() const {
137 return task_runner_->RunsTasksOnCurrentThread();
138 }
139
140 } // namespace base
141 } // namespace perfetto
142