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 
9 #include "lldb/Host/MainLoopBase.h"
10 
11 using namespace lldb;
12 using namespace lldb_private;
13 
14 void MainLoopBase::AddPendingCallback(const Callback &callback) {
15   {
16     std::lock_guard<std::mutex> lock{m_callback_mutex};
17     m_pending_callbacks.push_back(callback);
18   }
19   TriggerPendingCallbacks();
20 }
21 
22 void MainLoopBase::ProcessPendingCallbacks() {
23   // Move the callbacks to a local vector to avoid keeping m_pending_callbacks
24   // locked throughout the calls.
25   std::vector<Callback> pending_callbacks;
26   {
27     std::lock_guard<std::mutex> lock{m_callback_mutex};
28     pending_callbacks = std::move(m_pending_callbacks);
29   }
30 
31   for (const Callback &callback : pending_callbacks)
32     callback(*this);
33 }
34