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/SBThread.h"
16 #include "lldb/API/SBQueueItem.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
36 m_thread_list_fetched(false),
38 m_pending_items_fetched(false)
42 QueueImpl (const lldb::QueueSP &queue_sp) :
45 m_thread_list_fetched(false),
47 m_pending_items_fetched(false)
49 m_queue_wp = queue_sp;
52 QueueImpl (const QueueImpl &rhs)
56 m_queue_wp = rhs.m_queue_wp;
57 m_threads = rhs.m_threads;
58 m_thread_list_fetched = rhs.m_thread_list_fetched;
59 m_pending_items = rhs.m_pending_items;
60 m_pending_items_fetched = rhs.m_pending_items_fetched;
70 return m_queue_wp.lock() != NULL;
77 m_thread_list_fetched = false;
79 m_pending_items_fetched = false;
80 m_pending_items.clear();
84 SetQueue (const lldb::QueueSP &queue_sp)
87 m_queue_wp = queue_sp;
93 lldb::queue_id_t result = LLDB_INVALID_QUEUE_ID;
94 lldb::QueueSP queue_sp = m_queue_wp.lock();
97 result = queue_sp->GetID();
99 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
101 log->Printf ("SBQueue(%p)::GetQueueID () => 0x%" PRIx64,
102 static_cast<const void*>(this), result);
109 uint32_t result = LLDB_INVALID_INDEX32;
110 lldb::QueueSP queue_sp = m_queue_wp.lock();
113 result = queue_sp->GetIndexID();
115 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
117 log->Printf ("SBQueueImpl(%p)::GetIndexID () => %d",
118 static_cast<const void*>(this), result);
125 const char *name = NULL;
126 lldb::QueueSP queue_sp = m_queue_wp.lock ();
129 name = queue_sp->GetName();
132 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
134 log->Printf ("SBQueueImpl(%p)::GetName () => %s",
135 static_cast<const void*>(this),
136 name ? name : "NULL");
144 if (m_thread_list_fetched == false)
146 lldb::QueueSP queue_sp = m_queue_wp.lock();
149 Process::StopLocker stop_locker;
150 if (stop_locker.TryLock (&queue_sp->GetProcess()->GetRunLock()))
152 const std::vector<ThreadSP> thread_list(queue_sp->GetThreads());
153 m_thread_list_fetched = true;
154 const uint32_t num_threads = thread_list.size();
155 for (uint32_t idx = 0; idx < num_threads; ++idx)
157 ThreadSP thread_sp = thread_list[idx];
158 if (thread_sp && thread_sp->IsValid())
160 m_threads.push_back (thread_sp);
171 if (m_pending_items_fetched == false)
173 QueueSP queue_sp = m_queue_wp.lock();
176 Process::StopLocker stop_locker;
177 if (stop_locker.TryLock (&queue_sp->GetProcess()->GetRunLock()))
179 const std::vector<QueueItemSP> queue_items(queue_sp->GetPendingItems());
180 m_pending_items_fetched = true;
181 const uint32_t num_pending_items = queue_items.size();
182 for (uint32_t idx = 0; idx < num_pending_items; ++idx)
184 QueueItemSP item = queue_items[idx];
185 if (item && item->IsValid())
187 m_pending_items.push_back (item);
201 if (m_thread_list_fetched)
203 result = m_threads.size();
209 GetThreadAtIndex (uint32_t idx)
214 QueueSP queue_sp = m_queue_wp.lock();
215 if (queue_sp && idx < m_threads.size())
217 ProcessSP process_sp = queue_sp->GetProcess();
220 ThreadSP thread_sp = m_threads[idx].lock();
223 sb_thread.SetThread (thread_sp);
231 GetNumPendingItems ()
235 QueueSP queue_sp = m_queue_wp.lock();
236 if (m_pending_items_fetched == false && queue_sp)
238 result = queue_sp->GetNumPendingWorkItems();
242 result = m_pending_items.size();
248 GetPendingItemAtIndex (uint32_t idx)
252 if (m_pending_items_fetched && idx < m_pending_items.size())
254 result.SetQueueItem (m_pending_items[idx]);
260 GetNumRunningItems ()
263 QueueSP queue_sp = m_queue_wp.lock();
265 result = queue_sp->GetNumRunningWorkItems();
273 QueueSP queue_sp = m_queue_wp.lock();
276 result.SetSP (queue_sp->GetProcess());
284 lldb::QueueKind kind = eQueueKindUnknown;
285 QueueSP queue_sp = m_queue_wp.lock();
287 kind = queue_sp->GetKind();
293 lldb::QueueWP m_queue_wp;
294 std::vector<lldb::ThreadWP> m_threads; // threads currently executing this queue's items
295 bool m_thread_list_fetched; // have we tried to fetch the threads list already?
296 std::vector<lldb::QueueItemSP> m_pending_items; // items currently enqueued
297 bool m_pending_items_fetched; // have we tried to fetch the item list already?
302 SBQueue::SBQueue () :
303 m_opaque_sp (new QueueImpl())
307 SBQueue::SBQueue (const QueueSP& queue_sp) :
308 m_opaque_sp (new QueueImpl (queue_sp))
312 SBQueue::SBQueue (const SBQueue &rhs)
317 m_opaque_sp = rhs.m_opaque_sp;
320 const lldb::SBQueue &
321 SBQueue::operator = (const lldb::SBQueue &rhs)
323 m_opaque_sp = rhs.m_opaque_sp;
332 SBQueue::IsValid() const
334 bool is_valid = m_opaque_sp->IsValid ();
335 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
337 log->Printf("SBQueue(0x%" PRIx64 ")::IsValid() == %s", m_opaque_sp->GetQueueID(),
338 is_valid ? "true" : "false");
346 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
348 log->Printf("SBQueue(0x%" PRIx64 ")::Clear()", m_opaque_sp->GetQueueID());
349 m_opaque_sp->Clear();
354 SBQueue::SetQueue (const QueueSP& queue_sp)
356 m_opaque_sp->SetQueue (queue_sp);
360 SBQueue::GetQueueID () const
362 lldb::queue_id_t qid = m_opaque_sp->GetQueueID ();
363 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
365 log->Printf("SBQueue(0x%" PRIx64 ")::GetQueueID() == 0x%" PRIx64, m_opaque_sp->GetQueueID(), (uint64_t) qid);
370 SBQueue::GetIndexID () const
372 uint32_t index_id = m_opaque_sp->GetIndexID ();
373 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
375 log->Printf("SBQueue(0x%" PRIx64 ")::GetIndexID() == 0x%" PRIx32, m_opaque_sp->GetQueueID(), index_id);
380 SBQueue::GetName () const
382 const char *name = m_opaque_sp->GetName ();
383 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
385 log->Printf("SBQueue(0x%" PRIx64 ")::GetName() == %s", m_opaque_sp->GetQueueID(),
391 SBQueue::GetNumThreads ()
393 uint32_t numthreads = m_opaque_sp->GetNumThreads ();
394 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
396 log->Printf("SBQueue(0x%" PRIx64 ")::GetNumThreads() == %d", m_opaque_sp->GetQueueID(), numthreads);
401 SBQueue::GetThreadAtIndex (uint32_t idx)
403 SBThread th = m_opaque_sp->GetThreadAtIndex (idx);
404 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
406 log->Printf("SBQueue(0x%" PRIx64 ")::GetThreadAtIndex(%d)", m_opaque_sp->GetQueueID(), idx);
412 SBQueue::GetNumPendingItems ()
414 uint32_t pending_items = m_opaque_sp->GetNumPendingItems ();
415 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
417 log->Printf("SBQueue(0x%" PRIx64 ")::GetNumPendingItems() == %d", m_opaque_sp->GetQueueID(), pending_items);
418 return pending_items;
422 SBQueue::GetPendingItemAtIndex (uint32_t idx)
424 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
426 log->Printf("SBQueue(0x%" PRIx64 ")::GetPendingItemAtIndex(%d)", m_opaque_sp->GetQueueID(), idx);
427 return m_opaque_sp->GetPendingItemAtIndex (idx);
431 SBQueue::GetNumRunningItems ()
433 uint32_t running_items = m_opaque_sp->GetNumRunningItems ();
434 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
436 log->Printf("SBQueue(0x%" PRIx64 ")::GetNumRunningItems() == %d", m_opaque_sp->GetQueueID(), running_items);
437 return running_items;
441 SBQueue::GetProcess ()
443 return m_opaque_sp->GetProcess();
449 return m_opaque_sp->GetKind();