blob: 8d3f4a84273070e0ac4ee82ec7713f96ac015614 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ThreadList.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#include <stdlib.h>
10
11#include <algorithm>
12
Greg Clayton2cad65a2010-09-03 17:10:42 +000013#include "lldb/Core/Log.h"
14#include "lldb/Target/RegisterContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Target/ThreadList.h"
16#include "lldb/Target/Thread.h"
17#include "lldb/Target/ThreadPlan.h"
18#include "lldb/Target/Process.h"
19
20using namespace lldb;
21using namespace lldb_private;
22
23ThreadList::ThreadList (Process *process) :
24 m_process (process),
25 m_stop_id (0),
26 m_threads(),
27 m_threads_mutex (Mutex::eMutexTypeRecursive),
Jim Ingham2976d002010-08-26 21:32:51 +000028 m_selected_tid (LLDB_INVALID_THREAD_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029{
30}
31
32ThreadList::ThreadList (const ThreadList &rhs) :
33 m_process (),
34 m_stop_id (),
35 m_threads (),
36 m_threads_mutex (Mutex::eMutexTypeRecursive),
Jim Ingham2976d002010-08-26 21:32:51 +000037 m_selected_tid ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038{
39 // Use the assignment operator since it uses the mutex
40 *this = rhs;
41}
42
43const ThreadList&
44ThreadList::operator = (const ThreadList& rhs)
45{
46 if (this != &rhs)
47 {
48 // Lock both mutexes to make sure neither side changes anyone on us
49 // while the assignement occurs
Greg Claytonb1320972010-07-14 00:18:15 +000050 Mutex::Locker locker_lhs(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 Mutex::Locker locker_rhs(rhs.m_threads_mutex);
52 m_process = rhs.m_process;
53 m_stop_id = rhs.m_stop_id;
54 m_threads = rhs.m_threads;
Jim Ingham2976d002010-08-26 21:32:51 +000055 m_selected_tid = rhs.m_selected_tid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056 }
57 return *this;
58}
59
60
61ThreadList::~ThreadList()
62{
63}
64
65
66uint32_t
67ThreadList::GetStopID () const
68{
69 return m_stop_id;
70}
71
72void
73ThreadList::SetStopID (uint32_t stop_id)
74{
75 m_stop_id = stop_id;
76}
77
78
79void
Greg Claytonc3776bf2012-02-09 06:16:32 +000080ThreadList::AddThread (const ThreadSP &thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081{
82 Mutex::Locker locker(m_threads_mutex);
83 m_threads.push_back(thread_sp);
84}
85
86uint32_t
87ThreadList::GetSize (bool can_update)
88{
89 Mutex::Locker locker(m_threads_mutex);
90 if (can_update)
91 m_process->UpdateThreadListIfNeeded();
92 return m_threads.size();
93}
94
95ThreadSP
96ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
97{
98 Mutex::Locker locker(m_threads_mutex);
99 if (can_update)
100 m_process->UpdateThreadListIfNeeded();
101
102 ThreadSP thread_sp;
103 if (idx < m_threads.size())
104 thread_sp = m_threads[idx];
105 return thread_sp;
106}
107
108ThreadSP
109ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
110{
111 Mutex::Locker locker(m_threads_mutex);
112
113 if (can_update)
114 m_process->UpdateThreadListIfNeeded();
115
116 ThreadSP thread_sp;
117 uint32_t idx = 0;
118 const uint32_t num_threads = m_threads.size();
119 for (idx = 0; idx < num_threads; ++idx)
120 {
121 if (m_threads[idx]->GetID() == tid)
122 {
123 thread_sp = m_threads[idx];
124 break;
125 }
126 }
127 return thread_sp;
128}
129
130ThreadSP
131ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
132{
133 ThreadSP thread_sp;
134 if (thread_ptr)
135 {
136 Mutex::Locker locker(m_threads_mutex);
137
138 uint32_t idx = 0;
139 const uint32_t num_threads = m_threads.size();
140 for (idx = 0; idx < num_threads; ++idx)
141 {
142 if (m_threads[idx].get() == thread_ptr)
143 {
144 thread_sp = m_threads[idx];
145 break;
146 }
147 }
148 }
149 return thread_sp;
150}
151
152
153
154ThreadSP
155ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
156{
157 Mutex::Locker locker(m_threads_mutex);
158
159 if (can_update)
160 m_process->UpdateThreadListIfNeeded();
161
162 ThreadSP thread_sp;
163 const uint32_t num_threads = m_threads.size();
164 for (uint32_t idx = 0; idx < num_threads; ++idx)
165 {
166 if (m_threads[idx]->GetIndexID() == index_id)
167 {
168 thread_sp = m_threads[idx];
169 break;
170 }
171 }
172 return thread_sp;
173}
174
175bool
176ThreadList::ShouldStop (Event *event_ptr)
177{
Jim Inghamb42f3af2012-11-15 22:44:04 +0000178 bool should_stop = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 // Running events should never stop, obviously...
180
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000181 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182
Jim Inghamb42f3af2012-11-15 22:44:04 +0000183 // The ShouldStop method of the threads can do a whole lot of work,
184 // running breakpoint commands & conditions, etc. So we don't want
185 // to keep the ThreadList locked the whole time we are doing this.
186 // FIXME: It is possible that running code could cause new threads
187 // to be created. If that happens we will miss asking them whether
188 // then should stop. This is not a big deal, since we haven't had
189 // a chance to hang any interesting operations on those threads yet.
190
191 collection threads_copy;
192 {
193 // Scope for locker
194 Mutex::Locker locker(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195
Jim Inghamb42f3af2012-11-15 22:44:04 +0000196 m_process->UpdateThreadListIfNeeded();
197 threads_copy = m_threads;
198 }
199
200 collection::iterator pos, end = threads_copy.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201
Greg Clayton2cad65a2010-09-03 17:10:42 +0000202 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000203 {
204 log->PutCString("");
Greg Clayton43e0af02012-09-18 18:04:04 +0000205 log->Printf ("ThreadList::%s: %llu threads", __FUNCTION__, (uint64_t)m_threads.size());
Jim Ingham10c4b242011-10-15 00:23:43 +0000206 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000207
Jim Inghamb42f3af2012-11-15 22:44:04 +0000208 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209 {
210 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000211
Jim Ingham10c4b242011-10-15 00:23:43 +0000212 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000213 if (thread_should_stop)
214 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000216
Greg Clayton2cad65a2010-09-03 17:10:42 +0000217 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000218 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000219
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220 if (should_stop)
221 {
Jim Inghamb42f3af2012-11-15 22:44:04 +0000222 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223 {
224 ThreadSP thread_sp(*pos);
225 thread_sp->WillStop ();
226 }
227 }
228
229 return should_stop;
230}
231
232Vote
233ThreadList::ShouldReportStop (Event *event_ptr)
234{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000235 Mutex::Locker locker(m_threads_mutex);
236
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237 Vote result = eVoteNoOpinion;
238 m_process->UpdateThreadListIfNeeded();
239 collection::iterator pos, end = m_threads.end();
240
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000241 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000242
243 if (log)
Greg Clayton43e0af02012-09-18 18:04:04 +0000244 log->Printf ("ThreadList::%s %llu threads", __FUNCTION__, (uint64_t)m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000245
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246 // Run through the threads and ask whether we should report this event.
247 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
248 for (pos = m_threads.begin(); pos != end; ++pos)
249 {
250 ThreadSP thread_sp(*pos);
Jim Ingham92087d82012-01-31 23:09:20 +0000251 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
252 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253 {
Jim Ingham92087d82012-01-31 23:09:20 +0000254 case eVoteNoOpinion:
255 continue;
256
257 case eVoteYes:
258 result = eVoteYes;
259 break;
260
261 case eVoteNo:
262 if (result == eVoteNoOpinion)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263 {
Jim Ingham92087d82012-01-31 23:09:20 +0000264 result = eVoteNo;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265 }
Jim Ingham92087d82012-01-31 23:09:20 +0000266 else
267 {
268 if (log)
269 log->Printf ("ThreadList::%s thread 0x%4.4llx: voted %s, but lost out because result was %s",
270 __FUNCTION__,
271 thread_sp->GetID (),
272 GetVoteAsCString (vote),
273 GetVoteAsCString (result));
274 }
275 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276 }
277 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000278 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000279 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280 return result;
281}
282
283Vote
284ThreadList::ShouldReportRun (Event *event_ptr)
285{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000286
287 Mutex::Locker locker(m_threads_mutex);
288
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 Vote result = eVoteNoOpinion;
290 m_process->UpdateThreadListIfNeeded();
291 collection::iterator pos, end = m_threads.end();
292
293 // Run through the threads and ask whether we should report this event.
294 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
295
Jim Inghamce579832011-01-24 04:11:25 +0000296 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
297
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 for (pos = m_threads.begin(); pos != end; ++pos)
299 {
Jim Inghamce579832011-01-24 04:11:25 +0000300 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301 {
Jim Inghamce579832011-01-24 04:11:25 +0000302 switch ((*pos)->ShouldReportRun (event_ptr))
303 {
304 case eVoteNoOpinion:
305 continue;
306 case eVoteYes:
307 if (result == eVoteNoOpinion)
308 result = eVoteYes;
309 break;
310 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000311 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000312 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4llx) says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000313 (*pos)->GetIndexID(),
314 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000315 result = eVoteNo;
316 break;
317 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318 }
319 }
320 return result;
321}
322
323void
324ThreadList::Clear()
325{
Greg Claytonc4e411f2011-01-18 19:36:39 +0000326 Mutex::Locker locker(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327 m_stop_id = 0;
328 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000329 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330}
331
332void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000333ThreadList::Destroy()
334{
335 Mutex::Locker locker(m_threads_mutex);
336 const uint32_t num_threads = m_threads.size();
337 for (uint32_t idx = 0; idx < num_threads; ++idx)
338 {
339 m_threads[idx]->DestroyThread();
340 }
341}
342
343void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344ThreadList::RefreshStateAfterStop ()
345{
346 Mutex::Locker locker(m_threads_mutex);
347
348 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000349
350 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000351 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000352 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353
354 collection::iterator pos, end = m_threads.end();
355 for (pos = m_threads.begin(); pos != end; ++pos)
356 (*pos)->RefreshStateAfterStop ();
357}
358
359void
360ThreadList::DiscardThreadPlans ()
361{
362 // You don't need to update the thread list here, because only threads
363 // that you currently know about have any thread plans.
364 Mutex::Locker locker(m_threads_mutex);
365
366 collection::iterator pos, end = m_threads.end();
367 for (pos = m_threads.begin(); pos != end; ++pos)
368 (*pos)->DiscardThreadPlans (true);
369
370}
371
372bool
373ThreadList::WillResume ()
374{
375 // Run through the threads and perform their momentary actions.
376 // But we only do this for threads that are running, user suspended
377 // threads stay where they are.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378
379 Mutex::Locker locker(m_threads_mutex);
380 m_process->UpdateThreadListIfNeeded();
381
382 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383
Jim Inghama3241c12010-07-14 02:27:20 +0000384 // See if any thread wants to run stopping others. If it does, then we won't
385 // setup the other threads for resume, since they aren't going to get a chance
386 // to run. This is necessary because the SetupForResume might add "StopOthers"
387 // plans which would then get to be part of the who-gets-to-run negotiation, but
388 // they're coming in after the fact, and the threads that are already set up should
389 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390
Jim Inghama3241c12010-07-14 02:27:20 +0000391 bool wants_solo_run = false;
392
393 for (pos = m_threads.begin(); pos != end; ++pos)
394 {
395 if ((*pos)->GetResumeState() != eStateSuspended &&
396 (*pos)->GetCurrentPlan()->StopOthers())
397 {
398 wants_solo_run = true;
399 break;
400 }
401 }
402
Jim Ingham1c823b42011-01-22 01:33:44 +0000403 if (wants_solo_run)
404 {
405 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000406 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000407 log->Printf ("Turning on notification of new threads while single stepping a thread.");
408 m_process->StartNoticingNewThreads();
409 }
410 else
411 {
412 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000413 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000414 log->Printf ("Turning off notification of new threads while single stepping a thread.");
415 m_process->StopNoticingNewThreads();
416 }
Jim Inghama3241c12010-07-14 02:27:20 +0000417
418 // Give all the threads that are likely to run a last chance to set up their state before we
419 // negotiate who is actually going to get a chance to run...
420 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
421 // call setup on the threads that request StopOthers...
422
423 for (pos = m_threads.begin(); pos != end; ++pos)
424 {
425 if ((*pos)->GetResumeState() != eStateSuspended
426 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
427 {
428 (*pos)->SetupForResume ();
429 }
430 }
431
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 // Now go through the threads and see if any thread wants to run just itself.
433 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000434
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 ThreadList run_me_only_list (m_process);
436
437 run_me_only_list.SetStopID(m_process->GetStopID());
438
439 ThreadSP immediate_thread_sp;
440 bool run_only_current_thread = false;
441
442 for (pos = m_threads.begin(); pos != end; ++pos)
443 {
444 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000445 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 thread_sp->GetCurrentPlan()->StopOthers())
447 {
448 // You can't say "stop others" and also want yourself to be suspended.
449 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
450
Jim Ingham2976d002010-08-26 21:32:51 +0000451 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 {
453 run_only_current_thread = true;
454 run_me_only_list.Clear();
455 run_me_only_list.AddThread (thread_sp);
456 break;
457 }
458
459 run_me_only_list.AddThread (thread_sp);
460 }
461
462 }
463
Jim Ingham513c6bb2012-09-01 01:02:41 +0000464 bool need_to_resume = true;
465
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466 if (immediate_thread_sp)
467 {
468 for (pos = m_threads.begin(); pos != end; ++pos)
469 {
470 ThreadSP thread_sp(*pos);
471 if (thread_sp.get() == immediate_thread_sp.get())
472 thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState());
473 else
474 thread_sp->WillResume (eStateSuspended);
475 }
476 }
477 else if (run_me_only_list.GetSize (false) == 0)
478 {
479 // Everybody runs as they wish:
480 for (pos = m_threads.begin(); pos != end; ++pos)
481 {
482 ThreadSP thread_sp(*pos);
Jim Inghamcb5d5a52012-05-31 20:47:56 +0000483 StateType run_state;
484 if (thread_sp->GetResumeState() != eStateSuspended)
485 run_state = thread_sp->GetCurrentPlan()->RunState();
486 else
487 run_state = eStateSuspended;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000488 if (!thread_sp->WillResume(run_state))
489 need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 }
491 }
492 else
493 {
494 ThreadSP thread_to_run;
495
496 if (run_only_current_thread)
497 {
Jim Ingham2976d002010-08-26 21:32:51 +0000498 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499 }
500 else if (run_me_only_list.GetSize (false) == 1)
501 {
502 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
503 }
504 else
505 {
506 int random_thread = (int)
507 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
508 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
509 }
510
511 for (pos = m_threads.begin(); pos != end; ++pos)
512 {
513 ThreadSP thread_sp(*pos);
514 if (thread_sp == thread_to_run)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000515 {
516 if (!thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState()))
517 need_to_resume = false;
518 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519 else
520 thread_sp->WillResume (eStateSuspended);
521 }
522 }
523
Jim Ingham513c6bb2012-09-01 01:02:41 +0000524 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525}
526
527void
528ThreadList::DidResume ()
529{
Greg Claytonc4e411f2011-01-18 19:36:39 +0000530 Mutex::Locker locker(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 collection::iterator pos, end = m_threads.end();
532 for (pos = m_threads.begin(); pos != end; ++pos)
533 {
534 // Don't clear out threads that aren't going to get a chance to run, rather
535 // leave their state for the next time around.
536 ThreadSP thread_sp(*pos);
537 if (thread_sp->GetResumeState() != eStateSuspended)
538 thread_sp->DidResume ();
539 }
540}
541
542ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000543ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544{
545 Mutex::Locker locker(m_threads_mutex);
Johnny Chen943ddb72011-08-25 21:59:59 +0000546 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
547 if (!thread_sp.get())
548 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000549 if (m_threads.size() == 0)
550 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000551 m_selected_tid = m_threads[0]->GetID();
552 thread_sp = m_threads[0];
553 }
554 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555}
556
557bool
Jim Ingham2976d002010-08-26 21:32:51 +0000558ThreadList::SetSelectedThreadByID (lldb::tid_t tid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559{
560 Mutex::Locker locker(m_threads_mutex);
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000561 ThreadSP selected_thread_sp(FindThreadByID(tid));
562 if (selected_thread_sp)
563 {
Jim Ingham2976d002010-08-26 21:32:51 +0000564 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000565 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
566 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567 else
Jim Ingham2976d002010-08-26 21:32:51 +0000568 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569
Jim Ingham2976d002010-08-26 21:32:51 +0000570 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571}
572
573bool
Jim Ingham2976d002010-08-26 21:32:51 +0000574ThreadList::SetSelectedThreadByIndexID (uint32_t index_id)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575{
576 Mutex::Locker locker(m_threads_mutex);
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000577 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
578 if (selected_thread_sp.get())
579 {
580 m_selected_tid = selected_thread_sp->GetID();
581 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
582 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 else
Jim Ingham2976d002010-08-26 21:32:51 +0000584 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585
Jim Ingham2976d002010-08-26 21:32:51 +0000586 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000589void
590ThreadList::Update (ThreadList &rhs)
591{
592 if (this != &rhs)
593 {
594 // Lock both mutexes to make sure neither side changes anyone on us
595 // while the assignement occurs
596 Mutex::Locker locker_lhs(m_threads_mutex);
597 Mutex::Locker locker_rhs(rhs.m_threads_mutex);
598 m_process = rhs.m_process;
599 m_stop_id = rhs.m_stop_id;
600 m_threads.swap(rhs.m_threads);
601 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000602
603
604 // Now we look for threads that we are done with and
605 // make sure to clear them up as much as possible so
606 // anyone with a shared pointer will still have a reference,
607 // but the thread won't be of much use. Using std::weak_ptr
608 // for all backward references (such as a thread to a process)
609 // will eventually solve this issue for us, but for now, we
610 // need to work around the issue
611 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
612 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
613 {
614 const lldb::tid_t tid = (*rhs_pos)->GetID();
615 bool thread_is_alive = false;
616 const uint32_t num_threads = m_threads.size();
617 for (uint32_t idx = 0; idx < num_threads; ++idx)
618 {
619 if (m_threads[idx]->GetID() == tid)
620 {
621 thread_is_alive = true;
622 break;
623 }
624 }
625 if (!thread_is_alive)
626 (*rhs_pos)->DestroyThread();
627 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000628 }
629}
630
Greg Claytonfa559e52012-05-18 02:38:05 +0000631void
632ThreadList::Flush ()
633{
634 Mutex::Locker locker(m_threads_mutex);
635 collection::iterator pos, end = m_threads.end();
636 for (pos = m_threads.begin(); pos != end; ++pos)
637 (*pos)->Flush ();
638}
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000639