brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.4 KiB · 4df1377 Raw
80 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___THREAD_THIS_THREAD_H11#define _LIBCPP___THREAD_THIS_THREAD_H12 13#include <__chrono/duration.h>14#include <__chrono/steady_clock.h>15#include <__chrono/time_point.h>16#include <__condition_variable/condition_variable.h>17#include <__config>18#include <__mutex/mutex.h>19#include <__mutex/unique_lock.h>20#include <__thread/support.h>21 22#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)23#  pragma GCC system_header24#endif25 26_LIBCPP_PUSH_MACROS27#include <__undef_macros>28 29_LIBCPP_BEGIN_NAMESPACE_STD30 31namespace this_thread {32 33#if _LIBCPP_HAS_THREADS34 35_LIBCPP_EXPORTED_FROM_ABI void sleep_for(const chrono::nanoseconds& __ns);36 37template <class _Rep, class _Period>38_LIBCPP_HIDE_FROM_ABI void sleep_for(const chrono::duration<_Rep, _Period>& __d) {39  if (__d > chrono::duration<_Rep, _Period>::zero()) {40    // The standard guarantees a 64bit signed integer resolution for nanoseconds,41    // so use INT64_MAX / 1e9 as cut-off point. Use a constant to avoid <climits>42    // and issues with long double folding on PowerPC with GCC.43    _LIBCPP_CONSTEXPR chrono::duration<long double> __max = chrono::duration<long double>(9223372036.0L);44    chrono::nanoseconds __ns;45    if (__d < __max) {46      __ns = chrono::duration_cast<chrono::nanoseconds>(__d);47      if (__ns < __d)48        ++__ns;49    } else50      __ns = chrono::nanoseconds::max();51    this_thread::sleep_for(__ns);52  }53}54 55template <class _Clock, class _Duration>56_LIBCPP_HIDE_FROM_ABI void sleep_until(const chrono::time_point<_Clock, _Duration>& __t) {57  mutex __mut;58  condition_variable __cv;59  unique_lock<mutex> __lk(__mut);60  while (_Clock::now() < __t)61    __cv.wait_until(__lk, __t);62}63 64template <class _Duration>65inline _LIBCPP_HIDE_FROM_ABI void sleep_until(const chrono::time_point<chrono::steady_clock, _Duration>& __t) {66  this_thread::sleep_for(__t - chrono::steady_clock::now());67}68 69inline _LIBCPP_HIDE_FROM_ABI void yield() _NOEXCEPT { __libcpp_thread_yield(); }70 71#endif // _LIBCPP_HAS_THREADS72 73} // namespace this_thread74 75_LIBCPP_END_NAMESPACE_STD76 77_LIBCPP_POP_MACROS78 79#endif // _LIBCPP___THREAD_THIS_THREAD_H80