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_POLL_WITH_BACKOFF_H
10 #define _LIBCPP___THREAD_POLL_WITH_BACKOFF_H
11 
12 #include <__availability>
13 #include <__chrono/duration.h>
14 #include <__chrono/high_resolution_clock.h>
15 #include <__chrono/steady_clock.h>
16 #include <__chrono/time_point.h>
17 #include <__config>
18 #include <__filesystem/file_time_type.h>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 static _LIBCPP_CONSTEXPR const int __libcpp_polling_count = 64;
27 
28 // Polls a thread for a condition given by a predicate, and backs off based on a backoff policy
29 // before polling again.
30 //
31 // - __f is the "test function" that should return true if polling succeeded, and false if it failed.
32 //
33 // - __bf is the "backoff policy", which is called with the duration since we started polling. It should
34 //   return false in order to resume polling, and true if polling should stop entirely for some reason.
35 //   In general, backoff policies sleep for some time before returning control to the polling loop.
36 //
37 // - __max_elapsed is the maximum duration to try polling for. If the maximum duration is exceeded,
38 //   the polling loop will return false to report a timeout.
39 template<class _Fn, class _BFn>
40 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI
41 bool __libcpp_thread_poll_with_backoff(_Fn&& __f, _BFn&& __bf, chrono::nanoseconds __max_elapsed = chrono::nanoseconds::zero()) {
42     auto const __start = chrono::high_resolution_clock::now();
43     for (int __count = 0;;) {
44       if (__f())
45         return true; // _Fn completion means success
46       if (__count < __libcpp_polling_count) {
47         __count += 1;
48         continue;
49       }
50       chrono::nanoseconds const __elapsed = chrono::high_resolution_clock::now() - __start;
51       if (__max_elapsed != chrono::nanoseconds::zero() && __max_elapsed < __elapsed)
52           return false; // timeout failure
53       if (__bf(__elapsed))
54         return false; // _BFn completion means failure
55     }
56 }
57 
58 // A trivial backoff policy that always immediately returns the control to
59 // the polling loop.
60 //
61 // This is not very well-behaved since it will cause the polling loop to spin,
62 // so this should most likely only be used on single-threaded systems where there
63 // are no other threads to compete with.
64 struct __spinning_backoff_policy {
65   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
66   bool operator()(chrono::nanoseconds const&) const {
67       return false;
68   }
69 };
70 
71 _LIBCPP_END_NAMESPACE_STD
72 
73 #endif // _LIBCPP___THREAD_POLL_WITH_BACKOFF_H
74