xenium
backoff.hpp
1 //
2 // Copyright (c) 2018-2020 Manuel Pöter.
3 // Licensed under the MIT License. See LICENSE file in the project root for full license information.
4 //
5 
6 #ifndef XENIUM_BACKOFF_HPP
7 #define XENIUM_BACKOFF_HPP
8 
9 #include <xenium/detail/hardware.hpp>
10 #include <algorithm>
11 
12 namespace xenium {
16 struct no_backoff
17 {
18  void operator()() {}
19 };
20 
25 {
26  void operator()() { detail::hardware_pause(); }
27 };
28 
29 template <unsigned Max>
30 struct exponential_backoff {
31  static_assert(Max > 0, "Max must be greater than zero. If you don't want to backoff use the `no_backoff` class.");
32 
33  void operator()() {
34  for (unsigned i = 0; i < count; ++i)
35  detail::hardware_pause();
36  count = std::min(Max, count * 2);
37  }
38 
39  unsigned count = 1;
40 };
41 
42 }
43 
44 #endif
xenium::single_backoff
Simple backoff strategy that always perfoms a single hardware_pause operation.
Definition: backoff.hpp:25
xenium::no_backoff
Dummy backoff strategy that does nothing.
Definition: backoff.hpp:17