[Lldb-commits] [lldb] [lldb] Add timed callbacks to the MainLoop class (PR #112895)
Pavel Labath via lldb-commits
lldb-commits at lists.llvm.org
Wed Nov 6 11:42:08 PST 2024
================
@@ -7,27 +7,43 @@
//===----------------------------------------------------------------------===//
#include "lldb/Host/MainLoopBase.h"
+#include <chrono>
using namespace lldb;
using namespace lldb_private;
-void MainLoopBase::AddPendingCallback(const Callback &callback) {
+void MainLoopBase::AddCallback(const Callback &callback, TimePoint point) {
+ bool interrupt_needed;
{
std::lock_guard<std::mutex> lock{m_callback_mutex};
- m_pending_callbacks.push_back(callback);
+ // We need to interrupt the main thread if this callback is scheduled to
+ // execute at an earlier time than the earliest callback registered so far.
+ interrupt_needed = m_callbacks.empty() || point < m_callbacks.top().first;
+ m_callbacks.emplace(point, callback);
}
- TriggerPendingCallbacks();
+ if (interrupt_needed)
----------------
labath wrote:
> If I understand the logic here, we're checking before emplacement if an interrupt is needed and then once we emplace we Interrupt,
That's correct.
> Why are we setting interrupt needed when callbacks is empty?
Empty callbacks mean an infinite timeout (line MainLoopPosix.cpp, line 52). In that case, we definitely need to interrupt the polling thread (so it can go to sleep with a new timeout). In other cases, we only need to interrupt if this operation is scheduled to run before the previous earliest operation.
> Should we invert this where we trigger the interrupts before a potentially long emplace (this is in response to my nanoseconds fidelity question)
I think you're misunderstanding something here. The emplace is always fast - it just adds something to the queue and doesn't cause any callbacks to run. The callbacks will always be run on the mainloop thread. The interrupt needs to happen after the insertion to make sure the other thread observes the new callback (via the result of `GetNextWakeupTime`).
https://github.com/llvm/llvm-project/pull/112895
More information about the lldb-commits
mailing list