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> 12*940f425aSGreg Clayton #include <mutex> 13e4d4801cSKuba Brecka 14e4d4801cSKuba Brecka #include "lldb/Target/ThreadCollection.h" 1588f86b60SGreg Clayton #include "lldb/Target/Thread.h" 16e4d4801cSKuba Brecka 17e4d4801cSKuba Brecka using namespace lldb; 18e4d4801cSKuba Brecka using namespace lldb_private; 19e4d4801cSKuba Brecka 20e4d4801cSKuba Brecka ThreadCollection::ThreadCollection() : 21e4d4801cSKuba Brecka m_threads(), 22e4d4801cSKuba Brecka m_mutex() 23e4d4801cSKuba Brecka { 24e4d4801cSKuba Brecka } 25e4d4801cSKuba Brecka 26e4d4801cSKuba Brecka ThreadCollection::ThreadCollection(collection threads) : 27e4d4801cSKuba Brecka m_threads(threads), 28e4d4801cSKuba Brecka m_mutex() 29e4d4801cSKuba Brecka { 30e4d4801cSKuba Brecka } 31e4d4801cSKuba Brecka 32e4d4801cSKuba Brecka void 33e4d4801cSKuba Brecka ThreadCollection::AddThread (const ThreadSP &thread_sp) 34e4d4801cSKuba Brecka { 35bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex()); 36e4d4801cSKuba Brecka m_threads.push_back (thread_sp); 37e4d4801cSKuba Brecka } 38e4d4801cSKuba Brecka 39e4d4801cSKuba Brecka void 4088f86b60SGreg Clayton ThreadCollection::AddThreadSortedByIndexID (const ThreadSP &thread_sp) 4188f86b60SGreg Clayton { 4288f86b60SGreg Clayton std::lock_guard<std::recursive_mutex> guard(GetMutex()); 4388f86b60SGreg Clayton // Make sure we always keep the threads sorted by thread index ID 4488f86b60SGreg Clayton const uint32_t thread_index_id = thread_sp->GetIndexID(); 4588f86b60SGreg Clayton if (m_threads.empty() || m_threads.back()->GetIndexID() < thread_index_id) 4688f86b60SGreg Clayton m_threads.push_back (thread_sp); 4788f86b60SGreg Clayton else 4888f86b60SGreg Clayton { 4988f86b60SGreg Clayton m_threads.insert(std::upper_bound(m_threads.begin(), m_threads.end(), thread_sp, 5088f86b60SGreg Clayton [] (const ThreadSP &lhs, const ThreadSP &rhs) -> bool 5188f86b60SGreg Clayton { 5288f86b60SGreg Clayton return lhs->GetIndexID() < rhs->GetIndexID(); 5388f86b60SGreg Clayton }), thread_sp); 5488f86b60SGreg Clayton } 5588f86b60SGreg Clayton } 5688f86b60SGreg Clayton 5788f86b60SGreg Clayton void 58e4d4801cSKuba Brecka ThreadCollection::InsertThread (const lldb::ThreadSP &thread_sp, uint32_t idx) 59e4d4801cSKuba Brecka { 60bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex()); 61e4d4801cSKuba Brecka if (idx < m_threads.size()) 62e4d4801cSKuba Brecka m_threads.insert(m_threads.begin() + idx, thread_sp); 63e4d4801cSKuba Brecka else 64e4d4801cSKuba Brecka m_threads.push_back (thread_sp); 65e4d4801cSKuba Brecka } 66e4d4801cSKuba Brecka 67e4d4801cSKuba Brecka uint32_t 68e4d4801cSKuba Brecka ThreadCollection::GetSize () 69e4d4801cSKuba Brecka { 70bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex()); 71e4d4801cSKuba Brecka return m_threads.size(); 72e4d4801cSKuba Brecka } 73e4d4801cSKuba Brecka 74e4d4801cSKuba Brecka ThreadSP 75e4d4801cSKuba Brecka ThreadCollection::GetThreadAtIndex (uint32_t idx) 76e4d4801cSKuba Brecka { 77bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex()); 78e4d4801cSKuba Brecka ThreadSP thread_sp; 79e4d4801cSKuba Brecka if (idx < m_threads.size()) 80e4d4801cSKuba Brecka thread_sp = m_threads[idx]; 81e4d4801cSKuba Brecka return thread_sp; 82e4d4801cSKuba Brecka } 83