YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
injector.cpp
Go to the documentation of this file.
1#include <fault/util.hpp>
2
4
5#include <yaclib_std/thread>
6
7namespace yaclib::detail {
8
9static std::uint32_t sYieldFrequency = 16;
10static std::uint32_t sSleepTime = 100;
11static std::uint64_t sInjectedCount = 0;
12
14 if (NeedInject()) {
15#if YACLIB_FAULT == 2
17 yaclib_std::this_thread::yield();
18#elif defined(_MSC_VER)
19 yaclib_std::this_thread::yield();
20#else
21 yaclib_std::this_thread::sleep_for(std::chrono::nanoseconds(1 + GetRandNumber(sSleepTime)));
22#endif
23 }
24}
25
26bool Injector::NeedInject() noexcept {
27 if (_pause) {
28 return false;
29 }
30 if (_count.fetch_add(1, std::memory_order_relaxed) >= sYieldFrequency) {
31 Reset();
32 return true;
33 }
34 return false;
35}
36
37void Injector::Reset() noexcept {
38 _count = static_cast<std::uint32_t>(GetRandNumber(sYieldFrequency));
39}
40
41void Injector::SetFrequency(std::uint32_t freq) noexcept {
43}
44
45void Injector::SetSleepTime(std::uint32_t ns) noexcept {
46 sSleepTime = ns;
47}
48
50 return sSleepTime;
51}
52
54 return sInjectedCount;
55}
56
58 return _count.load(std::memory_order_relaxed);
59}
60
61void Injector::SetState(std::uint32_t state) noexcept {
62 _count.store(state, std::memory_order_relaxed);
63}
64
66 _pause = true;
67}
68
70 _pause = false;
71}
72
73} // namespace yaclib::detail
std::uint32_t GetState() const noexcept
Definition injector.cpp:57
void SetState(std::uint32_t state) noexcept
Definition injector.cpp:61
void Disable() noexcept
Definition injector.cpp:65
static void SetSleepTime(std::uint32_t ns) noexcept
Definition injector.cpp:45
static std::uint32_t GetSleepTime() noexcept
Definition injector.cpp:49
static std::uint64_t GetInjectedCount() noexcept
Definition injector.cpp:53
void MaybeInject() noexcept
Definition injector.cpp:13
void Enable() noexcept
Definition injector.cpp:69
static void SetFrequency(std::uint32_t freq) noexcept
Definition injector.cpp:41
static std::uint32_t sYieldFrequency
Definition injector.cpp:9
static std::uint64_t sInjectedCount
Definition injector.cpp:11
std::uint64_t GetRandNumber(std::uint64_t max)
Definition util.cpp:22
static std::uint32_t sSleepTime
Definition injector.cpp:10
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25