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