-
Notifications
You must be signed in to change notification settings - Fork 578
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CpuBoundWork#CpuBoundWork(): don't spin on atomic int to acquire slot #9990
base: master
Are you sure you want to change the base?
Changes from all commits
424e1bc
4374c36
46378f9
6d5b447
b413287
26ef66e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,60 +16,57 @@ | |
|
||
using namespace icinga; | ||
|
||
CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc) | ||
CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc, boost::asio::io_context::strand& strand) | ||
: m_Done(false) | ||
{ | ||
auto& ioEngine (IoEngine::Get()); | ||
auto& sem (ioEngine.m_CpuBoundSemaphore); | ||
std::unique_lock<std::mutex> lock (sem.Mutex); | ||
|
||
for (;;) { | ||
auto availableSlots (ioEngine.m_CpuBoundSemaphore.fetch_sub(1)); | ||
if (sem.FreeSlots) { | ||
--sem.FreeSlots; | ||
return; | ||
} | ||
|
||
if (availableSlots < 1) { | ||
ioEngine.m_CpuBoundSemaphore.fetch_add(1); | ||
IoEngine::YieldCurrentCoroutine(yc); | ||
continue; | ||
} | ||
auto cv (Shared<AsioConditionVariable>::Make(ioEngine.GetIoContext())); | ||
bool gotSlot = false; | ||
auto pos (sem.Waiting.insert(sem.Waiting.end(), IoEngine::CpuBoundQueueItem{&strand, cv, &gotSlot})); | ||
|
||
break; | ||
} | ||
} | ||
lock.unlock(); | ||
|
||
CpuBoundWork::~CpuBoundWork() | ||
{ | ||
if (!m_Done) { | ||
IoEngine::Get().m_CpuBoundSemaphore.fetch_add(1); | ||
} | ||
} | ||
try { | ||
cv->Wait(yc); | ||
} catch (...) { | ||
Comment on lines
+37
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are you trying to catch here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mainly catch forced_unwind. |
||
std::unique_lock<std::mutex> lock (sem.Mutex); | ||
|
||
void CpuBoundWork::Done() | ||
{ | ||
if (!m_Done) { | ||
IoEngine::Get().m_CpuBoundSemaphore.fetch_add(1); | ||
if (gotSlot) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Items get moved out of sem.Waiting which invalidates pos. gotSlot tells me whether pos is still valid or not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cppreference says this:
I would simply use a pointer to IoEngine::CpuBoundQueueItem item{&strand, cv, false};
auto pos (sem.Waiting.emplace(sem.Waiting.end(), &item)); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Exactly that gotSlot tells. |
||
lock.unlock(); | ||
Done(); | ||
} else { | ||
sem.Waiting.erase(pos); | ||
} | ||
|
||
m_Done = true; | ||
throw; | ||
} | ||
} | ||
|
||
IoBoundWorkSlot::IoBoundWorkSlot(boost::asio::yield_context yc) | ||
: yc(yc) | ||
{ | ||
IoEngine::Get().m_CpuBoundSemaphore.fetch_add(1); | ||
} | ||
|
||
IoBoundWorkSlot::~IoBoundWorkSlot() | ||
void CpuBoundWork::Done() | ||
{ | ||
auto& ioEngine (IoEngine::Get()); | ||
if (!m_Done) { | ||
auto& sem (IoEngine::Get().m_CpuBoundSemaphore); | ||
std::unique_lock<std::mutex> lock (sem.Mutex); | ||
|
||
for (;;) { | ||
auto availableSlots (ioEngine.m_CpuBoundSemaphore.fetch_sub(1)); | ||
if (sem.Waiting.empty()) { | ||
++sem.FreeSlots; | ||
} else { | ||
auto next (sem.Waiting.front()); | ||
|
||
if (availableSlots < 1) { | ||
ioEngine.m_CpuBoundSemaphore.fetch_add(1); | ||
IoEngine::YieldCurrentCoroutine(yc); | ||
continue; | ||
*next.GotSlot = true; | ||
sem.Waiting.pop_front(); | ||
boost::asio::post(*next.Strand, [cv = std::move(next.CV)]() { cv->Set(); }); | ||
} | ||
|
||
break; | ||
m_Done = true; | ||
} | ||
} | ||
|
||
|
@@ -88,7 +85,11 @@ boost::asio::io_context& IoEngine::GetIoContext() | |
IoEngine::IoEngine() : m_IoContext(), m_KeepAlive(boost::asio::make_work_guard(m_IoContext)), m_Threads(decltype(m_Threads)::size_type(Configuration::Concurrency * 2u)), m_AlreadyExpiredTimer(m_IoContext) | ||
{ | ||
m_AlreadyExpiredTimer.expires_at(boost::posix_time::neg_infin); | ||
m_CpuBoundSemaphore.store(Configuration::Concurrency * 3u / 2u); | ||
|
||
{ | ||
std::unique_lock<std::mutex> lock (m_CpuBoundSemaphore.Mutex); | ||
m_CpuBoundSemaphore.FreeSlots = Configuration::Concurrency * 3u / 2u; | ||
} | ||
|
||
for (auto& thread : m_Threads) { | ||
thread = std::thread(&IoEngine::RunEventLoop, this); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why you're using a boolean pointer here! Why not just use a simple bool type instead?