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 <thread>
30
31// C.
32#if defined(__x86_64__) || defined(__i386__)
33#include <immintrin.h>
34#endif
35#include <cstddef>
36
37namespace join
38{
42 class Backoff
43 {
44 public:
49 Backoff (size_t spin = 200)
50 : _spin (spin)
51 , _count (0)
52 {
53 }
54
58 void operator() () noexcept
59 {
60 if (_count < _spin)
61 {
62#if defined(__x86_64__) || defined(__i386__)
63 _mm_pause ();
64#elif defined(__aarch64__) || defined(__arm__)
65 __asm__ __volatile__ ("yield" ::: "memory");
66#endif
67 ++_count;
68 }
69 else
70 {
71 std::this_thread::yield ();
72 }
73 }
74
78 void reset () noexcept
79 {
80 _count = 0;
81 }
82
87 bool spinExhausted () const noexcept
88 {
89 return _count >= _spin;
90 }
91
92 private:
94 size_t _spin;
95
97 size_t _count;
98 };
99}
100
101#endif
adaptive backoff strategy for busy-wait loops.
Definition backoff.hpp:43
void reset() noexcept
reset backoff to initial state.
Definition backoff.hpp:78
Backoff(size_t spin=200)
construct a backoff strategy.
Definition backoff.hpp:49
void operator()() noexcept
execute one backoff iteration.
Definition backoff.hpp:58
bool spinExhausted() const noexcept
check if initial spin phase is over.
Definition backoff.hpp:87
Definition acceptor.hpp:32