Path: blob/main/contrib/llvm-project/libcxx/include/__thread/id.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___THREAD_ID_H10#define _LIBCPP___THREAD_ID_H1112#include <__compare/ordering.h>13#include <__config>14#include <__fwd/functional.h>15#include <__fwd/ostream.h>16#include <__thread/support.h>1718#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif2122_LIBCPP_BEGIN_NAMESPACE_STD2324#ifndef _LIBCPP_HAS_NO_THREADS25class _LIBCPP_EXPORTED_FROM_ABI __thread_id;2627namespace this_thread {2829_LIBCPP_HIDE_FROM_ABI __thread_id get_id() _NOEXCEPT;3031} // namespace this_thread3233template <>34struct hash<__thread_id>;3536class _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_;4142static _LIBCPP_HIDE_FROM_ABI bool43__lt_impl(__thread_id __x, __thread_id __y) _NOEXCEPT { // id==0 is always less than any other thread_id44if (__x.__id_ == 0)45return __y.__id_ != 0;46if (__y.__id_ == 0)47return false;48return __libcpp_thread_id_less(__x.__id_, __y.__id_);49}5051public:52_LIBCPP_HIDE_FROM_ABI __thread_id() _NOEXCEPT : __id_(0) {}5354_LIBCPP_HIDE_FROM_ABI void __reset() { __id_ = 0; }5556friend _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT;57# if _LIBCPP_STD_VER <= 1758friend _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT;59# else // _LIBCPP_STD_VER <= 1760friend _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept;61# endif // _LIBCPP_STD_VER <= 176263template <class _CharT, class _Traits>64friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&65operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);6667private:68_LIBCPP_HIDE_FROM_ABI __thread_id(__libcpp_thread_id __id) : __id_(__id) {}6970_LIBCPP_HIDE_FROM_ABI friend __libcpp_thread_id __get_underlying_id(const __thread_id __id) { return __id.__id_; }7172friend __thread_id this_thread::get_id() _NOEXCEPT;73friend class _LIBCPP_EXPORTED_FROM_ABI thread;74friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;75};7677inline _LIBCPP_HIDE_FROM_ABI bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT {78// Don't pass id==0 to underlying routines79if (__x.__id_ == 0)80return __y.__id_ == 0;81if (__y.__id_ == 0)82return false;83return __libcpp_thread_id_equal(__x.__id_, __y.__id_);84}8586# if _LIBCPP_STD_VER <= 178788inline _LIBCPP_HIDE_FROM_ABI bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x == __y); }8990inline _LIBCPP_HIDE_FROM_ABI bool operator<(__thread_id __x, __thread_id __y) _NOEXCEPT {91return __thread_id::__lt_impl(__x.__id_, __y.__id_);92}9394inline _LIBCPP_HIDE_FROM_ABI bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__y < __x); }95inline _LIBCPP_HIDE_FROM_ABI bool operator>(__thread_id __x, __thread_id __y) _NOEXCEPT { return __y < __x; }96inline _LIBCPP_HIDE_FROM_ABI bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT { return !(__x < __y); }9798# else // _LIBCPP_STD_VER <= 1799100inline _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(__thread_id __x, __thread_id __y) noexcept {101if (__x == __y)102return strong_ordering::equal;103if (__thread_id::__lt_impl(__x, __y))104return strong_ordering::less;105return strong_ordering::greater;106}107108# endif // _LIBCPP_STD_VER <= 17109110namespace this_thread {111112inline _LIBCPP_HIDE_FROM_ABI __thread_id get_id() _NOEXCEPT { return __libcpp_thread_get_current_id(); }113114} // namespace this_thread115116#endif // !_LIBCPP_HAS_NO_THREADS117118_LIBCPP_END_NAMESPACE_STD119120#endif // _LIBCPP___THREAD_ID_H121122123