Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::hot_path Namespace Reference

Functions

template<typename Map , typename Key , typename CreateFn >
auto get_or_create (Map &map, std::shared_mutex &mutex, const Key &key, CreateFn create_fn) -> typename std::remove_reference< decltype(*map.begin() ->second)>::type *
 
template<typename Map , typename Key , typename CreateFn , typename InitFn >
auto get_or_create_with_init (Map &map, std::shared_mutex &mutex, const Key &key, CreateFn create_fn, InitFn init_fn) -> typename std::remove_reference< decltype(*map.begin() ->second)>::type *
 
template<typename Map , typename Key , typename CreateFn , typename UpdateFn >
auto get_or_create_and_update (Map &map, std::shared_mutex &mutex, const Key &key, CreateFn create_fn, UpdateFn update_fn) -> decltype(update_fn(*map.begin() ->second))
 

Function Documentation

◆ get_or_create()

template<typename Map , typename Key , typename CreateFn >
auto kcenon::monitoring::hot_path::get_or_create ( Map & map,
std::shared_mutex & mutex,
const Key & key,
CreateFn create_fn ) -> typename std::remove_reference<decltype(*map.begin()->second)>::type*
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/utils/hot_path_helper.h.

Definition at line 69 of file hot_path_helper.h.

71 {
72 // Fast path: try with shared lock (read-only, allows concurrent readers)
73 {
74 std::shared_lock<std::shared_mutex> read_lock(mutex);
75 auto it = map.find(key);
76 if (it != map.end()) {
77 return it->second.get();
78 }
79 }
80
81 // Slow path: upgrade to write lock and double-check
82 {
83 std::unique_lock<std::shared_mutex> write_lock(mutex);
84 // Double-check after acquiring write lock
85 // Another thread may have created the entry while we were waiting
86 auto& ptr = map[key];
87 if (!ptr) {
88 ptr = create_fn();
89 }
90 return ptr.get();
91 }
92}

Referenced by get_or_create_and_update(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ get_or_create_and_update()

template<typename Map , typename Key , typename CreateFn , typename UpdateFn >
auto kcenon::monitoring::hot_path::get_or_create_and_update ( Map & map,
std::shared_mutex & mutex,
const Key & key,
CreateFn create_fn,
UpdateFn update_fn ) -> decltype(update_fn(*map.begin()->second))
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/utils/hot_path_helper.h.

Definition at line 204 of file hot_path_helper.h.

206 {
207 // Get or create the entry
208 auto* ptr = get_or_create(map, mutex, key, std::move(create_fn));
209
210 // Apply the update function outside of map locks
211 // The value's own synchronization handles thread safety
212 return update_fn(*ptr);
213}
auto get_or_create(Map &map, std::shared_mutex &mutex, const Key &key, CreateFn create_fn) -> typename std::remove_reference< decltype(*map.begin() ->second)>::type *

References get_or_create().

Referenced by TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_or_create_with_init()

template<typename Map , typename Key , typename CreateFn , typename InitFn >
auto kcenon::monitoring::hot_path::get_or_create_with_init ( Map & map,
std::shared_mutex & mutex,
const Key & key,
CreateFn create_fn,
InitFn init_fn ) -> typename std::remove_reference<decltype(*map.begin()->second)>::type*
Examples
/home/runner/work/monitoring_system/monitoring_system/include/kcenon/monitoring/utils/hot_path_helper.h.

Definition at line 135 of file hot_path_helper.h.

137 {
138 // Fast path: try with shared lock (read-only, allows concurrent readers)
139 {
140 std::shared_lock<std::shared_mutex> read_lock(mutex);
141 auto it = map.find(key);
142 if (it != map.end()) {
143 return it->second.get();
144 }
145 }
146
147 // Slow path: upgrade to write lock and double-check
148 {
149 std::unique_lock<std::shared_mutex> write_lock(mutex);
150 // Double-check after acquiring write lock
151 // Another thread may have created the entry while we were waiting
152 auto& ptr = map[key];
153 if (!ptr) {
154 ptr = create_fn();
155 // Initialize the newly created entry while still under write lock
156 init_fn(*ptr);
157 }
158 return ptr.get();
159 }
160}

Referenced by TEST_F(), and TEST_F().

Here is the caller graph for this function: