103 lines · c
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03___THREAD_ID_H11#define _LIBCPP___CXX03___THREAD_ID_H12 13#include <__cxx03/__config>14#include <__cxx03/__fwd/functional.h>15#include <__cxx03/__fwd/ostream.h>16#include <__cxx03/__thread/support.h>17 18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif21 22_LIBCPP_BEGIN_NAMESPACE_STD23 24#ifndef _LIBCPP_HAS_NO_THREADS25class _LIBCPP_EXPORTED_FROM_ABI __thread_id;26 27namespace this_thread {28 29_LIBCPP_HIDE_FROM_ABI __thread_id get_id() _NOEXCEPT;30 31} // namespace this_thread32 33template <>34struct hash<__thread_id>;35 36class _LIBCPP_TEMPLATE_VIS __thread_id {37 // FIXME: pthread_t is a pointer on Darwin but a long on Linux.38 // NULL is the no-thread value on Darwin. Someone needs to check39 // on other platforms. We assume 0 works everywhere for now.40 __libcpp_thread_id __id_;41 42 static _LIBCPP_HIDE_FROM_ABI bool43 __lt_impl(__thread_id __x, __thread_id __y) _NOEXCEPT { // id==0 is always less than any other thread_id44 if (__x.__id_ == 0)45 return __y.__id_ != 0;46 if (__y.__id_ == 0)47 return false;48 return __libcpp_thread_id_less(__x.__id_, __y.__id_);49 }50 51public:52 _LIBCPP_HIDE_FROM_ABI __thread_id() _NOEXCEPT : __id_(0) {}53 54 _LIBCPP_HIDE_FROM_ABI void __reset() { __id_ = 0; }55 56 friend _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT;57 friend _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT;58 59 template <class _CharT, class _Traits>60 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&61 operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);62 63private:64 _LIBCPP_HIDE_FROM_ABI __thread_id(__libcpp_thread_id __id) : __id_(__id) {}65 66 _LIBCPP_HIDE_FROM_ABI friend __libcpp_thread_id __get_underlying_id(const __thread_id __id) { return __id.__id_; }67 68 friend __thread_id this_thread::get_id() _NOEXCEPT;69 friend class _LIBCPP_EXPORTED_FROM_ABI thread;70 friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;71};72 73inline _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT {74 // Don't pass id==0 to underlying routines75 if (__x.__id_ == 0)76 return __y.__id_ == 0;77 if (__y.__id_ == 0)78 return false;79 return __libcpp_thread_id_equal(__x.__id_, __y.__id_);80}81 82inline _LIBCPP_HIDE_FROM_ABI bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x == __y); }83 84inline _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT {85 return __thread_id::__lt_impl(__x.__id_, __y.__id_);86}87 88inline _LIBCPP_HIDE_FROM_ABI bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__y < __x); }89inline _LIBCPP_HIDE_FROM_ABI bool operator>(__thread_id __x, __thread_id __y) _NOEXCEPT { return __y < __x; }90inline _LIBCPP_HIDE_FROM_ABI bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x < __y); }91 92namespace this_thread {93 94inline _LIBCPP_HIDE_FROM_ABI __thread_id get_id() _NOEXCEPT { return __libcpp_thread_get_current_id(); }95 96} // namespace this_thread97 98#endif // !_LIBCPP_HAS_NO_THREADS99 100_LIBCPP_END_NAMESPACE_STD101 102#endif // _LIBCPP___CXX03___THREAD_ID_H103