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