brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.6 KiB · 232b9bc Raw
52 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Host/MainLoopBase.h"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 to21    // 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 30void MainLoopBase::ProcessCallbacks() {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}52