1 //===-- ThreadCollection.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 #include "lldb/Target/Thread.h"
15 #include "lldb/Target/ThreadCollection.h"
18 using namespace lldb_private;
20 ThreadCollection::ThreadCollection() : m_threads(), m_mutex() {}
22 ThreadCollection::ThreadCollection(collection threads)
23 : m_threads(threads), m_mutex() {}
25 void ThreadCollection::AddThread(const ThreadSP &thread_sp) {
26 std::lock_guard<std::recursive_mutex> guard(GetMutex());
27 m_threads.push_back(thread_sp);
30 void ThreadCollection::AddThreadSortedByIndexID(const ThreadSP &thread_sp) {
31 std::lock_guard<std::recursive_mutex> guard(GetMutex());
32 // Make sure we always keep the threads sorted by thread index ID
33 const uint32_t thread_index_id = thread_sp->GetIndexID();
34 if (m_threads.empty() || m_threads.back()->GetIndexID() < thread_index_id)
35 m_threads.push_back(thread_sp);
38 std::upper_bound(m_threads.begin(), m_threads.end(), thread_sp,
39 [](const ThreadSP &lhs, const ThreadSP &rhs) -> bool {
40 return lhs->GetIndexID() < rhs->GetIndexID();
46 void ThreadCollection::InsertThread(const lldb::ThreadSP &thread_sp,
48 std::lock_guard<std::recursive_mutex> guard(GetMutex());
49 if (idx < m_threads.size())
50 m_threads.insert(m_threads.begin() + idx, thread_sp);
52 m_threads.push_back(thread_sp);
55 uint32_t ThreadCollection::GetSize() {
56 std::lock_guard<std::recursive_mutex> guard(GetMutex());
57 return m_threads.size();
60 ThreadSP ThreadCollection::GetThreadAtIndex(uint32_t idx) {
61 std::lock_guard<std::recursive_mutex> guard(GetMutex());
63 if (idx < m_threads.size())
64 thread_sp = m_threads[idx];