xref: /llvm-project/libcxx/include/__cxx03/__thread/poll_with_backoff.h (revision ce7771902dc50d900de639d499a60486b83f70e0)
1e78f53d1SNikolas Klauser // -*- C++ -*-
2e78f53d1SNikolas Klauser //===----------------------------------------------------------------------===//
3e78f53d1SNikolas Klauser //
4e78f53d1SNikolas Klauser // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5e78f53d1SNikolas Klauser // See https://llvm.org/LICENSE.txt for license information.
6e78f53d1SNikolas Klauser // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7e78f53d1SNikolas Klauser //
8e78f53d1SNikolas Klauser //===----------------------------------------------------------------------===//
9e78f53d1SNikolas Klauser 
10*ce777190SNikolas Klauser #ifndef _LIBCPP___CXX03___THREAD_POLL_WITH_BACKOFF_H
11*ce777190SNikolas Klauser #define _LIBCPP___CXX03___THREAD_POLL_WITH_BACKOFF_H
12e78f53d1SNikolas Klauser 
1373fbae83SNikolas Klauser #include <__cxx03/__chrono/duration.h>
1473fbae83SNikolas Klauser #include <__cxx03/__chrono/high_resolution_clock.h>
1573fbae83SNikolas Klauser #include <__cxx03/__config>
16e78f53d1SNikolas Klauser 
17e78f53d1SNikolas Klauser #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18e78f53d1SNikolas Klauser #  pragma GCC system_header
19e78f53d1SNikolas Klauser #endif
20e78f53d1SNikolas Klauser 
21e78f53d1SNikolas Klauser _LIBCPP_BEGIN_NAMESPACE_STD
22e78f53d1SNikolas Klauser 
23e78f53d1SNikolas Klauser static _LIBCPP_CONSTEXPR const int __libcpp_polling_count = 64;
24e78f53d1SNikolas Klauser 
25e78f53d1SNikolas Klauser // Polls a thread for a condition given by a predicate, and backs off based on a backoff policy
26e78f53d1SNikolas Klauser // before polling again.
27e78f53d1SNikolas Klauser //
28e78f53d1SNikolas Klauser // - __poll is the "test function" that should return true if polling succeeded, and false if it failed.
29e78f53d1SNikolas Klauser //
30e78f53d1SNikolas Klauser // - __backoff is the "backoff policy", which is called with the duration since we started polling. It should
31e78f53d1SNikolas Klauser //   return false in order to resume polling, and true if polling should stop entirely for some reason.
32e78f53d1SNikolas Klauser //   In general, backoff policies sleep for some time before returning control to the polling loop.
33e78f53d1SNikolas Klauser //
34e78f53d1SNikolas Klauser // - __max_elapsed is the maximum duration to try polling for. If the maximum duration is exceeded,
35e78f53d1SNikolas Klauser //   the polling loop will return false to report a timeout.
36e78f53d1SNikolas Klauser template <class _Poll, class _Backoff>
37e78f53d1SNikolas Klauser _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_poll_with_backoff(
38e78f53d1SNikolas Klauser     _Poll&& __poll, _Backoff&& __backoff, chrono::nanoseconds __max_elapsed = chrono::nanoseconds::zero()) {
39e78f53d1SNikolas Klauser   auto const __start = chrono::high_resolution_clock::now();
40e78f53d1SNikolas Klauser   for (int __count = 0;;) {
41e78f53d1SNikolas Klauser     if (__poll())
42e78f53d1SNikolas Klauser       return true; // __poll completion means success
43e78f53d1SNikolas Klauser     if (__count < __libcpp_polling_count) {
44e78f53d1SNikolas Klauser       __count += 1;
45e78f53d1SNikolas Klauser       continue;
46e78f53d1SNikolas Klauser     }
47e78f53d1SNikolas Klauser     chrono::nanoseconds const __elapsed = chrono::high_resolution_clock::now() - __start;
48e78f53d1SNikolas Klauser     if (__max_elapsed != chrono::nanoseconds::zero() && __max_elapsed < __elapsed)
49e78f53d1SNikolas Klauser       return false; // timeout failure
50e78f53d1SNikolas Klauser     if (__backoff(__elapsed))
51e78f53d1SNikolas Klauser       return false; // __backoff completion means failure
52e78f53d1SNikolas Klauser   }
53e78f53d1SNikolas Klauser }
54e78f53d1SNikolas Klauser 
55e78f53d1SNikolas Klauser // A trivial backoff policy that always immediately returns the control to
56e78f53d1SNikolas Klauser // the polling loop.
57e78f53d1SNikolas Klauser //
58e78f53d1SNikolas Klauser // This is not very well-behaved since it will cause the polling loop to spin,
59e78f53d1SNikolas Klauser // so this should most likely only be used on single-threaded systems where there
60e78f53d1SNikolas Klauser // are no other threads to compete with.
61e78f53d1SNikolas Klauser struct __spinning_backoff_policy {
62e78f53d1SNikolas Klauser   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(chrono::nanoseconds const&) const { return false; }
63e78f53d1SNikolas Klauser };
64e78f53d1SNikolas Klauser 
65e78f53d1SNikolas Klauser _LIBCPP_END_NAMESPACE_STD
66e78f53d1SNikolas Klauser 
67*ce777190SNikolas Klauser #endif // _LIBCPP___CXX03___THREAD_POLL_WITH_BACKOFF_H
68