| 1 | //===-- ThreadCollection.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 | #include <cstdlib> |
| 9 | |
| 10 | #include <algorithm> |
| 11 | #include <mutex> |
| 12 | |
| 13 | #include "lldb/Target/Thread.h" |
| 14 | #include "lldb/Target/ThreadCollection.h" |
| 15 | |
| 16 | using namespace lldb; |
| 17 | using namespace lldb_private; |
| 18 | |
| 19 | ThreadCollection::ThreadCollection() : m_threads(), m_mutex() {} |
| 20 | |
| 21 | ThreadCollection::ThreadCollection(collection threads) |
| 22 | : m_threads(threads), m_mutex() {} |
| 23 | |
| 24 | void ThreadCollection::AddThread(const ThreadSP &thread_sp) { |
| 25 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 26 | m_threads.push_back(x: thread_sp); |
| 27 | } |
| 28 | |
| 29 | void ThreadCollection::AddThreadSortedByIndexID(const ThreadSP &thread_sp) { |
| 30 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 31 | // Make sure we always keep the threads sorted by thread index ID |
| 32 | const uint32_t thread_index_id = thread_sp->GetIndexID(); |
| 33 | if (m_threads.empty() || m_threads.back()->GetIndexID() < thread_index_id) |
| 34 | m_threads.push_back(x: thread_sp); |
| 35 | else { |
| 36 | m_threads.insert( |
| 37 | position: llvm::upper_bound(Range&: m_threads, Value: thread_sp, |
| 38 | C: [](const ThreadSP &lhs, const ThreadSP &rhs) -> bool { |
| 39 | return lhs->GetIndexID() < rhs->GetIndexID(); |
| 40 | }), |
| 41 | x: thread_sp); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void ThreadCollection::InsertThread(const lldb::ThreadSP &thread_sp, |
| 46 | uint32_t idx) { |
| 47 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 48 | if (idx < m_threads.size()) |
| 49 | m_threads.insert(position: m_threads.begin() + idx, x: thread_sp); |
| 50 | else |
| 51 | m_threads.push_back(x: thread_sp); |
| 52 | } |
| 53 | |
| 54 | uint32_t ThreadCollection::GetSize() { |
| 55 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 56 | return m_threads.size(); |
| 57 | } |
| 58 | |
| 59 | ThreadSP ThreadCollection::GetThreadAtIndex(uint32_t idx) { |
| 60 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
| 61 | ThreadSP thread_sp; |
| 62 | if (idx < m_threads.size()) |
| 63 | thread_sp = m_threads[idx]; |
| 64 | return thread_sp; |
| 65 | } |
| 66 |
