Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:43

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___CXX03___THREAD_ID_H
0011 #define _LIBCPP___CXX03___THREAD_ID_H
0012 
0013 #include <__cxx03/__compare/ordering.h>
0014 #include <__cxx03/__config>
0015 #include <__cxx03/__fwd/functional.h>
0016 #include <__cxx03/__fwd/ostream.h>
0017 #include <__cxx03/__thread/support.h>
0018 
0019 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0020 #  pragma GCC system_header
0021 #endif
0022 
0023 _LIBCPP_BEGIN_NAMESPACE_STD
0024 
0025 #ifndef _LIBCPP_HAS_NO_THREADS
0026 class _LIBCPP_EXPORTED_FROM_ABI __thread_id;
0027 
0028 namespace this_thread {
0029 
0030 _LIBCPP_HIDE_FROM_ABI __thread_id get_id() _NOEXCEPT;
0031 
0032 } // namespace this_thread
0033 
0034 template <>
0035 struct hash<__thread_id>;
0036 
0037 class _LIBCPP_TEMPLATE_VIS __thread_id {
0038   // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
0039   // NULL is the no-thread value on Darwin.  Someone needs to check
0040   // on other platforms.  We assume 0 works everywhere for now.
0041   __libcpp_thread_id __id_;
0042 
0043   static _LIBCPP_HIDE_FROM_ABI bool
0044   __lt_impl(__thread_id __x, __thread_id __y) _NOEXCEPT { // id==0 is always less than any other thread_id
0045     if (__x.__id_ == 0)
0046       return __y.__id_ != 0;
0047     if (__y.__id_ == 0)
0048       return false;
0049     return __libcpp_thread_id_less(__x.__id_, __y.__id_);
0050   }
0051 
0052 public:
0053   _LIBCPP_HIDE_FROM_ABI __thread_id() _NOEXCEPT : __id_(0) {}
0054 
0055   _LIBCPP_HIDE_FROM_ABI void __reset() { __id_ = 0; }
0056 
0057   friend _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT;
0058 #  if _LIBCPP_STD_VER <= 17
0059   friend _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT;
0060 #  else  // _LIBCPP_STD_VER <= 17
0061   friend _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept;
0062 #  endif // _LIBCPP_STD_VER <= 17
0063 
0064   template <class _CharT, class _Traits>
0065   friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0066   operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);
0067 
0068 private:
0069   _LIBCPP_HIDE_FROM_ABI __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
0070 
0071   _LIBCPP_HIDE_FROM_ABI friend __libcpp_thread_id __get_underlying_id(const __thread_id __id) { return __id.__id_; }
0072 
0073   friend __thread_id this_thread::get_id() _NOEXCEPT;
0074   friend class _LIBCPP_EXPORTED_FROM_ABI thread;
0075   friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
0076 };
0077 
0078 inline _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT {
0079   // Don't pass id==0 to underlying routines
0080   if (__x.__id_ == 0)
0081     return __y.__id_ == 0;
0082   if (__y.__id_ == 0)
0083     return false;
0084   return __libcpp_thread_id_equal(__x.__id_, __y.__id_);
0085 }
0086 
0087 #  if _LIBCPP_STD_VER <= 17
0088 
0089 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x == __y); }
0090 
0091 inline _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT {
0092   return __thread_id::__lt_impl(__x.__id_, __y.__id_);
0093 }
0094 
0095 inline _LIBCPP_HIDE_FROM_ABI bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__y < __x); }
0096 inline _LIBCPP_HIDE_FROM_ABI bool operator>(__thread_id __x, __thread_id __y) _NOEXCEPT { return __y < __x; }
0097 inline _LIBCPP_HIDE_FROM_ABI bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x < __y); }
0098 
0099 #  else // _LIBCPP_STD_VER <= 17
0100 
0101 inline _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept {
0102   if (__x == __y)
0103     return strong_ordering::equal;
0104   if (__thread_id::__lt_impl(__x, __y))
0105     return strong_ordering::less;
0106   return strong_ordering::greater;
0107 }
0108 
0109 #  endif // _LIBCPP_STD_VER <= 17
0110 
0111 namespace this_thread {
0112 
0113 inline _LIBCPP_HIDE_FROM_ABI __thread_id get_id() _NOEXCEPT { return __libcpp_thread_get_current_id(); }
0114 
0115 } // namespace this_thread
0116 
0117 #endif // !_LIBCPP_HAS_NO_THREADS
0118 
0119 _LIBCPP_END_NAMESPACE_STD
0120 
0121 #endif // _LIBCPP___CXX03___THREAD_ID_H