fairness  v1.0.0
A collection of advanced syncronization mechanisms.
Loading...
Searching...
No Matches
wait_ops.hpp
Go to the documentation of this file.
1
14#ifndef BOOST_FAIRNESS_WAIT_OPS_HPP
15#define BOOST_FAIRNESS_WAIT_OPS_HPP
18#include <atomic>
19#include <thread>
20#include <functional>
21
22
24
25 static const std::function<void()> relaxOrYield[] = {pause ,std::this_thread::yield};
26
27
28 template<typename T ,typename K>
29 inline void spin_wait(T& mem, K const expected) noexcept {
30
31 for(int i = 0; i < BOOST_FAIRNESS_SPINWAIT_SPINS; ++i){
32
33 if (mem.load(std::memory_order_relaxed) != expected)
34 return;
35
36 relaxOrYield[i >= BOOST_FAIRNESS_SPINWAIT_SPINS_RELAXED]();
37
38 }
39
40 }
41
42 template<typename K>
43 inline void spin_wait(std::atomic_flag& mem, K const expected) noexcept {
44
45 for(int i = 0; i < BOOST_FAIRNESS_SPINWAIT_SPINS; ++i){
46
47 if (mem.test(std::memory_order_relaxed) != expected)
48 return;
49
50 relaxOrYield[i >= BOOST_FAIRNESS_SPINWAIT_SPINS_RELAXED]();
51
52 }
53
54 }
55
56 inline void spin_wait() noexcept {
57
58 for(int i = 0; i < BOOST_FAIRNESS_GETREQUEST_SPINS; ++i){
59
60 relaxOrYield[i >= BOOST_FAIRNESS_GETREQUEST_SPINS_RELAXED]();
61
62 }
63
64 }
65
66#if !defined(BOOST_FAIRNESS_USE_STD_WAIT_NOTIFY)
67
68 template<typename T, typename K>
69 inline void wait(T& mem, K const expected) noexcept {
70
71 auto memEqualsExpected = [&mem, expected]{
72 return mem.load(std::memory_order_relaxed) == expected;
73
74 };
75
76 do {
77
78 for(int i = 0; i < BOOST_FAIRNESS_WAIT_SPINS; ++i){
79
80 if (!memEqualsExpected())
81 return;
82
83 relaxOrYield[i >= BOOST_FAIRNESS_WAIT_SPINS_RELAXED]();
84
85 }
86
87 wait_(mem, expected);
88
89 } while (memEqualsExpected());
90
91 }
92
93#else // defined(BOOST_FAIRNESS_USE_STD_WAIT_NOTIFY)
94
95 template<typename T, typename K>
96 inline void wait(T& mem, K expected) noexcept {
97 wait_(mem, expected);
98 }
99
100#endif // BOOST_FAIRNESS_USE_STD_WAIT_NOTIFY
101
102 template<typename T>
103 inline void notify_one(T& mem) noexcept {
104 notify_one_(mem);
105 }
106
107 template<typename T>
108 inline void notify_all(T& mem) noexcept {
109 notify_all_(mem);
110 }
111
112}
113
114#endif // BOOST_FAIRNESS_WAIT_OPS_HPP
This file contains configurations about boost and 128bit cpu support. TODO.
#define BOOST_FAIRNESS_WAIT_SPINS_RELAXED
The total number of spins during a wait operation before a syscall to the OS to yield the cpu through...
Definition config.hpp:76
#define BOOST_FAIRNESS_GETREQUEST_SPINS_RELAXED
The number of paused spins performed while performing a getRequest (this is used only if BOOST_FAIRNE...
Definition config.hpp:40
#define BOOST_FAIRNESS_GETREQUEST_SPINS
The total number of spins performed while performing a getRequest (this is used only if BOOST_FAIRNES...
Definition config.hpp:31
#define BOOST_FAIRNESS_SPINWAIT_SPINS_RELAXED
The number of relaxed spins during a spin_wait operation.
Definition config.hpp:58
#define BOOST_FAIRNESS_SPINWAIT_SPINS
The total number of spins during a spin_wait operation.
Definition config.hpp:49
#define BOOST_FAIRNESS_WAIT_SPINS
The total number of spins during a wait operation before a syscall to the OS to yield the cpu through...
Definition config.hpp:67
Definition coherent_priority_lock.hpp:25
void wait(T &mem, K const expected) noexcept
Definition wait_ops.hpp:69
void wait_(T &mem, K expected)
Definition wait_ops_generic.hpp:22
void notify_one_(T &mem)
Definition wait_ops_generic.hpp:31
void notify_all(T &mem) noexcept
Definition wait_ops.hpp:108
void notify_all_(T &mem)
Definition wait_ops_generic.hpp:40
void spin_wait() noexcept
Definition wait_ops.hpp:56
void pause() noexcept
Definition pause_ops.hpp:21
void notify_one(T &mem) noexcept
Definition wait_ops.hpp:103
This file contains the implementation of the pause operations used by the mutexes.