Kuba Brecka | e4d4801 | 2014-09-05 19:13:15 +0000 | [diff] [blame] | 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 | |
| 13 | #include "lldb/Target/ThreadCollection.h" |
Greg Clayton | 88f86b6 | 2016-06-10 23:23:34 +0000 | [diff] [blame^] | 14 | #include "lldb/Target/Thread.h" |
Kuba Brecka | e4d4801 | 2014-09-05 19:13:15 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace lldb; |
| 17 | using namespace lldb_private; |
| 18 | |
| 19 | ThreadCollection::ThreadCollection() : |
| 20 | m_threads(), |
| 21 | m_mutex() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | ThreadCollection::ThreadCollection(collection threads) : |
| 26 | m_threads(threads), |
| 27 | m_mutex() |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | void |
| 32 | ThreadCollection::AddThread (const ThreadSP &thread_sp) |
| 33 | { |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 34 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
Kuba Brecka | e4d4801 | 2014-09-05 19:13:15 +0000 | [diff] [blame] | 35 | m_threads.push_back (thread_sp); |
| 36 | } |
| 37 | |
| 38 | void |
Greg Clayton | 88f86b6 | 2016-06-10 23:23:34 +0000 | [diff] [blame^] | 39 | ThreadCollection::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 | |
| 56 | void |
Kuba Brecka | e4d4801 | 2014-09-05 19:13:15 +0000 | [diff] [blame] | 57 | ThreadCollection::InsertThread (const lldb::ThreadSP &thread_sp, uint32_t idx) |
| 58 | { |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 59 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
Kuba Brecka | e4d4801 | 2014-09-05 19:13:15 +0000 | [diff] [blame] | 60 | 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 | |
| 66 | uint32_t |
| 67 | ThreadCollection::GetSize () |
| 68 | { |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 69 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
Kuba Brecka | e4d4801 | 2014-09-05 19:13:15 +0000 | [diff] [blame] | 70 | return m_threads.size(); |
| 71 | } |
| 72 | |
| 73 | ThreadSP |
| 74 | ThreadCollection::GetThreadAtIndex (uint32_t idx) |
| 75 | { |
Saleem Abdulrasool | bb19a13 | 2016-05-19 05:13:57 +0000 | [diff] [blame] | 76 | std::lock_guard<std::recursive_mutex> guard(GetMutex()); |
Kuba Brecka | e4d4801 | 2014-09-05 19:13:15 +0000 | [diff] [blame] | 77 | ThreadSP thread_sp; |
| 78 | if (idx < m_threads.size()) |
| 79 | thread_sp = m_threads[idx]; |
| 80 | return thread_sp; |
| 81 | } |