Path: blob/main/contrib/llvm-project/libcxx/include/__thread/this_thread.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_THIS_THREAD_H10#define _LIBCPP___THREAD_THIS_THREAD_H1112#include <__chrono/steady_clock.h>13#include <__chrono/time_point.h>14#include <__condition_variable/condition_variable.h>15#include <__config>16#include <__mutex/mutex.h>17#include <__mutex/unique_lock.h>18#include <__thread/support.h>1920#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21# pragma GCC system_header22#endif2324_LIBCPP_PUSH_MACROS25#include <__undef_macros>2627_LIBCPP_BEGIN_NAMESPACE_STD2829namespace this_thread {3031_LIBCPP_EXPORTED_FROM_ABI void sleep_for(const chrono::nanoseconds& __ns);3233template <class _Rep, class _Period>34_LIBCPP_HIDE_FROM_ABI void sleep_for(const chrono::duration<_Rep, _Period>& __d) {35if (__d > chrono::duration<_Rep, _Period>::zero()) {36// The standard guarantees a 64bit signed integer resolution for nanoseconds,37// so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid <climits>38// and issues with long double folding on PowerPC with GCC.39_LIBCPP_CONSTEXPR chrono::duration<long double> __max = chrono::duration<long double>(9223372036.0L);40chrono::nanoseconds __ns;41if (__d < __max) {42__ns = chrono::duration_cast<chrono::nanoseconds>(__d);43if (__ns < __d)44++__ns;45} else46__ns = chrono::nanoseconds::max();47this_thread::sleep_for(__ns);48}49}5051template <class _Clock, class _Duration>52_LIBCPP_HIDE_FROM_ABI void sleep_until(const chrono::time_point<_Clock, _Duration>& __t) {53mutex __mut;54condition_variable __cv;55unique_lock<mutex> __lk(__mut);56while (_Clock::now() < __t)57__cv.wait_until(__lk, __t);58}5960template <class _Duration>61inline _LIBCPP_HIDE_FROM_ABI void sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) {62this_thread::sleep_for(__t - chrono::steady_clock::now());63}6465inline _LIBCPP_HIDE_FROM_ABI void yield() _NOEXCEPT { __libcpp_thread_yield(); }6667} // namespace this_thread6869_LIBCPP_END_NAMESPACE_STD7071_LIBCPP_POP_MACROS7273#endif // _LIBCPP___THREAD_THIS_THREAD_H747576