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"
14e4d4801cSKuba Brecka 
15e4d4801cSKuba Brecka using namespace lldb;
16e4d4801cSKuba Brecka using namespace lldb_private;
17e4d4801cSKuba Brecka 
18e4d4801cSKuba Brecka ThreadCollection::ThreadCollection() :
19e4d4801cSKuba Brecka     m_threads(),
20e4d4801cSKuba Brecka     m_mutex()
21e4d4801cSKuba Brecka {
22e4d4801cSKuba Brecka }
23e4d4801cSKuba Brecka 
24e4d4801cSKuba Brecka ThreadCollection::ThreadCollection(collection threads) :
25e4d4801cSKuba Brecka     m_threads(threads),
26e4d4801cSKuba Brecka     m_mutex()
27e4d4801cSKuba Brecka {
28e4d4801cSKuba Brecka }
29e4d4801cSKuba Brecka 
30e4d4801cSKuba Brecka void
31e4d4801cSKuba Brecka ThreadCollection::AddThread (const ThreadSP &thread_sp)
32e4d4801cSKuba Brecka {
33*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
34e4d4801cSKuba Brecka     m_threads.push_back (thread_sp);
35e4d4801cSKuba Brecka }
36e4d4801cSKuba Brecka 
37e4d4801cSKuba Brecka void
38e4d4801cSKuba Brecka ThreadCollection::InsertThread (const lldb::ThreadSP &thread_sp, uint32_t idx)
39e4d4801cSKuba Brecka {
40*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
41e4d4801cSKuba Brecka     if (idx < m_threads.size())
42e4d4801cSKuba Brecka         m_threads.insert(m_threads.begin() + idx, thread_sp);
43e4d4801cSKuba Brecka     else
44e4d4801cSKuba Brecka         m_threads.push_back (thread_sp);
45e4d4801cSKuba Brecka }
46e4d4801cSKuba Brecka 
47e4d4801cSKuba Brecka uint32_t
48e4d4801cSKuba Brecka ThreadCollection::GetSize ()
49e4d4801cSKuba Brecka {
50*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
51e4d4801cSKuba Brecka     return m_threads.size();
52e4d4801cSKuba Brecka }
53e4d4801cSKuba Brecka 
54e4d4801cSKuba Brecka ThreadSP
55e4d4801cSKuba Brecka ThreadCollection::GetThreadAtIndex (uint32_t idx)
56e4d4801cSKuba Brecka {
57*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
58e4d4801cSKuba Brecka     ThreadSP thread_sp;
59e4d4801cSKuba Brecka     if (idx < m_threads.size())
60e4d4801cSKuba Brecka         thread_sp = m_threads[idx];
61e4d4801cSKuba Brecka     return thread_sp;
62e4d4801cSKuba Brecka }
63