1/*
2 * Copyright 2016-present Facebook, Inc.
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#pragma once
18
19#include <cstdint>
20
21#include <folly/portability/PThread.h>
22#include <folly/portability/SysSyscall.h>
23#include <folly/portability/Unistd.h>
24#include <folly/portability/Windows.h>
25
26namespace folly {
27
28/**
29 * Get a process-specific identifier for the current thread.
30 *
31 * The return value will uniquely identify the thread within the current
32 * process.
33 *
34 * Note that the return value does not necessarily correspond to an operating
35 * system thread ID. The return value is also only unique within the current
36 * process: getCurrentThreadID() may return the same value for two concurrently
37 * running threads in separate processes.
38 *
39 * The thread ID may be reused once the thread it corresponds to has been
40 * joined.
41 */
42inline uint64_t getCurrentThreadID() {
43#if __APPLE__
44 return uint64_t(pthread_mach_thread_np(pthread_self()));
45#elif _WIN32
46 return uint64_t(GetCurrentThreadId());
47#else
48 return uint64_t(pthread_self());
49#endif
50}
51
52/**
53 * Get the operating-system level thread ID for the current thread.
54 *
55 * The returned value will uniquely identify this thread on the system.
56 *
57 * This makes it more suitable for logging or displaying in user interfaces
58 * than the result of getCurrentThreadID().
59 *
60 * There are some potential caveats about this API, however:
61 *
62 * - In theory there is no guarantee that application threads map one-to-one to
63 * kernel threads. An application threading implementation could potentially
64 * share one OS thread across multiple application threads, and/or it could
65 * potentially move application threads between different OS threads over
66 * time. However, in practice all of the platforms we currently support have
67 * a one-to-one mapping between userspace threads and operating system
68 * threads.
69 *
70 * - This API may also be slightly slower than getCurrentThreadID() on some
71 * platforms. This API may require a system call, where getCurrentThreadID()
72 * may only need to read thread-local memory.
73 *
74 * On Linux the returned value is a pid_t, and can be used in contexts
75 * requiring a thread pid_t.
76 *
77 * The thread ID may be reused once the thread it corresponds to has been
78 * joined.
79 */
80inline uint64_t getOSThreadID() {
81#if __APPLE__
82 uint64_t tid;
83 pthread_threadid_np(nullptr, &tid);
84 return tid;
85#elif _WIN32
86 return uint64_t(GetCurrentThreadId());
87#else
88 return uint64_t(syscall(FOLLY_SYS_gettid));
89#endif
90}
91} // namespace folly
92