poll_with_backoff.h 2.7 KB

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