blob: cf3c1e2429999524c78ab2d1c724f343611dfc78 [file] [log] [blame]
Kuba Breckae4d48012014-09-05 19:13:15 +00001//===-- ThreadCollection.cpp ------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Kuba Breckae4d48012014-09-05 19:13:15 +00006//
7//===----------------------------------------------------------------------===//
8#include <stdlib.h>
9
10#include <algorithm>
Greg Clayton940f4252016-06-10 23:53:06 +000011#include <mutex>
Kuba Breckae4d48012014-09-05 19:13:15 +000012
Greg Clayton88f86b62016-06-10 23:23:34 +000013#include "lldb/Target/Thread.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000014#include "lldb/Target/ThreadCollection.h"
Kuba Breckae4d48012014-09-05 19:13:15 +000015
16using namespace lldb;
17using namespace lldb_private;
18
Kate Stoneb9c1b512016-09-06 20:57:50 +000019ThreadCollection::ThreadCollection() : m_threads(), m_mutex() {}
20
21ThreadCollection::ThreadCollection(collection threads)
22 : m_threads(threads), m_mutex() {}
23
24void ThreadCollection::AddThread(const ThreadSP &thread_sp) {
25 std::lock_guard<std::recursive_mutex> guard(GetMutex());
26 m_threads.push_back(thread_sp);
Kuba Breckae4d48012014-09-05 19:13:15 +000027}
28
Kate Stoneb9c1b512016-09-06 20:57:50 +000029void ThreadCollection::AddThreadSortedByIndexID(const ThreadSP &thread_sp) {
30 std::lock_guard<std::recursive_mutex> guard(GetMutex());
31 // Make sure we always keep the threads sorted by thread index ID
32 const uint32_t thread_index_id = thread_sp->GetIndexID();
33 if (m_threads.empty() || m_threads.back()->GetIndexID() < thread_index_id)
34 m_threads.push_back(thread_sp);
35 else {
36 m_threads.insert(
37 std::upper_bound(m_threads.begin(), m_threads.end(), thread_sp,
38 [](const ThreadSP &lhs, const ThreadSP &rhs) -> bool {
39 return lhs->GetIndexID() < rhs->GetIndexID();
40 }),
41 thread_sp);
42 }
Kuba Breckae4d48012014-09-05 19:13:15 +000043}
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045void ThreadCollection::InsertThread(const lldb::ThreadSP &thread_sp,
46 uint32_t idx) {
47 std::lock_guard<std::recursive_mutex> guard(GetMutex());
48 if (idx < m_threads.size())
49 m_threads.insert(m_threads.begin() + idx, thread_sp);
50 else
51 m_threads.push_back(thread_sp);
Kuba Breckae4d48012014-09-05 19:13:15 +000052}
53
Kate Stoneb9c1b512016-09-06 20:57:50 +000054uint32_t ThreadCollection::GetSize() {
55 std::lock_guard<std::recursive_mutex> guard(GetMutex());
56 return m_threads.size();
Greg Clayton88f86b62016-06-10 23:23:34 +000057}
58
Kate Stoneb9c1b512016-09-06 20:57:50 +000059ThreadSP ThreadCollection::GetThreadAtIndex(uint32_t idx) {
60 std::lock_guard<std::recursive_mutex> guard(GetMutex());
61 ThreadSP thread_sp;
62 if (idx < m_threads.size())
63 thread_sp = m_threads[idx];
64 return thread_sp;
Kuba Breckae4d48012014-09-05 19:13:15 +000065}