blob: 62eb1defde30d4de04ab77bb821e2122046eef7d [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
80ThreadList::AddThread (ThreadSP &thread_sp)
81{
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{
178 Mutex::Locker locker(m_threads_mutex);
179
180 // Running events should never stop, obviously...
181
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000182 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183
Jim Inghamb01e7422010-06-19 04:45:32 +0000184 bool should_stop = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185 m_process->UpdateThreadListIfNeeded();
186
187 collection::iterator pos, end = m_threads.end();
188
Greg Clayton2cad65a2010-09-03 17:10:42 +0000189 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000190 {
191 log->PutCString("");
192 log->Printf ("ThreadList::%s: %zu threads", __FUNCTION__, m_threads.size());
193 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000194
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195 // Run through the threads and ask whether we should stop. Don't ask
196 // suspended threads, however, it makes more sense for them to preserve their
197 // state across the times the process runs but they don't get a chance to.
198 for (pos = m_threads.begin(); pos != end; ++pos)
199 {
200 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000201
Greg Clayton2cad65a2010-09-03 17:10:42 +0000202 if (thread_sp->GetResumeState () == eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203 {
Greg Clayton2cad65a2010-09-03 17:10:42 +0000204 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000205 log->Printf ("ThreadList::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
Greg Clayton1346f7e2010-09-03 22:45:01 +0000206 __FUNCTION__,
207 thread_sp->GetID (),
208 thread_sp->GetRegisterContext()->GetPC());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000209 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000211
212 if (thread_sp->ThreadStoppedForAReason() == false)
213 {
214 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000215 log->Printf ("ThreadList::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
Greg Clayton1346f7e2010-09-03 22:45:01 +0000216 __FUNCTION__,
217 thread_sp->GetID (),
218 thread_sp->GetRegisterContext()->GetPC());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000219 continue;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000220 }
221
Greg Clayton2cad65a2010-09-03 17:10:42 +0000222 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000223 log->Printf ("ThreadList::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
Greg Clayton1346f7e2010-09-03 22:45:01 +0000224 __FUNCTION__,
225 thread_sp->GetID (),
Jim Ingham10c4b242011-10-15 00:23:43 +0000226 thread_sp->GetRegisterContext()->GetPC());
227 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000228 if (thread_should_stop)
229 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000231
Greg Clayton2cad65a2010-09-03 17:10:42 +0000232 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000233 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000234
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235 if (should_stop)
236 {
237 for (pos = m_threads.begin(); pos != end; ++pos)
238 {
239 ThreadSP thread_sp(*pos);
240 thread_sp->WillStop ();
241 }
242 }
243
244 return should_stop;
245}
246
247Vote
248ThreadList::ShouldReportStop (Event *event_ptr)
249{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000250 Mutex::Locker locker(m_threads_mutex);
251
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252 Vote result = eVoteNoOpinion;
253 m_process->UpdateThreadListIfNeeded();
254 collection::iterator pos, end = m_threads.end();
255
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000256 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000257
258 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000259 log->Printf ("ThreadList::%s %zu threads", __FUNCTION__, m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000260
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261 // Run through the threads and ask whether we should report this event.
262 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
263 for (pos = m_threads.begin(); pos != end; ++pos)
264 {
265 ThreadSP thread_sp(*pos);
266 if (thread_sp->ThreadStoppedForAReason() && (thread_sp->GetResumeState () != eStateSuspended))
267 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000268 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000269 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000270 log->Printf ("ThreadList::%s thread 0x%4.4llx: pc = 0x%16.16llx, vote = %s",
Greg Clayton2cad65a2010-09-03 17:10:42 +0000271 __FUNCTION__,
272 thread_sp->GetID (),
273 thread_sp->GetRegisterContext()->GetPC(),
274 GetVoteAsCString (vote));
275 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276 {
Greg Clayton2cad65a2010-09-03 17:10:42 +0000277 case eVoteNoOpinion:
278 continue;
279
280 case eVoteYes:
281 result = eVoteYes;
282 break;
283
284 case eVoteNo:
285 if (result == eVoteNoOpinion)
286 {
287 result = eVoteNo;
288 }
289 else
290 {
291 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000292 log->Printf ("ThreadList::%s thread 0x%4.4llx: pc = 0x%16.16llx voted %s, but lost out because result was %s",
Greg Clayton2cad65a2010-09-03 17:10:42 +0000293 __FUNCTION__,
294 thread_sp->GetID (),
295 thread_sp->GetRegisterContext()->GetPC(),
296 GetVoteAsCString (vote),
297 GetVoteAsCString (result));
298 }
299 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300 }
301 }
302 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000303 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000304 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 return result;
306}
307
308Vote
309ThreadList::ShouldReportRun (Event *event_ptr)
310{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000311
312 Mutex::Locker locker(m_threads_mutex);
313
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314 Vote result = eVoteNoOpinion;
315 m_process->UpdateThreadListIfNeeded();
316 collection::iterator pos, end = m_threads.end();
317
318 // Run through the threads and ask whether we should report this event.
319 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
320
Jim Inghamce579832011-01-24 04:11:25 +0000321 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
322
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 for (pos = m_threads.begin(); pos != end; ++pos)
324 {
Jim Inghamce579832011-01-24 04:11:25 +0000325 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326 {
Jim Inghamce579832011-01-24 04:11:25 +0000327 switch ((*pos)->ShouldReportRun (event_ptr))
328 {
329 case eVoteNoOpinion:
330 continue;
331 case eVoteYes:
332 if (result == eVoteNoOpinion)
333 result = eVoteYes;
334 break;
335 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000336 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000337 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4llx) says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000338 (*pos)->GetIndexID(),
339 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000340 result = eVoteNo;
341 break;
342 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 }
344 }
345 return result;
346}
347
348void
349ThreadList::Clear()
350{
Greg Claytonc4e411f2011-01-18 19:36:39 +0000351 Mutex::Locker locker(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 m_stop_id = 0;
353 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000354 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355}
356
357void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000358ThreadList::Destroy()
359{
360 Mutex::Locker locker(m_threads_mutex);
361 const uint32_t num_threads = m_threads.size();
362 for (uint32_t idx = 0; idx < num_threads; ++idx)
363 {
364 m_threads[idx]->DestroyThread();
365 }
366}
367
368void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369ThreadList::RefreshStateAfterStop ()
370{
371 Mutex::Locker locker(m_threads_mutex);
372
373 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000374
375 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000376 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000377 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378
379 collection::iterator pos, end = m_threads.end();
380 for (pos = m_threads.begin(); pos != end; ++pos)
381 (*pos)->RefreshStateAfterStop ();
382}
383
384void
385ThreadList::DiscardThreadPlans ()
386{
387 // You don't need to update the thread list here, because only threads
388 // that you currently know about have any thread plans.
389 Mutex::Locker locker(m_threads_mutex);
390
391 collection::iterator pos, end = m_threads.end();
392 for (pos = m_threads.begin(); pos != end; ++pos)
393 (*pos)->DiscardThreadPlans (true);
394
395}
396
397bool
398ThreadList::WillResume ()
399{
400 // Run through the threads and perform their momentary actions.
401 // But we only do this for threads that are running, user suspended
402 // threads stay where they are.
403 bool success = true;
404
405 Mutex::Locker locker(m_threads_mutex);
406 m_process->UpdateThreadListIfNeeded();
407
408 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409
Jim Inghama3241c12010-07-14 02:27:20 +0000410 // See if any thread wants to run stopping others. If it does, then we won't
411 // setup the other threads for resume, since they aren't going to get a chance
412 // to run. This is necessary because the SetupForResume might add "StopOthers"
413 // plans which would then get to be part of the who-gets-to-run negotiation, but
414 // they're coming in after the fact, and the threads that are already set up should
415 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416
Jim Inghama3241c12010-07-14 02:27:20 +0000417 bool wants_solo_run = false;
418
419 for (pos = m_threads.begin(); pos != end; ++pos)
420 {
421 if ((*pos)->GetResumeState() != eStateSuspended &&
422 (*pos)->GetCurrentPlan()->StopOthers())
423 {
424 wants_solo_run = true;
425 break;
426 }
427 }
428
Jim Ingham1c823b42011-01-22 01:33:44 +0000429 if (wants_solo_run)
430 {
431 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000432 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000433 log->Printf ("Turning on notification of new threads while single stepping a thread.");
434 m_process->StartNoticingNewThreads();
435 }
436 else
437 {
438 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000439 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000440 log->Printf ("Turning off notification of new threads while single stepping a thread.");
441 m_process->StopNoticingNewThreads();
442 }
Jim Inghama3241c12010-07-14 02:27:20 +0000443
444 // Give all the threads that are likely to run a last chance to set up their state before we
445 // negotiate who is actually going to get a chance to run...
446 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
447 // call setup on the threads that request StopOthers...
448
449 for (pos = m_threads.begin(); pos != end; ++pos)
450 {
451 if ((*pos)->GetResumeState() != eStateSuspended
452 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
453 {
454 (*pos)->SetupForResume ();
455 }
456 }
457
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458 // Now go through the threads and see if any thread wants to run just itself.
459 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000460
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 ThreadList run_me_only_list (m_process);
462
463 run_me_only_list.SetStopID(m_process->GetStopID());
464
465 ThreadSP immediate_thread_sp;
466 bool run_only_current_thread = false;
467
468 for (pos = m_threads.begin(); pos != end; ++pos)
469 {
470 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000471 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 thread_sp->GetCurrentPlan()->StopOthers())
473 {
474 // You can't say "stop others" and also want yourself to be suspended.
475 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
476
Jim Ingham2976d002010-08-26 21:32:51 +0000477 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 {
479 run_only_current_thread = true;
480 run_me_only_list.Clear();
481 run_me_only_list.AddThread (thread_sp);
482 break;
483 }
484
485 run_me_only_list.AddThread (thread_sp);
486 }
487
488 }
489
490 if (immediate_thread_sp)
491 {
492 for (pos = m_threads.begin(); pos != end; ++pos)
493 {
494 ThreadSP thread_sp(*pos);
495 if (thread_sp.get() == immediate_thread_sp.get())
496 thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState());
497 else
498 thread_sp->WillResume (eStateSuspended);
499 }
500 }
501 else if (run_me_only_list.GetSize (false) == 0)
502 {
503 // Everybody runs as they wish:
504 for (pos = m_threads.begin(); pos != end; ++pos)
505 {
506 ThreadSP thread_sp(*pos);
507 thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState());
508 }
509 }
510 else
511 {
512 ThreadSP thread_to_run;
513
514 if (run_only_current_thread)
515 {
Jim Ingham2976d002010-08-26 21:32:51 +0000516 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517 }
518 else if (run_me_only_list.GetSize (false) == 1)
519 {
520 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
521 }
522 else
523 {
524 int random_thread = (int)
525 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
526 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
527 }
528
529 for (pos = m_threads.begin(); pos != end; ++pos)
530 {
531 ThreadSP thread_sp(*pos);
532 if (thread_sp == thread_to_run)
533 thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState());
534 else
535 thread_sp->WillResume (eStateSuspended);
536 }
537 }
538
539 return success;
540}
541
542void
543ThreadList::DidResume ()
544{
Greg Claytonc4e411f2011-01-18 19:36:39 +0000545 Mutex::Locker locker(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546 collection::iterator pos, end = m_threads.end();
547 for (pos = m_threads.begin(); pos != end; ++pos)
548 {
549 // Don't clear out threads that aren't going to get a chance to run, rather
550 // leave their state for the next time around.
551 ThreadSP thread_sp(*pos);
552 if (thread_sp->GetResumeState() != eStateSuspended)
553 thread_sp->DidResume ();
554 }
555}
556
557ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000558ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559{
560 Mutex::Locker locker(m_threads_mutex);
Johnny Chen943ddb72011-08-25 21:59:59 +0000561 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
562 if (!thread_sp.get())
563 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000564 if (m_threads.size() == 0)
565 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000566 m_selected_tid = m_threads[0]->GetID();
567 thread_sp = m_threads[0];
568 }
569 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570}
571
572bool
Jim Ingham2976d002010-08-26 21:32:51 +0000573ThreadList::SetSelectedThreadByID (lldb::tid_t tid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574{
575 Mutex::Locker locker(m_threads_mutex);
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000576 ThreadSP selected_thread_sp(FindThreadByID(tid));
577 if (selected_thread_sp)
578 {
Jim Ingham2976d002010-08-26 21:32:51 +0000579 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000580 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
581 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582 else
Jim Ingham2976d002010-08-26 21:32:51 +0000583 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584
Jim Ingham2976d002010-08-26 21:32:51 +0000585 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586}
587
588bool
Jim Ingham2976d002010-08-26 21:32:51 +0000589ThreadList::SetSelectedThreadByIndexID (uint32_t index_id)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590{
591 Mutex::Locker locker(m_threads_mutex);
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000592 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
593 if (selected_thread_sp.get())
594 {
595 m_selected_tid = selected_thread_sp->GetID();
596 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
597 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 else
Jim Ingham2976d002010-08-26 21:32:51 +0000599 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600
Jim Ingham2976d002010-08-26 21:32:51 +0000601 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602}
603
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000604void
605ThreadList::Update (ThreadList &rhs)
606{
607 if (this != &rhs)
608 {
609 // Lock both mutexes to make sure neither side changes anyone on us
610 // while the assignement occurs
611 Mutex::Locker locker_lhs(m_threads_mutex);
612 Mutex::Locker locker_rhs(rhs.m_threads_mutex);
613 m_process = rhs.m_process;
614 m_stop_id = rhs.m_stop_id;
615 m_threads.swap(rhs.m_threads);
616 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000617
618
619 // Now we look for threads that we are done with and
620 // make sure to clear them up as much as possible so
621 // anyone with a shared pointer will still have a reference,
622 // but the thread won't be of much use. Using std::weak_ptr
623 // for all backward references (such as a thread to a process)
624 // will eventually solve this issue for us, but for now, we
625 // need to work around the issue
626 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
627 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
628 {
629 const lldb::tid_t tid = (*rhs_pos)->GetID();
630 bool thread_is_alive = false;
631 const uint32_t num_threads = m_threads.size();
632 for (uint32_t idx = 0; idx < num_threads; ++idx)
633 {
634 if (m_threads[idx]->GetID() == tid)
635 {
636 thread_is_alive = true;
637 break;
638 }
639 }
640 if (!thread_is_alive)
641 (*rhs_pos)->DestroyThread();
642 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000643 }
644}
645
646