blob: cdccc4b34dd43015489102953f9aded62b5e7a82 [file] [log] [blame]
Kuba Breckae4d48012014-09-05 19:13:15 +00001//===-- 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
13#include "lldb/Target/ThreadCollection.h"
Greg Clayton88f86b62016-06-10 23:23:34 +000014#include "lldb/Target/Thread.h"
Kuba Breckae4d48012014-09-05 19:13:15 +000015
16using namespace lldb;
17using namespace lldb_private;
18
19ThreadCollection::ThreadCollection() :
20 m_threads(),
21 m_mutex()
22{
23}
24
25ThreadCollection::ThreadCollection(collection threads) :
26 m_threads(threads),
27 m_mutex()
28{
29}
30
31void
32ThreadCollection::AddThread (const ThreadSP &thread_sp)
33{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000034 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Kuba Breckae4d48012014-09-05 19:13:15 +000035 m_threads.push_back (thread_sp);
36}
37
38void
Greg Clayton88f86b62016-06-10 23:23:34 +000039ThreadCollection::AddThreadSortedByIndexID (const ThreadSP &thread_sp)
40{
41 std::lock_guard<std::recursive_mutex> guard(GetMutex());
42 // Make sure we always keep the threads sorted by thread index ID
43 const uint32_t thread_index_id = thread_sp->GetIndexID();
44 if (m_threads.empty() || m_threads.back()->GetIndexID() < thread_index_id)
45 m_threads.push_back (thread_sp);
46 else
47 {
48 m_threads.insert(std::upper_bound(m_threads.begin(), m_threads.end(), thread_sp,
49 [] (const ThreadSP &lhs, const ThreadSP &rhs) -> bool
50 {
51 return lhs->GetIndexID() < rhs->GetIndexID();
52 }), thread_sp);
53 }
54}
55
56void
Kuba Breckae4d48012014-09-05 19:13:15 +000057ThreadCollection::InsertThread (const lldb::ThreadSP &thread_sp, uint32_t idx)
58{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000059 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Kuba Breckae4d48012014-09-05 19:13:15 +000060 if (idx < m_threads.size())
61 m_threads.insert(m_threads.begin() + idx, thread_sp);
62 else
63 m_threads.push_back (thread_sp);
64}
65
66uint32_t
67ThreadCollection::GetSize ()
68{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000069 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Kuba Breckae4d48012014-09-05 19:13:15 +000070 return m_threads.size();
71}
72
73ThreadSP
74ThreadCollection::GetThreadAtIndex (uint32_t idx)
75{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000076 std::lock_guard<std::recursive_mutex> guard(GetMutex());
Kuba Breckae4d48012014-09-05 19:13:15 +000077 ThreadSP thread_sp;
78 if (idx < m_threads.size())
79 thread_sp = m_threads[idx];
80 return thread_sp;
81}