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 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/API/SBQueue.h"
14 #include "lldb/API/SBProcess.h"
15 #include "lldb/API/SBThread.h"
16 #include "lldb/Core/Log.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Target/Queue.h"
19 #include "lldb/Target/QueueItem.h"
20 #include "lldb/Target/Thread.h"
23 using namespace lldb_private;
25 namespace lldb_private
34 m_thread_list_fetched(false),
36 m_pending_items_fetched(false)
40 QueueImpl (const lldb::QueueSP &queue_sp) :
43 m_thread_list_fetched(false),
45 m_pending_items_fetched(false)
47 m_queue_wp = queue_sp;
50 QueueImpl (const QueueImpl &rhs)
54 m_queue_wp = rhs.m_queue_wp;
55 m_threads = rhs.m_threads;
56 m_thread_list_fetched = rhs.m_thread_list_fetched;
57 m_pending_items = rhs.m_pending_items;
58 m_pending_items_fetched = rhs.m_pending_items_fetched;
68 return m_queue_wp.lock() != NULL;
75 m_thread_list_fetched = false;
77 m_pending_items_fetched = false;
78 m_pending_items.clear();
82 SetQueue (const lldb::QueueSP &queue_sp)
85 m_queue_wp = queue_sp;
91 lldb::queue_id_t result = LLDB_INVALID_QUEUE_ID;
92 lldb::QueueSP queue_sp = m_queue_wp.lock();
95 result = queue_sp->GetID();
97 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
99 log->Printf ("SBQueue(%p)::GetQueueID () => 0x%" PRIx64, this, result);
106 uint32_t result = LLDB_INVALID_INDEX32;
107 lldb::QueueSP queue_sp = m_queue_wp.lock();
110 result = queue_sp->GetIndexID();
112 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
114 log->Printf ("SBQueueImpl(%p)::GetIndexID () => %d", this, result);
121 const char *name = NULL;
122 lldb::QueueSP queue_sp = m_queue_wp.lock ();
125 name = queue_sp->GetName();
128 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
130 log->Printf ("SBQueueImpl(%p)::GetName () => %s", this, name ? name : "NULL");
138 if (m_thread_list_fetched == false)
140 lldb::QueueSP queue_sp = m_queue_wp.lock();
143 Process::StopLocker stop_locker;
144 if (stop_locker.TryLock (&queue_sp->GetProcess()->GetRunLock()))
146 const std::vector<ThreadSP> thread_list(queue_sp->GetThreads());
147 m_thread_list_fetched = true;
148 const uint32_t num_threads = thread_list.size();
149 for (uint32_t idx = 0; idx < num_threads; ++idx)
151 ThreadSP thread_sp = thread_list[idx];
152 if (thread_sp && thread_sp->IsValid())
154 m_threads.push_back (thread_sp);
165 if (m_pending_items_fetched == false)
167 QueueSP queue_sp = m_queue_wp.lock();
170 Process::StopLocker stop_locker;
171 if (stop_locker.TryLock (&queue_sp->GetProcess()->GetRunLock()))
173 const std::vector<QueueItemSP> queue_items(queue_sp->GetPendingItems());
174 m_pending_items_fetched = true;
175 const uint32_t num_pending_items = queue_items.size();
176 for (uint32_t idx = 0; idx < num_pending_items; ++idx)
178 QueueItemSP item = queue_items[idx];
179 if (item && item->IsValid())
181 m_pending_items.push_back (item);
195 if (m_thread_list_fetched)
197 result = m_threads.size();
203 GetThreadAtIndex (uint32_t idx)
208 QueueSP queue_sp = m_queue_wp.lock();
209 if (queue_sp && idx < m_threads.size())
211 ProcessSP process_sp = queue_sp->GetProcess();
214 ThreadSP thread_sp = m_threads[idx].lock();
217 sb_thread.SetThread (thread_sp);
226 GetNumPendingItems ()
231 if (m_pending_items_fetched)
233 result = m_pending_items.size();
239 GetPendingItemAtIndex (uint32_t idx)
243 if (m_pending_items_fetched && idx < m_pending_items.size())
245 result.SetQueueItem (m_pending_items[idx]);
254 QueueSP queue_sp = m_queue_wp.lock();
257 result.SetSP (queue_sp->GetProcess());
263 lldb::QueueWP m_queue_wp;
264 std::vector<lldb::ThreadWP> m_threads; // threads currently executing this queue's items
265 bool m_thread_list_fetched; // have we tried to fetch the threads list already?
266 std::vector<lldb::QueueItemSP> m_pending_items; // items currently enqueued
267 bool m_pending_items_fetched; // have we tried to fetch the item list already?
272 SBQueue::SBQueue () :
273 m_opaque_sp (new QueueImpl())
277 SBQueue::SBQueue (const QueueSP& queue_sp) :
278 m_opaque_sp (new QueueImpl (queue_sp))
282 SBQueue::SBQueue (const SBQueue &rhs)
287 m_opaque_sp = rhs.m_opaque_sp;
290 const lldb::SBQueue &
291 SBQueue::operator = (const lldb::SBQueue &rhs)
293 m_opaque_sp = rhs.m_opaque_sp;
302 SBQueue::IsValid() const
304 return m_opaque_sp->IsValid();
311 m_opaque_sp->Clear();
316 SBQueue::SetQueue (const QueueSP& queue_sp)
318 m_opaque_sp->SetQueue (queue_sp);
322 SBQueue::GetQueueID () const
324 return m_opaque_sp->GetQueueID ();
328 SBQueue::GetIndexID () const
330 return m_opaque_sp->GetIndexID ();
334 SBQueue::GetName () const
336 return m_opaque_sp->GetName ();
340 SBQueue::GetNumThreads ()
342 return m_opaque_sp->GetNumThreads ();
346 SBQueue::GetThreadAtIndex (uint32_t idx)
348 return m_opaque_sp->GetThreadAtIndex (idx);
353 SBQueue::GetNumPendingItems ()
355 return m_opaque_sp->GetNumPendingItems ();
359 SBQueue::GetPendingItemAtIndex (uint32_t idx)
361 return m_opaque_sp->GetPendingItemAtIndex (idx);
365 SBQueue::GetProcess ()
367 return m_opaque_sp->GetProcess();