fairness  v1.0.0
A collection of advanced syncronization mechanisms.
Loading...
Searching...
No Matches
waiting_utils.hpp
Go to the documentation of this file.
1
14#ifndef BOOST_FAIRNESS_WAITING_UTILS_HPP
15#define BOOST_FAIRNESS_WAITING_UTILS_HPP
16#include <chrono>
17
18#define NOW std::chrono::high_resolution_clock::now()
19
21 enum class WaitingLevel { Milli, Micro, Nano };
22
23 /*
24 std::this_thread::sleep_for is too imprecise, and also I could make a bunch of variables during busy waiting to simulate heated cpu caches.
25 */
26
27 template <WaitingLevel level>
28 void busy_wait(uint32_t utime) {
29 int i = 0;
30 auto begin = NOW;
31 if constexpr (level == WaitingLevel::Milli)
32 for(;std::chrono::duration_cast<std::chrono::milliseconds>(NOW - begin).count() < utime;){
33 benchmark::DoNotOptimize(i += 1);
34 }
35 if constexpr (level == WaitingLevel::Micro)
36 for(;std::chrono::duration_cast<std::chrono::microseconds>(NOW - begin).count() < utime;){
37 benchmark::DoNotOptimize(i += 1);
38 }
39 if constexpr (level == WaitingLevel::Nano)
40 for(;std::chrono::duration_cast<std::chrono::nanoseconds>(NOW - begin).count() < utime;){
41 benchmark::DoNotOptimize(i += 1);
42 }
43 }
44
45 void busy_wait_50milli_benchmark(benchmark::State& state) {
46 for (auto _ : state){
47 busy_wait<WaitingLevel::Milli>(50);
48 }
49 }
50
51 void busy_wait_50micro_benchmark(benchmark::State& state) {
52 for (auto _ : state){
53 busy_wait<WaitingLevel::Micro>(50);
54 }
55 }
56
57 void busy_wait_50nano_benchmark(benchmark::State& state) {
58 for (auto _ : state){
59 busy_wait<WaitingLevel::Nano>(50);
60 }
61 }
62
63}
64
65#undef NOW
66#endif // BOOST_FAIRNESS_WAITING_UTILS_HPP
Definition waiting_utils.hpp:20
void busy_wait_50micro_benchmark(benchmark::State &state)
Definition waiting_utils.hpp:51
void busy_wait_50nano_benchmark(benchmark::State &state)
Definition waiting_utils.hpp:57
void busy_wait(uint32_t utime)
Definition waiting_utils.hpp:28
void busy_wait_50milli_benchmark(benchmark::State &state)
Definition waiting_utils.hpp:45
WaitingLevel
Definition waiting_utils.hpp:21
#define NOW
Definition thread_utils.hpp:20