• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #ifndef INCLUDE_PERFETTO_BASE_THREAD_UTILS_H_
18 #define INCLUDE_PERFETTO_BASE_THREAD_UTILS_H_
19 
20 #include <stdint.h>
21 
22 #include "perfetto/base/build_config.h"
23 
24 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
25 #include <processthreadsapi.h>
26 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
27 #include <zircon/process.h>
28 #include <zircon/types.h>
29 #else
30 #include <pthread.h>
31 #include <sys/syscall.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #endif
35 
36 namespace perfetto {
37 namespace base {
38 
39 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
40 using PlatformThreadID = pid_t;
GetThreadId()41 inline PlatformThreadID GetThreadId() {
42   return gettid();
43 }
44 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX)
45 using PlatformThreadID = pid_t;
46 inline PlatformThreadID GetThreadId() {
47   return static_cast<pid_t>(syscall(__NR_gettid));
48 }
49 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
50 using PlatformThreadID = zx_handle_t;
51 inline PlatformThreadID GetThreadId() {
52   return static_cast<pid_t>(zx_thread_self());
53 }
54 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_MACOSX)
55 using PlatformThreadID = uint64_t;
56 inline PlatformThreadID GetThreadId() {
57   uint64_t tid;
58   pthread_threadid_np(nullptr, &tid);
59   return tid;
60 }
61 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
62 using PlatformThreadID = uint64_t;
63 inline PlatformThreadID GetThreadId() {
64   return static_cast<uint64_t>(GetCurrentThreadID());
65 }
66 #else  // Default to pthreads in case no OS is set.
67 using PlatformThreadID = pthread_t;
68 inline PlatformThreadID GetThreadId() {
69   return pthread_self();
70 }
71 #endif
72 
73 }  // namespace base
74 }  // namespace perfetto
75 
76 #endif  // INCLUDE_PERFETTO_BASE_THREAD_UTILS_H_
77