6
6
#include " base/exception.hpp"
7
7
#include " base/lazy-init.hpp"
8
8
#include " base/logger.hpp"
9
+ #include " base/shared.hpp"
9
10
#include " base/shared-object.hpp"
10
11
#include < atomic>
12
+ #include < cstdint>
11
13
#include < exception>
14
+ #include < list>
12
15
#include < memory>
16
+ #include < mutex>
13
17
#include < thread>
14
18
#include < utility>
15
19
#include < vector>
@@ -31,7 +35,7 @@ namespace icinga
31
35
class CpuBoundWork
32
36
{
33
37
public:
34
- CpuBoundWork (boost::asio::yield_context yc, boost::asio::io_context::strand&);
38
+ CpuBoundWork (boost::asio::yield_context yc, boost::asio::io_context::strand& strand );
35
39
CpuBoundWork (const CpuBoundWork&) = delete ;
36
40
CpuBoundWork (CpuBoundWork&&) = delete ;
37
41
CpuBoundWork& operator =(const CpuBoundWork&) = delete ;
@@ -48,6 +52,25 @@ class CpuBoundWork
48
52
bool m_Done;
49
53
};
50
54
55
+
56
+ /* *
57
+ * Condition variable which doesn't block I/O threads
58
+ *
59
+ * @ingroup base
60
+ */
61
+ class AsioConditionVariable
62
+ {
63
+ public:
64
+ AsioConditionVariable (boost::asio::io_context& io, bool init = false );
65
+
66
+ void Set ();
67
+ void Clear ();
68
+ void Wait (boost::asio::yield_context yc);
69
+
70
+ private:
71
+ boost::asio::deadline_timer m_Timer;
72
+ };
73
+
51
74
/* *
52
75
* Async I/O engine
53
76
*
@@ -110,6 +133,13 @@ class IoEngine
110
133
}
111
134
112
135
private:
136
+ struct CpuBoundQueueItem
137
+ {
138
+ boost::asio::io_context::strand* Strand;
139
+ Shared<AsioConditionVariable>::Ptr CV;
140
+ bool * GotSlot;
141
+ };
142
+
113
143
IoEngine ();
114
144
115
145
void RunEventLoop ();
@@ -120,29 +150,16 @@ class IoEngine
120
150
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> m_KeepAlive;
121
151
std::vector<std::thread> m_Threads;
122
152
boost::asio::deadline_timer m_AlreadyExpiredTimer;
123
- std::atomic_int_fast32_t m_CpuBoundSemaphore;
124
- };
125
153
126
- class TerminateIoThread : public std ::exception
127
- {
154
+ struct {
155
+ std::mutex Mutex;
156
+ uint_fast32_t FreeSlots;
157
+ std::list<CpuBoundQueueItem> Waiting;
158
+ } m_CpuBoundSemaphore;
128
159
};
129
160
130
- /* *
131
- * Condition variable which doesn't block I/O threads
132
- *
133
- * @ingroup base
134
- */
135
- class AsioConditionVariable
161
+ class TerminateIoThread : public std ::exception
136
162
{
137
- public:
138
- AsioConditionVariable (boost::asio::io_context& io, bool init = false );
139
-
140
- void Set ();
141
- void Clear ();
142
- void Wait (boost::asio::yield_context yc);
143
-
144
- private:
145
- boost::asio::deadline_timer m_Timer;
146
163
};
147
164
148
165
/* *
0 commit comments