blob: 10c8e79c7329b3557fd2b8054040207e8c44ec88 [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{
Greg Claytonac358da2013-03-28 18:33:53 +000063 // Clear the thread list. Clear will take the mutex lock
64 // which will ensure that if anyone is using the list
65 // they won't get it removed while using it.
66 Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067}
68
69
70uint32_t
71ThreadList::GetStopID () const
72{
73 return m_stop_id;
74}
75
76void
77ThreadList::SetStopID (uint32_t stop_id)
78{
79 m_stop_id = stop_id;
80}
81
82
83void
Greg Claytonc3776bf2012-02-09 06:16:32 +000084ThreadList::AddThread (const ThreadSP &thread_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085{
86 Mutex::Locker locker(m_threads_mutex);
87 m_threads.push_back(thread_sp);
88}
89
90uint32_t
91ThreadList::GetSize (bool can_update)
92{
93 Mutex::Locker locker(m_threads_mutex);
94 if (can_update)
95 m_process->UpdateThreadListIfNeeded();
96 return m_threads.size();
97}
98
99ThreadSP
100ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
101{
102 Mutex::Locker locker(m_threads_mutex);
103 if (can_update)
104 m_process->UpdateThreadListIfNeeded();
105
106 ThreadSP thread_sp;
107 if (idx < m_threads.size())
108 thread_sp = m_threads[idx];
109 return thread_sp;
110}
111
112ThreadSP
113ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
114{
115 Mutex::Locker locker(m_threads_mutex);
116
117 if (can_update)
118 m_process->UpdateThreadListIfNeeded();
119
120 ThreadSP thread_sp;
121 uint32_t idx = 0;
122 const uint32_t num_threads = m_threads.size();
123 for (idx = 0; idx < num_threads; ++idx)
124 {
125 if (m_threads[idx]->GetID() == tid)
126 {
127 thread_sp = m_threads[idx];
128 break;
129 }
130 }
131 return thread_sp;
132}
133
134ThreadSP
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000135ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
136{
137 Mutex::Locker locker(m_threads_mutex);
138
139 if (can_update)
140 m_process->UpdateThreadListIfNeeded();
141
142 ThreadSP thread_sp;
143 uint32_t idx = 0;
144 const uint32_t num_threads = m_threads.size();
145 for (idx = 0; idx < num_threads; ++idx)
146 {
147 if (m_threads[idx]->GetID() == tid)
148 {
149 thread_sp = m_threads[idx];
150 m_threads.erase(m_threads.begin()+idx);
151 break;
152 }
153 }
154 return thread_sp;
155}
156
157ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
159{
160 ThreadSP thread_sp;
161 if (thread_ptr)
162 {
163 Mutex::Locker locker(m_threads_mutex);
164
165 uint32_t idx = 0;
166 const uint32_t num_threads = m_threads.size();
167 for (idx = 0; idx < num_threads; ++idx)
168 {
169 if (m_threads[idx].get() == thread_ptr)
170 {
171 thread_sp = m_threads[idx];
172 break;
173 }
174 }
175 }
176 return thread_sp;
177}
178
179
180
181ThreadSP
182ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
183{
184 Mutex::Locker locker(m_threads_mutex);
185
186 if (can_update)
187 m_process->UpdateThreadListIfNeeded();
188
189 ThreadSP thread_sp;
190 const uint32_t num_threads = m_threads.size();
191 for (uint32_t idx = 0; idx < num_threads; ++idx)
192 {
193 if (m_threads[idx]->GetIndexID() == index_id)
194 {
195 thread_sp = m_threads[idx];
196 break;
197 }
198 }
199 return thread_sp;
200}
201
202bool
203ThreadList::ShouldStop (Event *event_ptr)
204{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205 // Running events should never stop, obviously...
206
Greg Clayton5160ce52013-03-27 23:08:40 +0000207 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208
Jim Inghamb42f3af2012-11-15 22:44:04 +0000209 // The ShouldStop method of the threads can do a whole lot of work,
210 // running breakpoint commands & conditions, etc. So we don't want
211 // to keep the ThreadList locked the whole time we are doing this.
212 // FIXME: It is possible that running code could cause new threads
213 // to be created. If that happens we will miss asking them whether
214 // then should stop. This is not a big deal, since we haven't had
215 // a chance to hang any interesting operations on those threads yet.
216
217 collection threads_copy;
218 {
219 // Scope for locker
220 Mutex::Locker locker(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221
Jim Inghamb42f3af2012-11-15 22:44:04 +0000222 m_process->UpdateThreadListIfNeeded();
223 threads_copy = m_threads;
224 }
225
226 collection::iterator pos, end = threads_copy.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227
Greg Clayton2cad65a2010-09-03 17:10:42 +0000228 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000229 {
230 log->PutCString("");
Daniel Malead01b2952012-11-29 21:49:15 +0000231 log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Jim Ingham10c4b242011-10-15 00:23:43 +0000232 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000233
Jim Inghama0079042013-03-21 21:46:56 +0000234 bool did_anybody_stop_for_a_reason = false;
235 bool should_stop = false;
236
Jim Inghamb42f3af2012-11-15 22:44:04 +0000237 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238 {
239 ThreadSP thread_sp(*pos);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000240
Jim Inghama0079042013-03-21 21:46:56 +0000241 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
242
Jim Ingham10c4b242011-10-15 00:23:43 +0000243 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000244 if (thread_should_stop)
245 should_stop |= true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000247
Jim Inghama0079042013-03-21 21:46:56 +0000248 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
249 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
250 // the right thing to do and stopping gives the user control over what to do in this instance.
251
252 if (!should_stop && !did_anybody_stop_for_a_reason)
253 {
254 should_stop = true;
255 if (log)
256 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
257 }
258
Greg Clayton2cad65a2010-09-03 17:10:42 +0000259 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000260 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000261
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262 if (should_stop)
263 {
Jim Inghamb42f3af2012-11-15 22:44:04 +0000264 for (pos = threads_copy.begin(); pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265 {
266 ThreadSP thread_sp(*pos);
267 thread_sp->WillStop ();
268 }
269 }
270
271 return should_stop;
272}
273
274Vote
275ThreadList::ShouldReportStop (Event *event_ptr)
276{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000277 Mutex::Locker locker(m_threads_mutex);
278
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 Vote result = eVoteNoOpinion;
280 m_process->UpdateThreadListIfNeeded();
281 collection::iterator pos, end = m_threads.end();
282
Greg Clayton5160ce52013-03-27 23:08:40 +0000283 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000284
285 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000286 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
Greg Clayton2cad65a2010-09-03 17:10:42 +0000287
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 // Run through the threads and ask whether we should report this event.
289 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
290 for (pos = m_threads.begin(); pos != end; ++pos)
291 {
292 ThreadSP thread_sp(*pos);
Jim Ingham92087d82012-01-31 23:09:20 +0000293 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
294 switch (vote)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 {
Jim Ingham92087d82012-01-31 23:09:20 +0000296 case eVoteNoOpinion:
297 continue;
298
299 case eVoteYes:
300 result = eVoteYes;
301 break;
302
303 case eVoteNo:
304 if (result == eVoteNoOpinion)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 {
Jim Ingham92087d82012-01-31 23:09:20 +0000306 result = eVoteNo;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307 }
Jim Ingham92087d82012-01-31 23:09:20 +0000308 else
309 {
310 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000311 log->Printf ("ThreadList::%s thread 0x%4.4" PRIx64 ": voted %s, but lost out because result was %s",
Jim Ingham92087d82012-01-31 23:09:20 +0000312 __FUNCTION__,
313 thread_sp->GetID (),
314 GetVoteAsCString (vote),
315 GetVoteAsCString (result));
316 }
317 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318 }
319 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000320 if (log)
Jim Ingham10c4b242011-10-15 00:23:43 +0000321 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 return result;
323}
324
325Vote
326ThreadList::ShouldReportRun (Event *event_ptr)
327{
Greg Clayton2cad65a2010-09-03 17:10:42 +0000328
329 Mutex::Locker locker(m_threads_mutex);
330
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331 Vote result = eVoteNoOpinion;
332 m_process->UpdateThreadListIfNeeded();
333 collection::iterator pos, end = m_threads.end();
334
335 // Run through the threads and ask whether we should report this event.
336 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
337
Greg Clayton5160ce52013-03-27 23:08:40 +0000338 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamce579832011-01-24 04:11:25 +0000339
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 for (pos = m_threads.begin(); pos != end; ++pos)
341 {
Jim Inghamce579832011-01-24 04:11:25 +0000342 if ((*pos)->GetResumeState () != eStateSuspended)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 {
Jim Inghamce579832011-01-24 04:11:25 +0000344 switch ((*pos)->ShouldReportRun (event_ptr))
345 {
346 case eVoteNoOpinion:
347 continue;
348 case eVoteYes:
349 if (result == eVoteNoOpinion)
350 result = eVoteYes;
351 break;
352 case eVoteNo:
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000353 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000354 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
Greg Claytonabcbc8a2011-01-24 05:36:47 +0000355 (*pos)->GetIndexID(),
356 (*pos)->GetID());
Jim Inghamce579832011-01-24 04:11:25 +0000357 result = eVoteNo;
358 break;
359 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360 }
361 }
362 return result;
363}
364
365void
366ThreadList::Clear()
367{
Greg Claytonc4e411f2011-01-18 19:36:39 +0000368 Mutex::Locker locker(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 m_stop_id = 0;
370 m_threads.clear();
Jim Ingham2976d002010-08-26 21:32:51 +0000371 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372}
373
374void
Greg Claytone1cd1be2012-01-29 20:56:30 +0000375ThreadList::Destroy()
376{
377 Mutex::Locker locker(m_threads_mutex);
378 const uint32_t num_threads = m_threads.size();
379 for (uint32_t idx = 0; idx < num_threads; ++idx)
380 {
381 m_threads[idx]->DestroyThread();
382 }
383}
384
385void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386ThreadList::RefreshStateAfterStop ()
387{
388 Mutex::Locker locker(m_threads_mutex);
389
390 m_process->UpdateThreadListIfNeeded();
Jim Ingham1c823b42011-01-22 01:33:44 +0000391
Greg Clayton5160ce52013-03-27 23:08:40 +0000392 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000393 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000394 log->Printf ("Turning off notification of new threads while single stepping a thread.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395
396 collection::iterator pos, end = m_threads.end();
397 for (pos = m_threads.begin(); pos != end; ++pos)
398 (*pos)->RefreshStateAfterStop ();
399}
400
401void
402ThreadList::DiscardThreadPlans ()
403{
404 // You don't need to update the thread list here, because only threads
405 // that you currently know about have any thread plans.
406 Mutex::Locker locker(m_threads_mutex);
407
408 collection::iterator pos, end = m_threads.end();
409 for (pos = m_threads.begin(); pos != end; ++pos)
410 (*pos)->DiscardThreadPlans (true);
411
412}
413
414bool
415ThreadList::WillResume ()
416{
417 // Run through the threads and perform their momentary actions.
418 // But we only do this for threads that are running, user suspended
419 // threads stay where they are.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420
421 Mutex::Locker locker(m_threads_mutex);
422 m_process->UpdateThreadListIfNeeded();
423
424 collection::iterator pos, end = m_threads.end();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425
Jim Inghama3241c12010-07-14 02:27:20 +0000426 // See if any thread wants to run stopping others. If it does, then we won't
427 // setup the other threads for resume, since they aren't going to get a chance
428 // to run. This is necessary because the SetupForResume might add "StopOthers"
429 // plans which would then get to be part of the who-gets-to-run negotiation, but
430 // they're coming in after the fact, and the threads that are already set up should
431 // take priority.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432
Jim Inghama3241c12010-07-14 02:27:20 +0000433 bool wants_solo_run = false;
434
435 for (pos = m_threads.begin(); pos != end; ++pos)
436 {
437 if ((*pos)->GetResumeState() != eStateSuspended &&
438 (*pos)->GetCurrentPlan()->StopOthers())
439 {
440 wants_solo_run = true;
441 break;
442 }
443 }
444
Jim Ingham1c823b42011-01-22 01:33:44 +0000445 if (wants_solo_run)
446 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000447 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000448 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000449 log->Printf ("Turning on notification of new threads while single stepping a thread.");
450 m_process->StartNoticingNewThreads();
451 }
452 else
453 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000454 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham10c4b242011-10-15 00:23:43 +0000455 if (log && log->GetVerbose())
Jim Ingham1c823b42011-01-22 01:33:44 +0000456 log->Printf ("Turning off notification of new threads while single stepping a thread.");
457 m_process->StopNoticingNewThreads();
458 }
Jim Inghama3241c12010-07-14 02:27:20 +0000459
460 // Give all the threads that are likely to run a last chance to set up their state before we
461 // negotiate who is actually going to get a chance to run...
462 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
463 // call setup on the threads that request StopOthers...
464
465 for (pos = m_threads.begin(); pos != end; ++pos)
466 {
467 if ((*pos)->GetResumeState() != eStateSuspended
468 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
469 {
470 (*pos)->SetupForResume ();
471 }
472 }
473
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474 // Now go through the threads and see if any thread wants to run just itself.
475 // if so then pick one and run it.
Jim Inghama3241c12010-07-14 02:27:20 +0000476
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477 ThreadList run_me_only_list (m_process);
478
479 run_me_only_list.SetStopID(m_process->GetStopID());
480
481 ThreadSP immediate_thread_sp;
482 bool run_only_current_thread = false;
483
484 for (pos = m_threads.begin(); pos != end; ++pos)
485 {
486 ThreadSP thread_sp(*pos);
Jim Inghamb15bfc72010-10-20 00:39:53 +0000487 if (thread_sp->GetResumeState() != eStateSuspended &&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 thread_sp->GetCurrentPlan()->StopOthers())
489 {
490 // You can't say "stop others" and also want yourself to be suspended.
491 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
492
Jim Ingham2976d002010-08-26 21:32:51 +0000493 if (thread_sp == GetSelectedThread())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 {
495 run_only_current_thread = true;
496 run_me_only_list.Clear();
497 run_me_only_list.AddThread (thread_sp);
498 break;
499 }
500
501 run_me_only_list.AddThread (thread_sp);
502 }
503
504 }
505
Jim Ingham513c6bb2012-09-01 01:02:41 +0000506 bool need_to_resume = true;
507
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508 if (immediate_thread_sp)
509 {
510 for (pos = m_threads.begin(); pos != end; ++pos)
511 {
512 ThreadSP thread_sp(*pos);
513 if (thread_sp.get() == immediate_thread_sp.get())
514 thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState());
515 else
516 thread_sp->WillResume (eStateSuspended);
517 }
518 }
519 else if (run_me_only_list.GetSize (false) == 0)
520 {
521 // Everybody runs as they wish:
522 for (pos = m_threads.begin(); pos != end; ++pos)
523 {
524 ThreadSP thread_sp(*pos);
Jim Inghamcb5d5a52012-05-31 20:47:56 +0000525 StateType run_state;
526 if (thread_sp->GetResumeState() != eStateSuspended)
527 run_state = thread_sp->GetCurrentPlan()->RunState();
528 else
529 run_state = eStateSuspended;
Jim Ingham513c6bb2012-09-01 01:02:41 +0000530 if (!thread_sp->WillResume(run_state))
531 need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532 }
533 }
534 else
535 {
536 ThreadSP thread_to_run;
537
538 if (run_only_current_thread)
539 {
Jim Ingham2976d002010-08-26 21:32:51 +0000540 thread_to_run = GetSelectedThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000541 }
542 else if (run_me_only_list.GetSize (false) == 1)
543 {
544 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
545 }
546 else
547 {
548 int random_thread = (int)
549 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
550 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
551 }
552
553 for (pos = m_threads.begin(); pos != end; ++pos)
554 {
555 ThreadSP thread_sp(*pos);
556 if (thread_sp == thread_to_run)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000557 {
558 if (!thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState()))
559 need_to_resume = false;
560 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 else
562 thread_sp->WillResume (eStateSuspended);
563 }
564 }
565
Jim Ingham513c6bb2012-09-01 01:02:41 +0000566 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567}
568
569void
570ThreadList::DidResume ()
571{
Greg Claytonc4e411f2011-01-18 19:36:39 +0000572 Mutex::Locker locker(m_threads_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573 collection::iterator pos, end = m_threads.end();
574 for (pos = m_threads.begin(); pos != end; ++pos)
575 {
576 // Don't clear out threads that aren't going to get a chance to run, rather
577 // leave their state for the next time around.
578 ThreadSP thread_sp(*pos);
579 if (thread_sp->GetResumeState() != eStateSuspended)
580 thread_sp->DidResume ();
581 }
582}
583
584ThreadSP
Jim Ingham2976d002010-08-26 21:32:51 +0000585ThreadList::GetSelectedThread ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586{
587 Mutex::Locker locker(m_threads_mutex);
Johnny Chen943ddb72011-08-25 21:59:59 +0000588 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
589 if (!thread_sp.get())
590 {
Jason Molenda354b9a62011-09-13 01:13:16 +0000591 if (m_threads.size() == 0)
592 return thread_sp;
Johnny Chen943ddb72011-08-25 21:59:59 +0000593 m_selected_tid = m_threads[0]->GetID();
594 thread_sp = m_threads[0];
595 }
596 return thread_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597}
598
599bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000600ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601{
602 Mutex::Locker locker(m_threads_mutex);
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000603 ThreadSP selected_thread_sp(FindThreadByID(tid));
604 if (selected_thread_sp)
605 {
Jim Ingham2976d002010-08-26 21:32:51 +0000606 m_selected_tid = tid;
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000607 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
608 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609 else
Jim Ingham2976d002010-08-26 21:32:51 +0000610 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611
Jim Inghamc3faa192012-12-11 02:31:48 +0000612 if (notify)
613 NotifySelectedThreadChanged(m_selected_tid);
614
Jim Ingham2976d002010-08-26 21:32:51 +0000615 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616}
617
618bool
Jim Inghamc3faa192012-12-11 02:31:48 +0000619ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620{
621 Mutex::Locker locker(m_threads_mutex);
Jim Inghamb7f6b2f2011-09-08 22:13:49 +0000622 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
623 if (selected_thread_sp.get())
624 {
625 m_selected_tid = selected_thread_sp->GetID();
626 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
627 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 else
Jim Ingham2976d002010-08-26 21:32:51 +0000629 m_selected_tid = LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630
Jim Inghamc3faa192012-12-11 02:31:48 +0000631 if (notify)
632 NotifySelectedThreadChanged(m_selected_tid);
633
Jim Ingham2976d002010-08-26 21:32:51 +0000634 return m_selected_tid != LLDB_INVALID_THREAD_ID;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635}
636
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000637void
Jim Inghamc3faa192012-12-11 02:31:48 +0000638ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
639{
640 ThreadSP selected_thread_sp (FindThreadByID(tid));
641 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
642 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
643 new Thread::ThreadEventData(selected_thread_sp));
644}
645
646void
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000647ThreadList::Update (ThreadList &rhs)
648{
649 if (this != &rhs)
650 {
651 // Lock both mutexes to make sure neither side changes anyone on us
652 // while the assignement occurs
653 Mutex::Locker locker_lhs(m_threads_mutex);
654 Mutex::Locker locker_rhs(rhs.m_threads_mutex);
655 m_process = rhs.m_process;
656 m_stop_id = rhs.m_stop_id;
657 m_threads.swap(rhs.m_threads);
658 m_selected_tid = rhs.m_selected_tid;
Greg Claytone1cd1be2012-01-29 20:56:30 +0000659
660
661 // Now we look for threads that we are done with and
662 // make sure to clear them up as much as possible so
663 // anyone with a shared pointer will still have a reference,
664 // but the thread won't be of much use. Using std::weak_ptr
665 // for all backward references (such as a thread to a process)
666 // will eventually solve this issue for us, but for now, we
667 // need to work around the issue
668 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
669 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
670 {
671 const lldb::tid_t tid = (*rhs_pos)->GetID();
672 bool thread_is_alive = false;
673 const uint32_t num_threads = m_threads.size();
674 for (uint32_t idx = 0; idx < num_threads; ++idx)
675 {
676 if (m_threads[idx]->GetID() == tid)
677 {
678 thread_is_alive = true;
679 break;
680 }
681 }
682 if (!thread_is_alive)
683 (*rhs_pos)->DestroyThread();
684 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000685 }
686}
687
Greg Claytonfa559e52012-05-18 02:38:05 +0000688void
689ThreadList::Flush ()
690{
691 Mutex::Locker locker(m_threads_mutex);
692 collection::iterator pos, end = m_threads.end();
693 for (pos = m_threads.begin(); pos != end; ++pos)
694 (*pos)->Flush ();
695}
Greg Clayton56d9a1b2011-08-22 02:49:39 +0000696