File: backoff.hpp

package info (click to toggle)
xenium 0.0.2%2Bds-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 1,152 kB
  • sloc: cpp: 12,299; makefile: 20
file content (44 lines) | stat: -rw-r--r-- 958 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// Copyright (c) 2018-2020 Manuel Pöter.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//

#ifndef XENIUM_BACKOFF_HPP
#define XENIUM_BACKOFF_HPP

#include <xenium/detail/hardware.hpp>
#include <algorithm>

namespace xenium {
/**
 * @brief Dummy backoff strategy that does nothing.
 */
struct no_backoff
{
  void operator()() {}
};

/**
 * @brief Simple backoff strategy that always perfoms a single `hardware_pause` operation.
 */
struct single_backoff
{
  void operator()() { detail::hardware_pause(); }
};

template <unsigned Max>
struct exponential_backoff {
  static_assert(Max > 0, "Max must be greater than zero. If you don't want to backoff use the `no_backoff` class.");

  void operator()() {
    for (unsigned i = 0; i < count; ++i)
      detail::hardware_pause();
    count = std::min(Max, count * 2);
  }

  unsigned count = 1;
};

}

#endif