fairness  v1.0.0
A collection of advanced syncronization mechanisms.
Loading...
Searching...
No Matches
thread_utils.hpp
Go to the documentation of this file.
1
14#ifndef BOOST_FAIRNESS_THREAD_UTILS_HPP
15#define BOOST_FAIRNESS_THREAD_UTILS_HPP
16#include <mutex>
17#include <shared_mutex>
18#include "waiting_utils.hpp"
19
20#define NOW std::chrono::high_resolution_clock::now()
21
22namespace utils::thread{
24
25 template <LockLevel lock_level, utils::waiting::WaitingLevel waiting_level, typename Lockable>
27 if constexpr (lock_level == LockLevel::Unique) {
28 utils::waiting::busy_wait<waiting_level>(preCriticalTime);
29 if constexpr (std::is_same_v<Lockable, std::mutex> || std::is_same_v<Lockable, std::shared_mutex> || std::is_same_v<Lockable, std::recursive_mutex>) lockable.lock();
30 else lockable.lock(p);
31 utils::waiting::busy_wait<waiting_level>(criticalTime);
32 lockable.unlock();
33 utils::waiting::busy_wait<waiting_level>(postCriticalTime);
34 }
35 if constexpr (lock_level == LockLevel::Shared) {
36 utils::waiting::busy_wait<waiting_level>(preCriticalTime);
37 if constexpr (std::is_same_v<Lockable, std::mutex> || std::is_same_v<Lockable, std::shared_mutex> || std::is_same_v<Lockable, std::recursive_mutex>) lockable.lock_shared();
38 else lockable.lock_shared(p);
39 utils::waiting::busy_wait<waiting_level>(criticalTime);
40 lockable.unlock_shared();
41 utils::waiting::busy_wait<waiting_level>(postCriticalTime);
42 }
43 if constexpr (lock_level == LockLevel::Unique_Try) {
44 utils::waiting::busy_wait<waiting_level>(preCriticalTime);
45 if constexpr (std::is_same_v<Lockable, std::mutex> || std::is_same_v<Lockable, std::shared_mutex> || std::is_same_v<Lockable, std::recursive_mutex>) lockable.try_lock();
46 else lockable.try_lock(p);
47 utils::waiting::busy_wait<waiting_level>(criticalTime);
48 lockable.unlock();
49 utils::waiting::busy_wait<waiting_level>(postCriticalTime);
50 }
51 if constexpr (lock_level == LockLevel::Shared_Try) {
52 utils::waiting::busy_wait<waiting_level>(preCriticalTime);
53 if constexpr (std::is_same_v<Lockable, std::mutex> || std::is_same_v<Lockable, std::shared_mutex> || std::is_same_v<Lockable, std::recursive_mutex>) lockable.try_lock_shared();
54 else lockable.try_lock_shared(p);
55 utils::waiting::busy_wait<waiting_level>(criticalTime);
56 lockable.unlock_shared();
57 utils::waiting::busy_wait<waiting_level>(postCriticalTime);
58 }
59 }
60}
61
62#undef NOW
63#endif // BOOST_FAIRNESS_THREAD_UTILS_HPP
Definition thread_utils.hpp:22
void thread_function(Lockable &lockable, int preCriticalTime, int criticalTime, int postCriticalTime, int p=0)
Definition thread_utils.hpp:26
LockLevel
Definition thread_utils.hpp:23
Alias waiting_utils.