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 = m_callbacks.empty() || point < m_callbacks.top().first;
23 m_callbacks.emplace(point, callback);
24 }
25 if (interrupt_needed)
26 interrupt_succeeded = Interrupt();
27 return interrupt_succeeded;
28}
29
31 while (true) {
32 Callback callback;
33 {
34 std::lock_guard<std::mutex> lock{m_callback_mutex};
35 if (m_callbacks.empty() ||
36 std::chrono::steady_clock::now() < m_callbacks.top().first)
37 return;
38 callback = std::move(m_callbacks.top().second);
39 m_callbacks.pop();
40 }
41
42 callback(*this);
43 }
44}
45
46std::optional<MainLoopBase::TimePoint> MainLoopBase::GetNextWakeupTime() {
47 std::lock_guard<std::mutex> lock(m_callback_mutex);
48 if (m_callbacks.empty())
49 return std::nullopt;
50 return m_callbacks.top().first;
51}
std::priority_queue< std::pair< TimePoint, Callback >, std::vector< std::pair< TimePoint, Callback > >, llvm::on_first< std::greater< TimePoint > > > m_callbacks
std::chrono::time_point< std::chrono::steady_clock, std::chrono::nanoseconds > TimePoint
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.