180814287SRaphael Isemann //===-- ThreadCollection.cpp ----------------------------------------------===//
2e4d4801cSKuba Brecka //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e4d4801cSKuba Brecka //
7e4d4801cSKuba Brecka //===----------------------------------------------------------------------===//
8*76e47d48SRaphael Isemann #include <cstdlib>
9e4d4801cSKuba Brecka
10e4d4801cSKuba Brecka #include <algorithm>
11940f425aSGreg Clayton #include <mutex>
12e4d4801cSKuba Brecka
1388f86b60SGreg Clayton #include "lldb/Target/Thread.h"
14b9c1b51eSKate Stone #include "lldb/Target/ThreadCollection.h"
15e4d4801cSKuba Brecka
16e4d4801cSKuba Brecka using namespace lldb;
17e4d4801cSKuba Brecka using namespace lldb_private;
18e4d4801cSKuba Brecka
ThreadCollection()19b9c1b51eSKate Stone ThreadCollection::ThreadCollection() : m_threads(), m_mutex() {}
20e4d4801cSKuba Brecka
ThreadCollection(collection threads)21b9c1b51eSKate Stone ThreadCollection::ThreadCollection(collection threads)
22b9c1b51eSKate Stone : m_threads(threads), m_mutex() {}
23e4d4801cSKuba Brecka
AddThread(const ThreadSP & thread_sp)24b9c1b51eSKate Stone void ThreadCollection::AddThread(const ThreadSP &thread_sp) {
25bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex());
26e4d4801cSKuba Brecka m_threads.push_back(thread_sp);
27e4d4801cSKuba Brecka }
28e4d4801cSKuba Brecka
AddThreadSortedByIndexID(const ThreadSP & thread_sp)29b9c1b51eSKate Stone void ThreadCollection::AddThreadSortedByIndexID(const ThreadSP &thread_sp) {
3088f86b60SGreg Clayton std::lock_guard<std::recursive_mutex> guard(GetMutex());
3188f86b60SGreg Clayton // Make sure we always keep the threads sorted by thread index ID
3288f86b60SGreg Clayton const uint32_t thread_index_id = thread_sp->GetIndexID();
3388f86b60SGreg Clayton if (m_threads.empty() || m_threads.back()->GetIndexID() < thread_index_id)
3488f86b60SGreg Clayton m_threads.push_back(thread_sp);
35b9c1b51eSKate Stone else {
36b9c1b51eSKate Stone m_threads.insert(
37b9c1b51eSKate Stone std::upper_bound(m_threads.begin(), m_threads.end(), thread_sp,
38b9c1b51eSKate Stone [](const ThreadSP &lhs, const ThreadSP &rhs) -> bool {
3988f86b60SGreg Clayton return lhs->GetIndexID() < rhs->GetIndexID();
40b9c1b51eSKate Stone }),
41b9c1b51eSKate Stone thread_sp);
4288f86b60SGreg Clayton }
4388f86b60SGreg Clayton }
4488f86b60SGreg Clayton
InsertThread(const lldb::ThreadSP & thread_sp,uint32_t idx)45b9c1b51eSKate Stone void ThreadCollection::InsertThread(const lldb::ThreadSP &thread_sp,
46b9c1b51eSKate Stone uint32_t idx) {
47bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex());
48e4d4801cSKuba Brecka if (idx < m_threads.size())
49e4d4801cSKuba Brecka m_threads.insert(m_threads.begin() + idx, thread_sp);
50e4d4801cSKuba Brecka else
51e4d4801cSKuba Brecka m_threads.push_back(thread_sp);
52e4d4801cSKuba Brecka }
53e4d4801cSKuba Brecka
GetSize()54b9c1b51eSKate Stone uint32_t ThreadCollection::GetSize() {
55bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex());
56e4d4801cSKuba Brecka return m_threads.size();
57e4d4801cSKuba Brecka }
58e4d4801cSKuba Brecka
GetThreadAtIndex(uint32_t idx)59b9c1b51eSKate Stone ThreadSP ThreadCollection::GetThreadAtIndex(uint32_t idx) {
60bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex());
61e4d4801cSKuba Brecka ThreadSP thread_sp;
62e4d4801cSKuba Brecka if (idx < m_threads.size())
63e4d4801cSKuba Brecka thread_sp = m_threads[idx];
64e4d4801cSKuba Brecka return thread_sp;
65e4d4801cSKuba Brecka }
66