17aa51b79SEd Maste //===-- ThreadCollection.cpp ------------------------------------*- C++ -*-===//
27aa51b79SEd Maste //
37aa51b79SEd Maste // The LLVM Compiler Infrastructure
47aa51b79SEd Maste //
57aa51b79SEd Maste // This file is distributed under the University of Illinois Open Source
67aa51b79SEd Maste // License. See LICENSE.TXT for details.
77aa51b79SEd Maste //
87aa51b79SEd Maste //===----------------------------------------------------------------------===//
97aa51b79SEd Maste #include <stdlib.h>
107aa51b79SEd Maste
117aa51b79SEd Maste #include <algorithm>
124bb0738eSEd Maste #include <mutex>
137aa51b79SEd Maste
144bb0738eSEd Maste #include "lldb/Target/Thread.h"
15*435933ddSDimitry Andric #include "lldb/Target/ThreadCollection.h"
167aa51b79SEd Maste
177aa51b79SEd Maste using namespace lldb;
187aa51b79SEd Maste using namespace lldb_private;
197aa51b79SEd Maste
ThreadCollection()20*435933ddSDimitry Andric ThreadCollection::ThreadCollection() : m_threads(), m_mutex() {}
217aa51b79SEd Maste
ThreadCollection(collection threads)22*435933ddSDimitry Andric ThreadCollection::ThreadCollection(collection threads)
23*435933ddSDimitry Andric : m_threads(threads), m_mutex() {}
247aa51b79SEd Maste
AddThread(const ThreadSP & thread_sp)25*435933ddSDimitry Andric void ThreadCollection::AddThread(const ThreadSP &thread_sp) {
264bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(GetMutex());
277aa51b79SEd Maste m_threads.push_back(thread_sp);
287aa51b79SEd Maste }
297aa51b79SEd Maste
AddThreadSortedByIndexID(const ThreadSP & thread_sp)30*435933ddSDimitry Andric void ThreadCollection::AddThreadSortedByIndexID(const ThreadSP &thread_sp) {
314bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(GetMutex());
324bb0738eSEd Maste // Make sure we always keep the threads sorted by thread index ID
334bb0738eSEd Maste const uint32_t thread_index_id = thread_sp->GetIndexID();
344bb0738eSEd Maste if (m_threads.empty() || m_threads.back()->GetIndexID() < thread_index_id)
354bb0738eSEd Maste m_threads.push_back(thread_sp);
36*435933ddSDimitry Andric else {
37*435933ddSDimitry Andric m_threads.insert(
38*435933ddSDimitry Andric std::upper_bound(m_threads.begin(), m_threads.end(), thread_sp,
39*435933ddSDimitry Andric [](const ThreadSP &lhs, const ThreadSP &rhs) -> bool {
404bb0738eSEd Maste return lhs->GetIndexID() < rhs->GetIndexID();
41*435933ddSDimitry Andric }),
42*435933ddSDimitry Andric thread_sp);
434bb0738eSEd Maste }
444bb0738eSEd Maste }
454bb0738eSEd Maste
InsertThread(const lldb::ThreadSP & thread_sp,uint32_t idx)46*435933ddSDimitry Andric void ThreadCollection::InsertThread(const lldb::ThreadSP &thread_sp,
47*435933ddSDimitry Andric uint32_t idx) {
484bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(GetMutex());
497aa51b79SEd Maste if (idx < m_threads.size())
507aa51b79SEd Maste m_threads.insert(m_threads.begin() + idx, thread_sp);
517aa51b79SEd Maste else
527aa51b79SEd Maste m_threads.push_back(thread_sp);
537aa51b79SEd Maste }
547aa51b79SEd Maste
GetSize()55*435933ddSDimitry Andric uint32_t ThreadCollection::GetSize() {
564bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(GetMutex());
577aa51b79SEd Maste return m_threads.size();
587aa51b79SEd Maste }
597aa51b79SEd Maste
GetThreadAtIndex(uint32_t idx)60*435933ddSDimitry Andric ThreadSP ThreadCollection::GetThreadAtIndex(uint32_t idx) {
614bb0738eSEd Maste std::lock_guard<std::recursive_mutex> guard(GetMutex());
627aa51b79SEd Maste ThreadSP thread_sp;
637aa51b79SEd Maste if (idx < m_threads.size())
647aa51b79SEd Maste thread_sp = m_threads[idx];
657aa51b79SEd Maste return thread_sp;
667aa51b79SEd Maste }
67