File tree Expand file tree Collapse file tree 3 files changed +92
-0
lines changed Expand file tree Collapse file tree 3 files changed +92
-0
lines changed Original file line number Diff line number Diff line change @@ -87,6 +87,7 @@ set(base_SOURCES
87
87
unixsocket.cpp unixsocket.hpp
88
88
utility.cpp utility.hpp
89
89
value .cpp value .hpp value -operators.cpp
90
+ wait-group.cpp wait-group.hpp
90
91
win32 .hpp
91
92
workqueue.cpp workqueue.hpp
92
93
)
Original file line number Diff line number Diff line change
1
+ /* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */
2
+
3
+ #include " base/wait-group.hpp"
4
+
5
+ using namespace icinga ;
6
+
7
+ bool StoppableWaitGroup::try_lock_shared ()
8
+ {
9
+ std::unique_lock lock (m_Mutex);
10
+
11
+ if (m_Stopped) {
12
+ return false ;
13
+ }
14
+
15
+ ++m_SharedLocks;
16
+ return true ;
17
+ }
18
+
19
+ void StoppableWaitGroup::unlock_shared ()
20
+ {
21
+ std::unique_lock lock (m_Mutex);
22
+
23
+ if (!--m_SharedLocks && m_Stopped) {
24
+ m_CV.notify_all ();
25
+ }
26
+ }
27
+
28
+ /* *
29
+ * Disallow new shared locks, wait for all existing ones.
30
+ */
31
+ void StoppableWaitGroup::Join ()
32
+ {
33
+ std::unique_lock lock (m_Mutex);
34
+
35
+ m_Stopped = true ;
36
+ m_CV.wait (lock, [this ] { return !m_SharedLocks; });
37
+ }
Original file line number Diff line number Diff line change
1
+ /* Icinga 2 | (c) 2025 Icinga GmbH | GPLv2+ */
2
+
3
+ #pragma once
4
+
5
+ #include " base/object.hpp"
6
+ #include < condition_variable>
7
+ #include < cstdint>
8
+ #include < mutex>
9
+
10
+ namespace icinga
11
+ {
12
+
13
+ /* *
14
+ * A synchronization interface that allows concurrent shared locking.
15
+ *
16
+ * @ingroup base
17
+ */
18
+ class WaitGroup : public Object
19
+ {
20
+ public:
21
+ DECLARE_PTR_TYPEDEFS (WaitGroup);
22
+
23
+ virtual bool try_lock_shared () = 0;
24
+ virtual void unlock_shared () = 0;
25
+ };
26
+
27
+ /* *
28
+ * A thread-safe wait group that can be stopped to prevent further shared locking.
29
+ *
30
+ * @ingroup base
31
+ */
32
+ class StoppableWaitGroup : public WaitGroup
33
+ {
34
+ public:
35
+ DECLARE_PTR_TYPEDEFS (StoppableWaitGroup);
36
+
37
+ StoppableWaitGroup () = default ;
38
+ StoppableWaitGroup (const StoppableWaitGroup&) = delete ;
39
+ StoppableWaitGroup (StoppableWaitGroup&&) = delete ;
40
+ StoppableWaitGroup& operator =(const StoppableWaitGroup&) = delete ;
41
+ StoppableWaitGroup& operator =(StoppableWaitGroup&&) = delete ;
42
+
43
+ bool try_lock_shared () override ;
44
+ void unlock_shared () override ;
45
+ void Join ();
46
+
47
+ private:
48
+ std::mutex m_Mutex;
49
+ std::condition_variable m_CV;
50
+ uint_fast32_t m_SharedLocks = 0 ;
51
+ bool m_Stopped = false ;
52
+ };
53
+
54
+ }
You can’t perform that action at this time.
0 commit comments