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 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
19
20 #include "perfetto/ext/base/thread_task_runner.h"
21
22 #include <condition_variable>
23 #include <functional>
24 #include <mutex>
25 #include <thread>
26
27 #include "perfetto/base/logging.h"
28 #include "perfetto/ext/base/thread_utils.h"
29 #include "perfetto/ext/base/unix_task_runner.h"
30
31 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
32 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
33 #include <sys/prctl.h>
34 #endif
35
36 namespace perfetto {
37 namespace base {
38
ThreadTaskRunner(ThreadTaskRunner && other)39 ThreadTaskRunner::ThreadTaskRunner(ThreadTaskRunner&& other) noexcept
40 : thread_(std::move(other.thread_)), task_runner_(other.task_runner_) {
41 other.task_runner_ = nullptr;
42 }
43
operator =(ThreadTaskRunner && other)44 ThreadTaskRunner& ThreadTaskRunner::operator=(ThreadTaskRunner&& other) {
45 this->~ThreadTaskRunner();
46 new (this) ThreadTaskRunner(std::move(other));
47 return *this;
48 }
49
~ThreadTaskRunner()50 ThreadTaskRunner::~ThreadTaskRunner() {
51 if (task_runner_) {
52 PERFETTO_CHECK(!task_runner_->QuitCalled());
53 task_runner_->Quit();
54
55 PERFETTO_DCHECK(thread_.joinable());
56 }
57 if (thread_.joinable())
58 thread_.join();
59 }
60
ThreadTaskRunner(const std::string & name)61 ThreadTaskRunner::ThreadTaskRunner(const std::string& name) : name_(name) {
62 std::mutex init_lock;
63 std::condition_variable init_cv;
64
65 std::function<void(UnixTaskRunner*)> initializer =
66 [this, &init_lock, &init_cv](UnixTaskRunner* task_runner) {
67 std::lock_guard<std::mutex> lock(init_lock);
68 task_runner_ = task_runner;
69 // Notify while still holding the lock, as init_cv ceases to exist as
70 // soon as the main thread observes a non-null task_runner_, and it can
71 // wake up spuriously (i.e. before the notify if we had unlocked before
72 // notifying).
73 init_cv.notify_one();
74 };
75
76 thread_ = std::thread(&ThreadTaskRunner::RunTaskThread, this,
77 std::move(initializer));
78
79 std::unique_lock<std::mutex> lock(init_lock);
80 init_cv.wait(lock, [this] { return !!task_runner_; });
81 }
82
RunTaskThread(std::function<void (UnixTaskRunner *)> initializer)83 void ThreadTaskRunner::RunTaskThread(
84 std::function<void(UnixTaskRunner*)> initializer) {
85 if (!name_.empty()) {
86 base::MaybeSetThreadName(name_);
87 }
88
89 UnixTaskRunner task_runner;
90 task_runner.PostTask(std::bind(std::move(initializer), &task_runner));
91 task_runner.Run();
92 }
93
PostTaskAndWaitForTesting(std::function<void ()> fn)94 void ThreadTaskRunner::PostTaskAndWaitForTesting(std::function<void()> fn) {
95 std::mutex mutex;
96 std::condition_variable cv;
97
98 std::unique_lock<std::mutex> lock(mutex);
99 bool done = false;
100 task_runner_->PostTask([&mutex, &cv, &done, &fn] {
101 fn();
102
103 std::lock_guard<std::mutex> inner_lock(mutex);
104 done = true;
105 cv.notify_one();
106 });
107 cv.wait(lock, [&done] { return done; });
108 }
109
GetThreadCPUTimeNsForTesting()110 uint64_t ThreadTaskRunner::GetThreadCPUTimeNsForTesting() {
111 uint64_t thread_time_ns = 0;
112 PostTaskAndWaitForTesting([&thread_time_ns] {
113 thread_time_ns = static_cast<uint64_t>(base::GetThreadCPUTimeNs().count());
114 });
115 return thread_time_ns;
116 }
117
118 } // namespace base
119 } // namespace perfetto
120
121 #endif // !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
122