poll_with_backoff.h 2.7 KB

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