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 <algorithm>
10 #include <xenium/detail/hardware.hpp>
11 
12 namespace xenium {
16 struct no_backoff {
17  void operator()() {}
18 };
19 
24  void operator()() { detail::hardware_pause(); }
25 };
26 
27 template <unsigned Max>
28 struct exponential_backoff {
29  static_assert(Max > 0, "Max must be greater than zero. If you don't want to backoff use the `no_backoff` class.");
30 
31  void operator()() {
32  for (unsigned i = 0; i < count; ++i) {
33  detail::hardware_pause();
34  }
35  count = std::min(Max, count * 2);
36  }
37 
38 private:
39  unsigned count = 1;
40 };
41 
42 } // namespace xenium
43 
44 #endif
xenium::single_backoff
Simple backoff strategy that always perfoms a single hardware_pause operation.
Definition: backoff.hpp:23
xenium::no_backoff
Dummy backoff strategy that does nothing.
Definition: backoff.hpp:16