1cfca06d7SDimitry Andric //===-- ThreadCollection.cpp ----------------------------------------------===//
2205afe67SEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6205afe67SEd Maste //
7205afe67SEd Maste //===----------------------------------------------------------------------===//
8344a3780SDimitry Andric #include <cstdlib>
9205afe67SEd Maste
10205afe67SEd Maste #include <algorithm>
11f3fbd1c0SDimitry Andric #include <mutex>
12205afe67SEd Maste
13f3fbd1c0SDimitry Andric #include "lldb/Target/Thread.h"
1414f1b3e8SDimitry Andric #include "lldb/Target/ThreadCollection.h"
15205afe67SEd Maste
16205afe67SEd Maste using namespace lldb;
17205afe67SEd Maste using namespace lldb_private;
18205afe67SEd Maste
ThreadCollection()1914f1b3e8SDimitry Andric ThreadCollection::ThreadCollection() : m_threads(), m_mutex() {}
20205afe67SEd Maste
ThreadCollection(collection threads)2114f1b3e8SDimitry Andric ThreadCollection::ThreadCollection(collection threads)
2214f1b3e8SDimitry Andric : m_threads(threads), m_mutex() {}
23205afe67SEd Maste
AddThread(const ThreadSP & thread_sp)2414f1b3e8SDimitry Andric void ThreadCollection::AddThread(const ThreadSP &thread_sp) {
25f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(GetMutex());
26205afe67SEd Maste m_threads.push_back(thread_sp);
27205afe67SEd Maste }
28205afe67SEd Maste
AddThreadSortedByIndexID(const ThreadSP & thread_sp)2914f1b3e8SDimitry Andric void ThreadCollection::AddThreadSortedByIndexID(const ThreadSP &thread_sp) {
30f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(GetMutex());
31f3fbd1c0SDimitry Andric // Make sure we always keep the threads sorted by thread index ID
32f3fbd1c0SDimitry Andric const uint32_t thread_index_id = thread_sp->GetIndexID();
33f3fbd1c0SDimitry Andric if (m_threads.empty() || m_threads.back()->GetIndexID() < thread_index_id)
34f3fbd1c0SDimitry Andric m_threads.push_back(thread_sp);
3514f1b3e8SDimitry Andric else {
3614f1b3e8SDimitry Andric m_threads.insert(
37e3b55780SDimitry Andric llvm::upper_bound(m_threads, thread_sp,
3814f1b3e8SDimitry Andric [](const ThreadSP &lhs, const ThreadSP &rhs) -> bool {
39f3fbd1c0SDimitry Andric return lhs->GetIndexID() < rhs->GetIndexID();
4014f1b3e8SDimitry Andric }),
4114f1b3e8SDimitry Andric thread_sp);
42f3fbd1c0SDimitry Andric }
43f3fbd1c0SDimitry Andric }
44f3fbd1c0SDimitry Andric
InsertThread(const lldb::ThreadSP & thread_sp,uint32_t idx)4514f1b3e8SDimitry Andric void ThreadCollection::InsertThread(const lldb::ThreadSP &thread_sp,
4614f1b3e8SDimitry Andric uint32_t idx) {
47f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(GetMutex());
48205afe67SEd Maste if (idx < m_threads.size())
49205afe67SEd Maste m_threads.insert(m_threads.begin() + idx, thread_sp);
50205afe67SEd Maste else
51205afe67SEd Maste m_threads.push_back(thread_sp);
52205afe67SEd Maste }
53205afe67SEd Maste
GetSize()5414f1b3e8SDimitry Andric uint32_t ThreadCollection::GetSize() {
55f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(GetMutex());
56205afe67SEd Maste return m_threads.size();
57205afe67SEd Maste }
58205afe67SEd Maste
GetThreadAtIndex(uint32_t idx)5914f1b3e8SDimitry Andric ThreadSP ThreadCollection::GetThreadAtIndex(uint32_t idx) {
60f3fbd1c0SDimitry Andric std::lock_guard<std::recursive_mutex> guard(GetMutex());
61205afe67SEd Maste ThreadSP thread_sp;
62205afe67SEd Maste if (idx < m_threads.size())
63205afe67SEd Maste thread_sp = m_threads[idx];
64205afe67SEd Maste return thread_sp;
65205afe67SEd Maste }
66