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 <Windows.h> 26 #include <processthreadsapi.h> 27 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA) 28 #include <zircon/process.h> 29 #include <zircon/types.h> 30 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \ 31 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) 32 #include <sys/syscall.h> 33 #include <sys/types.h> 34 #include <unistd.h> 35 #else 36 #include <pthread.h> 37 #endif 38 39 namespace perfetto { 40 namespace base { 41 42 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) 43 using PlatformThreadId = pid_t; GetThreadId()44inline PlatformThreadId GetThreadId() { 45 return gettid(); 46 } 47 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) 48 using PlatformThreadId = pid_t; 49 inline PlatformThreadId GetThreadId() { 50 return static_cast<pid_t>(syscall(__NR_gettid)); 51 } 52 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA) 53 using PlatformThreadId = zx_handle_t; 54 inline PlatformThreadId GetThreadId() { 55 return zx_thread_self(); 56 } 57 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE) 58 using PlatformThreadId = uint64_t; 59 inline PlatformThreadId GetThreadId() { 60 uint64_t tid; 61 pthread_threadid_np(nullptr, &tid); 62 return tid; 63 } 64 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) 65 using PlatformThreadId = uint64_t; 66 inline PlatformThreadId GetThreadId() { 67 return static_cast<uint64_t>(GetCurrentThreadId()); 68 } 69 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_NACL) 70 using PlatformThreadId = pid_t; 71 inline PlatformThreadId GetThreadId() { 72 return reinterpret_cast<int32_t>(pthread_self()); 73 } 74 #else // Default to pthreads in case no OS is set. 75 using PlatformThreadId = pthread_t; 76 inline PlatformThreadId GetThreadId() { 77 return pthread_self(); 78 } 79 #endif 80 81 } // namespace base 82 } // namespace perfetto 83 84 #endif // INCLUDE_PERFETTO_BASE_THREAD_UTILS_H_ 85