Path: blob/main/contrib/llvm-project/lldb/source/API/SBQueue.cpp
39587 views
//===-- SBQueue.cpp -------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <cinttypes>910#include "lldb/API/SBQueue.h"11#include "lldb/Utility/Instrumentation.h"1213#include "lldb/API/SBProcess.h"14#include "lldb/API/SBQueueItem.h"15#include "lldb/API/SBThread.h"1617#include "lldb/Target/Process.h"18#include "lldb/Target/Queue.h"19#include "lldb/Target/QueueItem.h"20#include "lldb/Target/Thread.h"2122using namespace lldb;23using namespace lldb_private;2425namespace lldb_private {2627class QueueImpl {28public:29QueueImpl() = default;3031QueueImpl(const lldb::QueueSP &queue_sp) { m_queue_wp = queue_sp; }3233QueueImpl(const QueueImpl &rhs) {34if (&rhs == this)35return;36m_queue_wp = rhs.m_queue_wp;37m_threads = rhs.m_threads;38m_thread_list_fetched = rhs.m_thread_list_fetched;39m_pending_items = rhs.m_pending_items;40m_pending_items_fetched = rhs.m_pending_items_fetched;41}4243~QueueImpl() = default;4445bool IsValid() { return m_queue_wp.lock() != nullptr; }4647void Clear() {48m_queue_wp.reset();49m_thread_list_fetched = false;50m_threads.clear();51m_pending_items_fetched = false;52m_pending_items.clear();53}5455void SetQueue(const lldb::QueueSP &queue_sp) {56Clear();57m_queue_wp = queue_sp;58}5960lldb::queue_id_t GetQueueID() const {61lldb::queue_id_t result = LLDB_INVALID_QUEUE_ID;62lldb::QueueSP queue_sp = m_queue_wp.lock();63if (queue_sp) {64result = queue_sp->GetID();65}66return result;67}6869uint32_t GetIndexID() const {70uint32_t result = LLDB_INVALID_INDEX32;71lldb::QueueSP queue_sp = m_queue_wp.lock();72if (queue_sp) {73result = queue_sp->GetIndexID();74}75return result;76}7778const char *GetName() const {79lldb::QueueSP queue_sp = m_queue_wp.lock();80if (!queue_sp)81return nullptr;82return ConstString(queue_sp->GetName()).GetCString();83}8485void FetchThreads() {86if (!m_thread_list_fetched) {87lldb::QueueSP queue_sp = m_queue_wp.lock();88if (queue_sp) {89Process::StopLocker stop_locker;90if (stop_locker.TryLock(&queue_sp->GetProcess()->GetRunLock())) {91const std::vector<ThreadSP> thread_list(queue_sp->GetThreads());92m_thread_list_fetched = true;93const uint32_t num_threads = thread_list.size();94for (uint32_t idx = 0; idx < num_threads; ++idx) {95ThreadSP thread_sp = thread_list[idx];96if (thread_sp && thread_sp->IsValid()) {97m_threads.push_back(thread_sp);98}99}100}101}102}103}104105void FetchItems() {106if (!m_pending_items_fetched) {107QueueSP queue_sp = m_queue_wp.lock();108if (queue_sp) {109Process::StopLocker stop_locker;110if (stop_locker.TryLock(&queue_sp->GetProcess()->GetRunLock())) {111const std::vector<QueueItemSP> queue_items(112queue_sp->GetPendingItems());113m_pending_items_fetched = true;114const uint32_t num_pending_items = queue_items.size();115for (uint32_t idx = 0; idx < num_pending_items; ++idx) {116QueueItemSP item = queue_items[idx];117if (item && item->IsValid()) {118m_pending_items.push_back(item);119}120}121}122}123}124}125126uint32_t GetNumThreads() {127uint32_t result = 0;128129FetchThreads();130if (m_thread_list_fetched) {131result = m_threads.size();132}133return result;134}135136lldb::SBThread GetThreadAtIndex(uint32_t idx) {137FetchThreads();138139SBThread sb_thread;140QueueSP queue_sp = m_queue_wp.lock();141if (queue_sp && idx < m_threads.size()) {142ProcessSP process_sp = queue_sp->GetProcess();143if (process_sp) {144ThreadSP thread_sp = m_threads[idx].lock();145if (thread_sp) {146sb_thread.SetThread(thread_sp);147}148}149}150return sb_thread;151}152153uint32_t GetNumPendingItems() {154uint32_t result = 0;155156QueueSP queue_sp = m_queue_wp.lock();157if (!m_pending_items_fetched && queue_sp) {158result = queue_sp->GetNumPendingWorkItems();159} else {160result = m_pending_items.size();161}162return result;163}164165lldb::SBQueueItem GetPendingItemAtIndex(uint32_t idx) {166SBQueueItem result;167FetchItems();168if (m_pending_items_fetched && idx < m_pending_items.size()) {169result.SetQueueItem(m_pending_items[idx]);170}171return result;172}173174uint32_t GetNumRunningItems() {175uint32_t result = 0;176QueueSP queue_sp = m_queue_wp.lock();177if (queue_sp)178result = queue_sp->GetNumRunningWorkItems();179return result;180}181182lldb::SBProcess GetProcess() {183SBProcess result;184QueueSP queue_sp = m_queue_wp.lock();185if (queue_sp) {186result.SetSP(queue_sp->GetProcess());187}188return result;189}190191lldb::QueueKind GetKind() {192lldb::QueueKind kind = eQueueKindUnknown;193QueueSP queue_sp = m_queue_wp.lock();194if (queue_sp)195kind = queue_sp->GetKind();196197return kind;198}199200private:201lldb::QueueWP m_queue_wp;202std::vector<lldb::ThreadWP>203m_threads; // threads currently executing this queue's items204bool m_thread_list_fetched =205false; // have we tried to fetch the threads list already?206std::vector<lldb::QueueItemSP> m_pending_items; // items currently enqueued207bool m_pending_items_fetched =208false; // have we tried to fetch the item list already?209};210}211212SBQueue::SBQueue() : m_opaque_sp(new QueueImpl()) { LLDB_INSTRUMENT_VA(this); }213214SBQueue::SBQueue(const QueueSP &queue_sp)215: m_opaque_sp(new QueueImpl(queue_sp)) {216LLDB_INSTRUMENT_VA(this, queue_sp);217}218219SBQueue::SBQueue(const SBQueue &rhs) {220LLDB_INSTRUMENT_VA(this, rhs);221222if (&rhs == this)223return;224225m_opaque_sp = rhs.m_opaque_sp;226}227228const lldb::SBQueue &SBQueue::operator=(const lldb::SBQueue &rhs) {229LLDB_INSTRUMENT_VA(this, rhs);230231m_opaque_sp = rhs.m_opaque_sp;232return *this;233}234235SBQueue::~SBQueue() = default;236237bool SBQueue::IsValid() const {238LLDB_INSTRUMENT_VA(this);239return this->operator bool();240}241SBQueue::operator bool() const {242LLDB_INSTRUMENT_VA(this);243244return m_opaque_sp->IsValid();245}246247void SBQueue::Clear() {248LLDB_INSTRUMENT_VA(this);249250m_opaque_sp->Clear();251}252253void SBQueue::SetQueue(const QueueSP &queue_sp) {254m_opaque_sp->SetQueue(queue_sp);255}256257lldb::queue_id_t SBQueue::GetQueueID() const {258LLDB_INSTRUMENT_VA(this);259260return m_opaque_sp->GetQueueID();261}262263uint32_t SBQueue::GetIndexID() const {264LLDB_INSTRUMENT_VA(this);265266uint32_t index_id = m_opaque_sp->GetIndexID();267return index_id;268}269270const char *SBQueue::GetName() const {271LLDB_INSTRUMENT_VA(this);272273return m_opaque_sp->GetName();274}275276uint32_t SBQueue::GetNumThreads() {277LLDB_INSTRUMENT_VA(this);278279return m_opaque_sp->GetNumThreads();280}281282SBThread SBQueue::GetThreadAtIndex(uint32_t idx) {283LLDB_INSTRUMENT_VA(this, idx);284285SBThread th = m_opaque_sp->GetThreadAtIndex(idx);286return th;287}288289uint32_t SBQueue::GetNumPendingItems() {290LLDB_INSTRUMENT_VA(this);291292return m_opaque_sp->GetNumPendingItems();293}294295SBQueueItem SBQueue::GetPendingItemAtIndex(uint32_t idx) {296LLDB_INSTRUMENT_VA(this, idx);297298return m_opaque_sp->GetPendingItemAtIndex(idx);299}300301uint32_t SBQueue::GetNumRunningItems() {302LLDB_INSTRUMENT_VA(this);303304return m_opaque_sp->GetNumRunningItems();305}306307SBProcess SBQueue::GetProcess() {308LLDB_INSTRUMENT_VA(this);309310return m_opaque_sp->GetProcess();311}312313lldb::QueueKind SBQueue::GetKind() {314LLDB_INSTRUMENT_VA(this);315316return m_opaque_sp->GetKind();317}318319320