blob: 58e3fcc495ffe8169f0406105e2959dcc7ca8952 [file] [log] [blame]
Jason Molenda5e8dce42013-12-13 00:29:16 +00001//===-- SBQueue.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/lldb-python.h"
11
12#include "lldb/API/SBQueue.h"
13
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"
21
22using namespace lldb;
23using namespace lldb_private;
24
25//----------------------------------------------------------------------
26// Constructors
27//----------------------------------------------------------------------
28SBQueue::SBQueue () :
29 m_queue_wp(),
30 m_threads(),
31 m_thread_list_fetched(false),
32 m_items(),
33 m_queue_items_fetched(false)
34{
35}
36
37SBQueue::SBQueue (const QueueSP& queue_sp) :
38 m_queue_wp(queue_sp),
39 m_threads(),
40 m_thread_list_fetched(false),
41 m_items(),
42 m_queue_items_fetched(false)
43{
44}
45
46//----------------------------------------------------------------------
47// Destructor
48//----------------------------------------------------------------------
49SBQueue::~SBQueue()
50{
51 m_threads.clear();
52 m_items.clear();
53}
54
55bool
56SBQueue::IsValid() const
57{
58 QueueSP queue_sp = m_queue_wp.lock();
59 return queue_sp.get() != NULL;
60}
61
62
63void
64SBQueue::Clear ()
65{
66 m_queue_wp.reset();
67 m_thread_list_fetched = false;
68 m_threads.clear();
69 m_queue_items_fetched = false;
70 m_items.clear();
71}
72
73
74void
75SBQueue::SetQueue (const QueueSP& queue_sp)
76{
77 m_queue_wp = queue_sp;
78 m_thread_list_fetched = false;
79 m_threads.clear();
80 m_queue_items_fetched = false;
81 m_items.clear();
82}
83
84lldb::queue_id_t
85SBQueue::GetQueueID () const
86{
87 queue_id_t result = LLDB_INVALID_QUEUE_ID;
88 QueueSP queue_sp = m_queue_wp.lock();
89 if (queue_sp)
90 {
91 result = queue_sp->GetID();
92 }
93 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
94 if (log)
95 log->Printf ("SBQueue(%p)::GetQueueID () => 0x%" PRIx64, this, result);
96 return result;
97}
98
99uint32_t
100SBQueue::GetIndexID () const
101{
102 uint32_t result = LLDB_INVALID_INDEX32;
103 QueueSP queue_sp = m_queue_wp.lock();
104 if (queue_sp)
105 {
106 result = queue_sp->GetIndexID();
107 }
108 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
109 if (log)
110 log->Printf ("SBQueue(%p)::GetIndexID () => %d", this, result);
111 return result;
112}
113
114const char *
115SBQueue::GetName () const
116{
117 const char *name = NULL;
118 QueueSP queue_sp = m_queue_wp.lock ();
119 if (queue_sp.get())
120 {
121 name = queue_sp->GetName();
122 }
123
124 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
125 if (log)
126 log->Printf ("SBQueue(%p)::GetName () => %s", this, name ? name : "NULL");
127
128 return name;
129}
130
131void
132SBQueue::FetchThreads ()
133{
134 if (m_thread_list_fetched == false)
135 {
136 QueueSP queue_sp = m_queue_wp.lock();
137 if (queue_sp)
138 {
139 Process::StopLocker stop_locker;
140 if (stop_locker.TryLock (&queue_sp->GetProcess()->GetRunLock()))
141 {
142 const std::vector<ThreadSP> thread_list(queue_sp->GetThreads());
143 m_thread_list_fetched = true;
144 const uint32_t num_threads = thread_list.size();
145 for (uint32_t idx = 0; idx < num_threads; ++idx)
146 {
147 ThreadSP thread_sp = thread_list[idx];
148 if (thread_sp && thread_sp->IsValid())
149 {
150 m_threads.push_back (thread_sp);
151 }
152 }
153 }
154 }
155 }
156}
157
158void
159SBQueue::FetchItems ()
160{
161 if (m_queue_items_fetched == false)
162 {
163 QueueSP queue_sp = m_queue_wp.lock();
164 if (queue_sp)
165 {
166 Process::StopLocker stop_locker;
167 if (stop_locker.TryLock (&queue_sp->GetProcess()->GetRunLock()))
168 {
169 const std::vector<QueueItemSP> queue_items(queue_sp->GetItems());
170 m_queue_items_fetched = true;
171 const uint32_t num_items = queue_items.size();
172 for (uint32_t idx = 0; idx < num_items; ++idx)
173 {
174 QueueItemSP item = queue_items[idx];
175 if (item && item->IsValid())
176 {
177 m_items.push_back (item);
178 }
179 }
180 }
181 }
182 }
183}
184
185uint32_t
186SBQueue::GetNumThreads ()
187{
188 uint32_t result = 0;
189
190 FetchThreads();
191 if (m_thread_list_fetched)
192 {
193 result = m_threads.size();
194 }
195 return result;
196}
197
198SBThread
199SBQueue::GetThreadAtIndex (uint32_t idx)
200{
201 FetchThreads();
202
203 SBThread sb_thread;
204 QueueSP queue_sp = m_queue_wp.lock();
205 if (queue_sp && idx < m_threads.size())
206 {
207 ProcessSP process_sp = queue_sp->GetProcess();
208 if (process_sp)
209 {
210 ThreadSP thread_sp = m_threads[idx].lock();
211 if (thread_sp)
212 {
213 sb_thread.SetThread (thread_sp);
214 }
215 }
216 }
217 return sb_thread;
218}
219
220
221uint32_t
222SBQueue::GetNumItems ()
223{
224 uint32_t result = 0;
225 FetchItems();
226
227 if (m_queue_items_fetched)
228 {
229 result = m_items.size();
230 }
231 return result;
232}
233
234SBQueueItem
235SBQueue::GetItemAtIndex (uint32_t idx)
236{
237 SBQueueItem result;
238 FetchItems();
239 if (m_queue_items_fetched && idx < m_items.size())
240 {
241 result.SetQueueItem (m_items[idx]);
242 }
243 return result;
244}
245
246SBProcess
247SBQueue::GetProcess ()
248{
249 SBProcess result;
250 QueueSP queue_sp = m_queue_wp.lock();
251 if (queue_sp)
252 {
253 result.SetSP (queue_sp->GetProcess());
254 }
255 return result;
256}