1//===-- Progress.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 9#include "lldb/Core/Progress.h" 10 11#include "lldb/Core/Debugger.h" 12#include "lldb/Utility/StreamString.h" 13 14#include <optional> 15 16using namespace lldb; 17using namespace lldb_private; 18 19std::atomic<uint64_t> Progress::g_id(0); 20 21Progress::Progress(std::string title, std::string details, 22 std::optional<uint64_t> total, 23 lldb_private::Debugger *debugger) 24 : m_title(title), m_details(details), m_id(++g_id), m_completed(0), 25 m_total(1) { 26 assert(total == std::nullopt || total > 0); 27 if (total) 28 m_total = *total; 29 30 if (debugger) 31 m_debugger_id = debugger->GetID(); 32 std::lock_guard<std::mutex> guard(m_mutex); 33 ReportProgress(); 34} 35 36Progress::~Progress() { 37 // Make sure to always report progress completed when this object is 38 // destructed so it indicates the progress dialog/activity should go away. 39 std::lock_guard<std::mutex> guard(m_mutex); 40 if (!m_completed) 41 m_completed = m_total; 42 ReportProgress(); 43} 44 45void Progress::Increment(uint64_t amount, 46 std::optional<std::string> updated_detail) { 47 if (amount > 0) { 48 std::lock_guard<std::mutex> guard(m_mutex); 49 if (updated_detail) 50 m_details = std::move(updated_detail.value()); 51 // Watch out for unsigned overflow and make sure we don't increment too 52 // much and exceed m_total. 53 if (m_total && (amount > (m_total - m_completed))) 54 m_completed = m_total; 55 else 56 m_completed += amount; 57 ReportProgress(); 58 } 59} 60 61void Progress::ReportProgress() { 62 if (!m_complete) { 63 // Make sure we only send one notification that indicates the progress is 64 // complete 65 m_complete = m_completed == m_total; 66 Debugger::ReportProgress(m_id, m_title, m_details, m_completed, m_total, 67 m_debugger_id); 68 } 69} 70