timed_backoff_policy.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 <__thread/support.h>
  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_HIDE_FROM_ABI bool operator()(chrono::nanoseconds __elapsed) const {
  21. if (__elapsed > chrono::milliseconds(128))
  22. __libcpp_thread_sleep_for(chrono::milliseconds(8));
  23. else if (__elapsed > chrono::microseconds(64))
  24. __libcpp_thread_sleep_for(__elapsed / 2);
  25. else if (__elapsed > chrono::microseconds(4))
  26. __libcpp_thread_yield();
  27. else {
  28. } // poll
  29. return false;
  30. }
  31. };
  32. _LIBCPP_END_NAMESPACE_STD
  33. #endif // _LIBCPP_HAS_NO_THREADS
  34. #endif // _LIBCPP___THREAD_TIMED_BACKOFF_POLICY_H