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-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___THREAD_TIMED_BACKOFF_POLICY_H
11#define _LIBCPP___THREAD_TIMED_BACKOFF_POLICY_H
12
13#include <__config>
14#include <__thread/poll_with_backoff.h>
15
16#if _LIBCPP_HAS_THREADS
17
18# include <__chrono/duration.h>
19# include <__thread/support.h>
20
21# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22# pragma GCC system_header
23# endif
24
25_LIBCPP_BEGIN_NAMESPACE_STD
26
27struct __libcpp_timed_backoff_policy {
28 _LIBCPP_HIDE_FROM_ABI __backoff_results operator()(chrono::nanoseconds __elapsed) const {
29 if (__elapsed > chrono::milliseconds(128))
30 __libcpp_thread_sleep_for(ns: chrono::milliseconds(8));
31 else if (__elapsed > chrono::microseconds(64))
32 __libcpp_thread_sleep_for(ns: __elapsed / 2);
33 else if (__elapsed > chrono::microseconds(4))
34 __libcpp_thread_yield();
35 else {
36 } // poll
37 return __backoff_results::__continue_poll;
38 }
39};
40
41_LIBCPP_END_NAMESPACE_STD
42
43#endif // _LIBCPP_HAS_THREADS
44
45#endif // _LIBCPP___THREAD_TIMED_BACKOFF_POLICY_H
46