LLDB mainline
MainLoopBase.cpp
Go to the documentation of this file.
1//===-- MainLoopBase.cpp --------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include <chrono>
11
12using namespace lldb;
13using namespace lldb_private;
14
15bool MainLoopBase::AddCallback(const Callback &callback, TimePoint point) {
16 bool interrupt_needed;
17 bool interrupt_succeeded = true;
18 {
19 std::lock_guard<std::mutex> lock{m_callback_mutex};
20 // We need to interrupt the main thread if this callback is scheduled to
21 // execute at an earlier time than the earliest callback registered so far.
22 interrupt_needed =
23 m_callbacks.empty() || point < m_callbacks.top().time_point;
24 m_callbacks.emplace(point, callback, m_callback_sequence++);
25 }
26 if (interrupt_needed)
27 interrupt_succeeded = Interrupt();
28 return interrupt_succeeded;
29}
30
32 while (true) {
33 Callback callback;
34 {
35 std::lock_guard<std::mutex> lock{m_callback_mutex};
36 if (m_callbacks.empty() ||
37 std::chrono::steady_clock::now() < m_callbacks.top().time_point)
38 return;
39 callback = std::move(m_callbacks.top().callback);
40 m_callbacks.pop();
41 }
42
43 callback(*this);
44 }
45}
46
47std::optional<MainLoopBase::TimePoint> MainLoopBase::GetNextWakeupTime() {
48 std::lock_guard<std::mutex> lock(m_callback_mutex);
49 if (m_callbacks.empty())
50 return std::nullopt;
51 return m_callbacks.top().time_point;
52}
std::chrono::time_point< std::chrono::steady_clock, std::chrono::nanoseconds > TimePoint
std::priority_queue< CallbackEntry > m_callbacks
std::optional< TimePoint > GetNextWakeupTime()
std::function< void(MainLoopBase &)> Callback
virtual bool Interrupt()=0
Interrupt the loop that is currently waiting for events.
bool AddCallback(const Callback &callback, std::chrono::nanoseconds delay)
A class that represents a running process on the host machine.