Path: blob/main/contrib/llvm-project/lldb/source/Target/ThreadCollection.cpp
39587 views
//===-- ThreadCollection.cpp ----------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7#include <cstdlib>89#include <algorithm>10#include <mutex>1112#include "lldb/Target/Thread.h"13#include "lldb/Target/ThreadCollection.h"1415using namespace lldb;16using namespace lldb_private;1718ThreadCollection::ThreadCollection() : m_threads(), m_mutex() {}1920ThreadCollection::ThreadCollection(collection threads)21: m_threads(threads), m_mutex() {}2223void ThreadCollection::AddThread(const ThreadSP &thread_sp) {24std::lock_guard<std::recursive_mutex> guard(GetMutex());25m_threads.push_back(thread_sp);26}2728void ThreadCollection::AddThreadSortedByIndexID(const ThreadSP &thread_sp) {29std::lock_guard<std::recursive_mutex> guard(GetMutex());30// Make sure we always keep the threads sorted by thread index ID31const uint32_t thread_index_id = thread_sp->GetIndexID();32if (m_threads.empty() || m_threads.back()->GetIndexID() < thread_index_id)33m_threads.push_back(thread_sp);34else {35m_threads.insert(36llvm::upper_bound(m_threads, thread_sp,37[](const ThreadSP &lhs, const ThreadSP &rhs) -> bool {38return lhs->GetIndexID() < rhs->GetIndexID();39}),40thread_sp);41}42}4344void ThreadCollection::InsertThread(const lldb::ThreadSP &thread_sp,45uint32_t idx) {46std::lock_guard<std::recursive_mutex> guard(GetMutex());47if (idx < m_threads.size())48m_threads.insert(m_threads.begin() + idx, thread_sp);49else50m_threads.push_back(thread_sp);51}5253uint32_t ThreadCollection::GetSize() {54std::lock_guard<std::recursive_mutex> guard(GetMutex());55return m_threads.size();56}5758ThreadSP ThreadCollection::GetThreadAtIndex(uint32_t idx) {59std::lock_guard<std::recursive_mutex> guard(GetMutex());60ThreadSP thread_sp;61if (idx < m_threads.size())62thread_sp = m_threads[idx];63return thread_sp;64}656667