1e4d4801cSKuba Brecka //===-- ThreadCollection.cpp ------------------------------------*- C++ -*-===// 2e4d4801cSKuba Brecka // 3e4d4801cSKuba Brecka // The LLVM Compiler Infrastructure 4e4d4801cSKuba Brecka // 5e4d4801cSKuba Brecka // This file is distributed under the University of Illinois Open Source 6e4d4801cSKuba Brecka // License. See LICENSE.TXT for details. 7e4d4801cSKuba Brecka // 8e4d4801cSKuba Brecka //===----------------------------------------------------------------------===// 9e4d4801cSKuba Brecka #include <stdlib.h> 10e4d4801cSKuba Brecka 11e4d4801cSKuba Brecka #include <algorithm> 12940f425aSGreg Clayton #include <mutex> 13e4d4801cSKuba Brecka 1488f86b60SGreg Clayton #include "lldb/Target/Thread.h" 15*b9c1b51eSKate Stone #include "lldb/Target/ThreadCollection.h" 16e4d4801cSKuba Brecka 17e4d4801cSKuba Brecka using namespace lldb; 18e4d4801cSKuba Brecka using namespace lldb_private; 19e4d4801cSKuba Brecka 20*b9c1b51eSKate Stone ThreadCollection::ThreadCollection() : m_threads(), m_mutex() {} 21e4d4801cSKuba Brecka 22*b9c1b51eSKate Stone ThreadCollection::ThreadCollection(collection threads) 23*b9c1b51eSKate Stone : m_threads(threads), m_mutex() {} 24e4d4801cSKuba Brecka 25*b9c1b51eSKate Stone void ThreadCollection::AddThread(const ThreadSP &thread_sp) { 26bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex()); 27e4d4801cSKuba Brecka m_threads.push_back(thread_sp); 28e4d4801cSKuba Brecka } 29e4d4801cSKuba Brecka 30*b9c1b51eSKate Stone void ThreadCollection::AddThreadSortedByIndexID(const ThreadSP &thread_sp) { 3188f86b60SGreg Clayton std::lock_guard<std::recursive_mutex> guard(GetMutex()); 3288f86b60SGreg Clayton // Make sure we always keep the threads sorted by thread index ID 3388f86b60SGreg Clayton const uint32_t thread_index_id = thread_sp->GetIndexID(); 3488f86b60SGreg Clayton if (m_threads.empty() || m_threads.back()->GetIndexID() < thread_index_id) 3588f86b60SGreg Clayton m_threads.push_back(thread_sp); 36*b9c1b51eSKate Stone else { 37*b9c1b51eSKate Stone m_threads.insert( 38*b9c1b51eSKate Stone std::upper_bound(m_threads.begin(), m_threads.end(), thread_sp, 39*b9c1b51eSKate Stone [](const ThreadSP &lhs, const ThreadSP &rhs) -> bool { 4088f86b60SGreg Clayton return lhs->GetIndexID() < rhs->GetIndexID(); 41*b9c1b51eSKate Stone }), 42*b9c1b51eSKate Stone thread_sp); 4388f86b60SGreg Clayton } 4488f86b60SGreg Clayton } 4588f86b60SGreg Clayton 46*b9c1b51eSKate Stone void ThreadCollection::InsertThread(const lldb::ThreadSP &thread_sp, 47*b9c1b51eSKate Stone uint32_t idx) { 48bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex()); 49e4d4801cSKuba Brecka if (idx < m_threads.size()) 50e4d4801cSKuba Brecka m_threads.insert(m_threads.begin() + idx, thread_sp); 51e4d4801cSKuba Brecka else 52e4d4801cSKuba Brecka m_threads.push_back(thread_sp); 53e4d4801cSKuba Brecka } 54e4d4801cSKuba Brecka 55*b9c1b51eSKate Stone uint32_t ThreadCollection::GetSize() { 56bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex()); 57e4d4801cSKuba Brecka return m_threads.size(); 58e4d4801cSKuba Brecka } 59e4d4801cSKuba Brecka 60*b9c1b51eSKate Stone ThreadSP ThreadCollection::GetThreadAtIndex(uint32_t idx) { 61bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex()); 62e4d4801cSKuba Brecka ThreadSP thread_sp; 63e4d4801cSKuba Brecka if (idx < m_threads.size()) 64e4d4801cSKuba Brecka thread_sp = m_threads[idx]; 65e4d4801cSKuba Brecka return thread_sp; 66e4d4801cSKuba Brecka } 67