xref: /freebsd/contrib/llvm-project/lldb/source/Host/common/MainLoopBase.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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 
AddPendingCallback(const Callback & callback)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 
ProcessPendingCallbacks()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