1 //===-- SBQueue.cpp ---------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "lldb/API/SBQueue.h"
14 #include "lldb/API/SBProcess.h"
15 #include "lldb/API/SBQueueItem.h"
16 #include "lldb/API/SBThread.h"
18 #include "lldb/Core/Log.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/Queue.h"
21 #include "lldb/Target/QueueItem.h"
22 #include "lldb/Target/Thread.h"
25 using namespace lldb_private;
27 namespace lldb_private {
32 : m_queue_wp(), m_threads(), m_thread_list_fetched(false),
33 m_pending_items(), m_pending_items_fetched(false) {}
35 QueueImpl(const lldb::QueueSP &queue_sp)
36 : m_queue_wp(), m_threads(), m_thread_list_fetched(false),
37 m_pending_items(), m_pending_items_fetched(false) {
38 m_queue_wp = queue_sp;
41 QueueImpl(const QueueImpl &rhs) {
44 m_queue_wp = rhs.m_queue_wp;
45 m_threads = rhs.m_threads;
46 m_thread_list_fetched = rhs.m_thread_list_fetched;
47 m_pending_items = rhs.m_pending_items;
48 m_pending_items_fetched = rhs.m_pending_items_fetched;
53 bool IsValid() { return m_queue_wp.lock() != NULL; }
57 m_thread_list_fetched = false;
59 m_pending_items_fetched = false;
60 m_pending_items.clear();
63 void SetQueue(const lldb::QueueSP &queue_sp) {
65 m_queue_wp = queue_sp;
68 lldb::queue_id_t GetQueueID() const {
69 lldb::queue_id_t result = LLDB_INVALID_QUEUE_ID;
70 lldb::QueueSP queue_sp = m_queue_wp.lock();
72 result = queue_sp->GetID();
74 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
76 log->Printf("SBQueue(%p)::GetQueueID () => 0x%" PRIx64,
77 static_cast<const void *>(this), result);
81 uint32_t GetIndexID() const {
82 uint32_t result = LLDB_INVALID_INDEX32;
83 lldb::QueueSP queue_sp = m_queue_wp.lock();
85 result = queue_sp->GetIndexID();
87 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
89 log->Printf("SBQueueImpl(%p)::GetIndexID () => %d",
90 static_cast<const void *>(this), result);
94 const char *GetName() const {
95 const char *name = NULL;
96 lldb::QueueSP queue_sp = m_queue_wp.lock();
98 name = queue_sp->GetName();
101 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
103 log->Printf("SBQueueImpl(%p)::GetName () => %s",
104 static_cast<const void *>(this), name ? name : "NULL");
109 void FetchThreads() {
110 if (m_thread_list_fetched == false) {
111 lldb::QueueSP queue_sp = m_queue_wp.lock();
113 Process::StopLocker stop_locker;
114 if (stop_locker.TryLock(&queue_sp->GetProcess()->GetRunLock())) {
115 const std::vector<ThreadSP> thread_list(queue_sp->GetThreads());
116 m_thread_list_fetched = true;
117 const uint32_t num_threads = thread_list.size();
118 for (uint32_t idx = 0; idx < num_threads; ++idx) {
119 ThreadSP thread_sp = thread_list[idx];
120 if (thread_sp && thread_sp->IsValid()) {
121 m_threads.push_back(thread_sp);
130 if (m_pending_items_fetched == false) {
131 QueueSP queue_sp = m_queue_wp.lock();
133 Process::StopLocker stop_locker;
134 if (stop_locker.TryLock(&queue_sp->GetProcess()->GetRunLock())) {
135 const std::vector<QueueItemSP> queue_items(
136 queue_sp->GetPendingItems());
137 m_pending_items_fetched = true;
138 const uint32_t num_pending_items = queue_items.size();
139 for (uint32_t idx = 0; idx < num_pending_items; ++idx) {
140 QueueItemSP item = queue_items[idx];
141 if (item && item->IsValid()) {
142 m_pending_items.push_back(item);
150 uint32_t GetNumThreads() {
154 if (m_thread_list_fetched) {
155 result = m_threads.size();
160 lldb::SBThread GetThreadAtIndex(uint32_t idx) {
164 QueueSP queue_sp = m_queue_wp.lock();
165 if (queue_sp && idx < m_threads.size()) {
166 ProcessSP process_sp = queue_sp->GetProcess();
168 ThreadSP thread_sp = m_threads[idx].lock();
170 sb_thread.SetThread(thread_sp);
177 uint32_t GetNumPendingItems() {
180 QueueSP queue_sp = m_queue_wp.lock();
181 if (m_pending_items_fetched == false && queue_sp) {
182 result = queue_sp->GetNumPendingWorkItems();
184 result = m_pending_items.size();
189 lldb::SBQueueItem GetPendingItemAtIndex(uint32_t idx) {
192 if (m_pending_items_fetched && idx < m_pending_items.size()) {
193 result.SetQueueItem(m_pending_items[idx]);
198 uint32_t GetNumRunningItems() {
200 QueueSP queue_sp = m_queue_wp.lock();
202 result = queue_sp->GetNumRunningWorkItems();
206 lldb::SBProcess GetProcess() {
208 QueueSP queue_sp = m_queue_wp.lock();
210 result.SetSP(queue_sp->GetProcess());
215 lldb::QueueKind GetKind() {
216 lldb::QueueKind kind = eQueueKindUnknown;
217 QueueSP queue_sp = m_queue_wp.lock();
219 kind = queue_sp->GetKind();
225 lldb::QueueWP m_queue_wp;
226 std::vector<lldb::ThreadWP>
227 m_threads; // threads currently executing this queue's items
229 m_thread_list_fetched; // have we tried to fetch the threads list already?
230 std::vector<lldb::QueueItemSP> m_pending_items; // items currently enqueued
231 bool m_pending_items_fetched; // have we tried to fetch the item list already?
235 SBQueue::SBQueue() : m_opaque_sp(new QueueImpl()) {}
237 SBQueue::SBQueue(const QueueSP &queue_sp)
238 : m_opaque_sp(new QueueImpl(queue_sp)) {}
240 SBQueue::SBQueue(const SBQueue &rhs) {
244 m_opaque_sp = rhs.m_opaque_sp;
247 const lldb::SBQueue &SBQueue::operator=(const lldb::SBQueue &rhs) {
248 m_opaque_sp = rhs.m_opaque_sp;
252 SBQueue::~SBQueue() {}
254 bool SBQueue::IsValid() const {
255 bool is_valid = m_opaque_sp->IsValid();
256 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
258 log->Printf("SBQueue(0x%" PRIx64 ")::IsValid() == %s",
259 m_opaque_sp->GetQueueID(), is_valid ? "true" : "false");
263 void SBQueue::Clear() {
264 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
266 log->Printf("SBQueue(0x%" PRIx64 ")::Clear()", m_opaque_sp->GetQueueID());
267 m_opaque_sp->Clear();
270 void SBQueue::SetQueue(const QueueSP &queue_sp) {
271 m_opaque_sp->SetQueue(queue_sp);
274 lldb::queue_id_t SBQueue::GetQueueID() const {
275 lldb::queue_id_t qid = m_opaque_sp->GetQueueID();
276 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
278 log->Printf("SBQueue(0x%" PRIx64 ")::GetQueueID() == 0x%" PRIx64,
279 m_opaque_sp->GetQueueID(), (uint64_t)qid);
283 uint32_t SBQueue::GetIndexID() const {
284 uint32_t index_id = m_opaque_sp->GetIndexID();
285 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
287 log->Printf("SBQueue(0x%" PRIx64 ")::GetIndexID() == 0x%" PRIx32,
288 m_opaque_sp->GetQueueID(), index_id);
292 const char *SBQueue::GetName() const {
293 const char *name = m_opaque_sp->GetName();
294 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
296 log->Printf("SBQueue(0x%" PRIx64 ")::GetName() == %s",
297 m_opaque_sp->GetQueueID(), name ? name : "");
301 uint32_t SBQueue::GetNumThreads() {
302 uint32_t numthreads = m_opaque_sp->GetNumThreads();
303 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
305 log->Printf("SBQueue(0x%" PRIx64 ")::GetNumThreads() == %d",
306 m_opaque_sp->GetQueueID(), numthreads);
310 SBThread SBQueue::GetThreadAtIndex(uint32_t idx) {
311 SBThread th = m_opaque_sp->GetThreadAtIndex(idx);
312 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
314 log->Printf("SBQueue(0x%" PRIx64 ")::GetThreadAtIndex(%d)",
315 m_opaque_sp->GetQueueID(), idx);
319 uint32_t SBQueue::GetNumPendingItems() {
320 uint32_t pending_items = m_opaque_sp->GetNumPendingItems();
321 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
323 log->Printf("SBQueue(0x%" PRIx64 ")::GetNumPendingItems() == %d",
324 m_opaque_sp->GetQueueID(), pending_items);
325 return pending_items;
328 SBQueueItem SBQueue::GetPendingItemAtIndex(uint32_t idx) {
329 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
331 log->Printf("SBQueue(0x%" PRIx64 ")::GetPendingItemAtIndex(%d)",
332 m_opaque_sp->GetQueueID(), idx);
333 return m_opaque_sp->GetPendingItemAtIndex(idx);
336 uint32_t SBQueue::GetNumRunningItems() {
337 uint32_t running_items = m_opaque_sp->GetNumRunningItems();
338 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
340 log->Printf("SBQueue(0x%" PRIx64 ")::GetNumRunningItems() == %d",
341 m_opaque_sp->GetQueueID(), running_items);
342 return running_items;
345 SBProcess SBQueue::GetProcess() { return m_opaque_sp->GetProcess(); }
347 lldb::QueueKind SBQueue::GetKind() { return m_opaque_sp->GetKind(); }