[Fuchsia] Ensure that GetThreadId() is cached.
On Fuchsia a syscall is required to get ID of the current thread, so
ideally it should be cached. Previously GetThreadId() was trying to
cache it using thread_local variable, but it was inlined, so the value
wasn't actually cached. Moved the function to a new .cc file to a
function that's not inlined, which allows to cache the result properly.
Bug: 1166873
Change-Id: I2e3433856b393032af2a3a53847b5f42ddfe3872
diff --git a/Android.bp b/Android.bp
index bace9eb..b6300a0 100644
--- a/Android.bp
+++ b/Android.bp
@@ -7979,6 +7979,7 @@
"src/base/temp_file.cc",
"src/base/thread_checker.cc",
"src/base/thread_task_runner.cc",
+ "src/base/thread_utils.cc",
"src/base/time.cc",
"src/base/unix_task_runner.cc",
"src/base/utils.cc",
diff --git a/BUILD b/BUILD
index 57325e4..7350c41 100644
--- a/BUILD
+++ b/BUILD
@@ -750,6 +750,7 @@
"src/base/temp_file.cc",
"src/base/thread_checker.cc",
"src/base/thread_task_runner.cc",
+ "src/base/thread_utils.cc",
"src/base/time.cc",
"src/base/unix_task_runner.cc",
"src/base/utils.cc",
diff --git a/include/perfetto/base/thread_utils.h b/include/perfetto/base/thread_utils.h
index ae61941..40ca066 100644
--- a/include/perfetto/base/thread_utils.h
+++ b/include/perfetto/base/thread_utils.h
@@ -27,8 +27,6 @@
__declspec(dllimport) unsigned long __stdcall GetCurrentThreadId();
}
#elif PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
-#include <zircon/process.h>
-#include <zircon/syscalls.h>
#include <zircon/types.h>
#elif PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
@@ -54,17 +52,8 @@
}
#elif PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
using PlatformThreadId = zx_koid_t;
-static PlatformThreadId ResolveThreadId() {
- zx_info_handle_basic_t basic;
- return (zx_object_get_info(zx_thread_self(), ZX_INFO_HANDLE_BASIC, &basic,
- sizeof(basic), nullptr, nullptr) == ZX_OK)
- ? basic.koid
- : ZX_KOID_INVALID;
-}
-inline PlatformThreadId GetThreadId() {
- thread_local static PlatformThreadId thread_id = ResolveThreadId();
- return thread_id;
-}
+// Not inlined because the result is cached internally.
+PlatformThreadId GetThreadId();
#elif PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
using PlatformThreadId = uint64_t;
inline PlatformThreadId GetThreadId() {
diff --git a/src/base/BUILD.gn b/src/base/BUILD.gn
index 2f299c7..6d98a83 100644
--- a/src/base/BUILD.gn
+++ b/src/base/BUILD.gn
@@ -50,6 +50,7 @@
"string_view.cc",
"temp_file.cc",
"thread_checker.cc",
+ "thread_utils.cc",
"time.cc",
"utils.cc",
"uuid.cc",
diff --git a/src/base/thread_utils.cc b/src/base/thread_utils.cc
new file mode 100644
index 0000000..bc28f0f
--- /dev/null
+++ b/src/base/thread_utils.cc
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "perfetto/base/thread_utils.h"
+
+#include "perfetto/base/build_config.h"
+
+#if PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
+#include <zircon/process.h>
+#include <zircon/syscalls.h>
+#include <zircon/types.h>
+#endif // PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
+
+namespace perfetto {
+namespace base {
+
+#if PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
+static PlatformThreadId ResolveThreadId() {
+ zx_info_handle_basic_t basic;
+ return (zx_object_get_info(zx_thread_self(), ZX_INFO_HANDLE_BASIC, &basic,
+ sizeof(basic), nullptr, nullptr) == ZX_OK)
+ ? basic.koid
+ : ZX_KOID_INVALID;
+}
+PlatformThreadId GetThreadId() {
+ thread_local static PlatformThreadId thread_id = ResolveThreadId();
+ return thread_id;
+}
+#endif // PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
+
+} // namespace base
+} // namespace perfetto