ThreadId.h 3.0 KB

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