timed_backoff_policy.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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_TIMED_BACKOFF_POLICY_H
  10. #define _LIBCPP___THREAD_TIMED_BACKOFF_POLICY_H
  11. #include <__config>
  12. #ifndef _LIBCPP_HAS_NO_THREADS
  13. # include <__chrono/duration.h>
  14. # include <__threading_support>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. _LIBCPP_BEGIN_NAMESPACE_STD
  19. struct __libcpp_timed_backoff_policy {
  20. _LIBCPP_INLINE_VISIBILITY
  21. bool operator()(chrono::nanoseconds __elapsed) const
  22. {
  23. if(__elapsed > chrono::milliseconds(128))
  24. __libcpp_thread_sleep_for(chrono::milliseconds(8));
  25. else if(__elapsed > chrono::microseconds(64))
  26. __libcpp_thread_sleep_for(__elapsed / 2);
  27. else if(__elapsed > chrono::microseconds(4))
  28. __libcpp_thread_yield();
  29. else
  30. {} // poll
  31. return false;
  32. }
  33. };
  34. _LIBCPP_END_NAMESPACE_STD
  35. #endif // _LIBCPP_HAS_NO_THREADS
  36. #endif // _LIBCPP___THREAD_TIMED_BACKOFF_POLICY_H