join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
backoff.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_BACKOFF_HPP
26#define JOIN_CORE_BACKOFF_HPP
27
28// C++.
29#include <algorithm>
30#include <chrono>
31#include <thread>
32
33// C.
34#if defined(__x86_64__) || defined(__i386__)
35#include <immintrin.h>
36#endif
37#include <cstddef>
38
39namespace join
40{
44 class Backoff
45 {
46 public:
51 Backoff (size_t spin = 200)
52 : _spin (spin)
53 , _count (0)
54 {
55 }
56
60 void operator() () noexcept
61 {
62 if (_count < _spin)
63 {
64#if defined(__x86_64__) || defined(__i386__)
65 _mm_pause ();
66#elif defined(__aarch64__) || defined(__arm__)
67 __asm__ __volatile__ ("yield" ::: "memory");
68#endif
69 ++_count;
70 }
71 else
72 {
73 std::this_thread::yield ();
74 }
75 }
76
80 void reset () noexcept
81 {
82 _count = 0;
83 }
84
85 private:
87 size_t _spin;
88
90 size_t _count;
91 };
92}
93
94#endif
adaptive backoff strategy for busy-wait loops.
Definition backoff.hpp:45
void reset() noexcept
reset backoff to initial state.
Definition backoff.hpp:80
Backoff(size_t spin=200)
construct a backoff strategy.
Definition backoff.hpp:51
void operator()() noexcept
execute one backoff iteration.
Definition backoff.hpp:60
Definition acceptor.hpp:32