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> 12e4d4801cSKuba Brecka 13e4d4801cSKuba Brecka #include "lldb/Target/ThreadCollection.h" 14*88f86b60SGreg Clayton #include "lldb/Target/Thread.h" 15e4d4801cSKuba Brecka 16e4d4801cSKuba Brecka using namespace lldb; 17e4d4801cSKuba Brecka using namespace lldb_private; 18e4d4801cSKuba Brecka 19e4d4801cSKuba Brecka ThreadCollection::ThreadCollection() : 20e4d4801cSKuba Brecka m_threads(), 21e4d4801cSKuba Brecka m_mutex() 22e4d4801cSKuba Brecka { 23e4d4801cSKuba Brecka } 24e4d4801cSKuba Brecka 25e4d4801cSKuba Brecka ThreadCollection::ThreadCollection(collection threads) : 26e4d4801cSKuba Brecka m_threads(threads), 27e4d4801cSKuba Brecka m_mutex() 28e4d4801cSKuba Brecka { 29e4d4801cSKuba Brecka } 30e4d4801cSKuba Brecka 31e4d4801cSKuba Brecka void 32e4d4801cSKuba Brecka ThreadCollection::AddThread (const ThreadSP &thread_sp) 33e4d4801cSKuba Brecka { 34bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex()); 35e4d4801cSKuba Brecka m_threads.push_back (thread_sp); 36e4d4801cSKuba Brecka } 37e4d4801cSKuba Brecka 38e4d4801cSKuba Brecka void 39*88f86b60SGreg Clayton ThreadCollection::AddThreadSortedByIndexID (const ThreadSP &thread_sp) 40*88f86b60SGreg Clayton { 41*88f86b60SGreg Clayton std::lock_guard<std::recursive_mutex> guard(GetMutex()); 42*88f86b60SGreg Clayton // Make sure we always keep the threads sorted by thread index ID 43*88f86b60SGreg Clayton const uint32_t thread_index_id = thread_sp->GetIndexID(); 44*88f86b60SGreg Clayton if (m_threads.empty() || m_threads.back()->GetIndexID() < thread_index_id) 45*88f86b60SGreg Clayton m_threads.push_back (thread_sp); 46*88f86b60SGreg Clayton else 47*88f86b60SGreg Clayton { 48*88f86b60SGreg Clayton m_threads.insert(std::upper_bound(m_threads.begin(), m_threads.end(), thread_sp, 49*88f86b60SGreg Clayton [] (const ThreadSP &lhs, const ThreadSP &rhs) -> bool 50*88f86b60SGreg Clayton { 51*88f86b60SGreg Clayton return lhs->GetIndexID() < rhs->GetIndexID(); 52*88f86b60SGreg Clayton }), thread_sp); 53*88f86b60SGreg Clayton } 54*88f86b60SGreg Clayton } 55*88f86b60SGreg Clayton 56*88f86b60SGreg Clayton void 57e4d4801cSKuba Brecka ThreadCollection::InsertThread (const lldb::ThreadSP &thread_sp, uint32_t idx) 58e4d4801cSKuba Brecka { 59bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex()); 60e4d4801cSKuba Brecka if (idx < m_threads.size()) 61e4d4801cSKuba Brecka m_threads.insert(m_threads.begin() + idx, thread_sp); 62e4d4801cSKuba Brecka else 63e4d4801cSKuba Brecka m_threads.push_back (thread_sp); 64e4d4801cSKuba Brecka } 65e4d4801cSKuba Brecka 66e4d4801cSKuba Brecka uint32_t 67e4d4801cSKuba Brecka ThreadCollection::GetSize () 68e4d4801cSKuba Brecka { 69bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex()); 70e4d4801cSKuba Brecka return m_threads.size(); 71e4d4801cSKuba Brecka } 72e4d4801cSKuba Brecka 73e4d4801cSKuba Brecka ThreadSP 74e4d4801cSKuba Brecka ThreadCollection::GetThreadAtIndex (uint32_t idx) 75e4d4801cSKuba Brecka { 76bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(GetMutex()); 77e4d4801cSKuba Brecka ThreadSP thread_sp; 78e4d4801cSKuba Brecka if (idx < m_threads.size()) 79e4d4801cSKuba Brecka thread_sp = m_threads[idx]; 80e4d4801cSKuba Brecka return thread_sp; 81e4d4801cSKuba Brecka } 82