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