Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:14:04

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___THREAD_POLL_WITH_BACKOFF_H
0011 #define _LIBCPP___THREAD_POLL_WITH_BACKOFF_H
0012 
0013 #include <__chrono/duration.h>
0014 #include <__chrono/high_resolution_clock.h>
0015 #include <__config>
0016 
0017 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0018 #  pragma GCC system_header
0019 #endif
0020 
0021 _LIBCPP_BEGIN_NAMESPACE_STD
0022 
0023 static _LIBCPP_CONSTEXPR const int __libcpp_polling_count = 64;
0024 
0025 // Polls a thread for a condition given by a predicate, and backs off based on a backoff policy
0026 // before polling again.
0027 //
0028 // - __poll is the "test function" that should return true if polling succeeded, and false if it failed.
0029 //
0030 // - __backoff is the "backoff policy", which is called with the duration since we started polling. It should
0031 //   return false in order to resume polling, and true if polling should stop entirely for some reason.
0032 //   In general, backoff policies sleep for some time before returning control to the polling loop.
0033 //
0034 // - __max_elapsed is the maximum duration to try polling for. If the maximum duration is exceeded,
0035 //   the polling loop will return false to report a timeout.
0036 template <class _Poll, class _Backoff>
0037 _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool __libcpp_thread_poll_with_backoff(
0038     _Poll&& __poll, _Backoff&& __backoff, chrono::nanoseconds __max_elapsed = chrono::nanoseconds::zero()) {
0039   auto const __start = chrono::high_resolution_clock::now();
0040   for (int __count = 0;;) {
0041     if (__poll())
0042       return true; // __poll completion means success
0043     if (__count < __libcpp_polling_count) {
0044       __count += 1;
0045       continue;
0046     }
0047     chrono::nanoseconds const __elapsed = chrono::high_resolution_clock::now() - __start;
0048     if (__max_elapsed != chrono::nanoseconds::zero() && __max_elapsed < __elapsed)
0049       return false; // timeout failure
0050     if (__backoff(__elapsed))
0051       return false; // __backoff completion means failure
0052   }
0053 }
0054 
0055 // A trivial backoff policy that always immediately returns the control to
0056 // the polling loop.
0057 //
0058 // This is not very well-behaved since it will cause the polling loop to spin,
0059 // so this should most likely only be used on single-threaded systems where there
0060 // are no other threads to compete with.
0061 struct __spinning_backoff_policy {
0062   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(chrono::nanoseconds const&) const { return false; }
0063 };
0064 
0065 _LIBCPP_END_NAMESPACE_STD
0066 
0067 #endif // _LIBCPP___THREAD_POLL_WITH_BACKOFF_H