id.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___THREAD_ID_H
  10. #define _LIBCPP___THREAD_ID_H
  11. #include <__compare/ordering.h>
  12. #include <__config>
  13. #include <__fwd/functional.h>
  14. #include <__fwd/ostream.h>
  15. #include <__thread/support.h>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. _LIBCPP_BEGIN_NAMESPACE_STD
  20. #ifndef _LIBCPP_HAS_NO_THREADS
  21. class _LIBCPP_EXPORTED_FROM_ABI __thread_id;
  22. namespace this_thread {
  23. _LIBCPP_HIDE_FROM_ABI __thread_id get_id() _NOEXCEPT;
  24. } // namespace this_thread
  25. template <>
  26. struct hash<__thread_id>;
  27. class _LIBCPP_TEMPLATE_VIS __thread_id {
  28. // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
  29. // NULL is the no-thread value on Darwin. Someone needs to check
  30. // on other platforms. We assume 0 works everywhere for now.
  31. __libcpp_thread_id __id_;
  32. static _LIBCPP_HIDE_FROM_ABI bool
  33. __lt_impl(__thread_id __x, __thread_id __y) _NOEXCEPT { // id==0 is always less than any other thread_id
  34. if (__x.__id_ == 0)
  35. return __y.__id_ != 0;
  36. if (__y.__id_ == 0)
  37. return false;
  38. return __libcpp_thread_id_less(__x.__id_, __y.__id_);
  39. }
  40. public:
  41. _LIBCPP_HIDE_FROM_ABI __thread_id() _NOEXCEPT : __id_(0) {}
  42. _LIBCPP_HIDE_FROM_ABI void __reset() { __id_ = 0; }
  43. friend _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT;
  44. # if _LIBCPP_STD_VER <= 17
  45. friend _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT;
  46. # else // _LIBCPP_STD_VER <= 17
  47. friend _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept;
  48. # endif // _LIBCPP_STD_VER <= 17
  49. template <class _CharT, class _Traits>
  50. friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  51. operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);
  52. private:
  53. _LIBCPP_HIDE_FROM_ABI __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
  54. _LIBCPP_HIDE_FROM_ABI friend __libcpp_thread_id __get_underlying_id(const __thread_id __id) { return __id.__id_; }
  55. friend __thread_id this_thread::get_id() _NOEXCEPT;
  56. friend class _LIBCPP_EXPORTED_FROM_ABI thread;
  57. friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
  58. };
  59. inline _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT {
  60. // Don't pass id==0 to underlying routines
  61. if (__x.__id_ == 0)
  62. return __y.__id_ == 0;
  63. if (__y.__id_ == 0)
  64. return false;
  65. return __libcpp_thread_id_equal(__x.__id_, __y.__id_);
  66. }
  67. # if _LIBCPP_STD_VER <= 17
  68. inline _LIBCPP_HIDE_FROM_ABI bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x == __y); }
  69. inline _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT {
  70. return __thread_id::__lt_impl(__x.__id_, __y.__id_);
  71. }
  72. inline _LIBCPP_HIDE_FROM_ABI bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__y < __x); }
  73. inline _LIBCPP_HIDE_FROM_ABI bool operator>(__thread_id __x, __thread_id __y) _NOEXCEPT { return __y < __x; }
  74. inline _LIBCPP_HIDE_FROM_ABI bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x < __y); }
  75. # else // _LIBCPP_STD_VER <= 17
  76. inline _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept {
  77. if (__x == __y)
  78. return strong_ordering::equal;
  79. if (__thread_id::__lt_impl(__x, __y))
  80. return strong_ordering::less;
  81. return strong_ordering::greater;
  82. }
  83. # endif // _LIBCPP_STD_VER <= 17
  84. namespace this_thread {
  85. inline _LIBCPP_HIDE_FROM_ABI __thread_id get_id() _NOEXCEPT { return __libcpp_thread_get_current_id(); }
  86. } // namespace this_thread
  87. #endif // !_LIBCPP_HAS_NO_THREADS
  88. _LIBCPP_END_NAMESPACE_STD
  89. #endif // _LIBCPP___THREAD_ID_H