Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 13 | #include "lldb/Core/Log.h" |
| 14 | #include "lldb/Target/RegisterContext.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "lldb/Target/ThreadList.h" |
| 16 | #include "lldb/Target/Thread.h" |
| 17 | #include "lldb/Target/ThreadPlan.h" |
| 18 | #include "lldb/Target/Process.h" |
| 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | |
| 23 | ThreadList::ThreadList (Process *process) : |
| 24 | m_process (process), |
| 25 | m_stop_id (0), |
| 26 | m_threads(), |
| 27 | m_threads_mutex (Mutex::eMutexTypeRecursive), |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 28 | m_selected_tid (LLDB_INVALID_THREAD_ID) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | { |
| 30 | } |
| 31 | |
| 32 | ThreadList::ThreadList (const ThreadList &rhs) : |
| 33 | m_process (), |
| 34 | m_stop_id (), |
| 35 | m_threads (), |
| 36 | m_threads_mutex (Mutex::eMutexTypeRecursive), |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 37 | m_selected_tid () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | { |
| 39 | // Use the assignment operator since it uses the mutex |
| 40 | *this = rhs; |
| 41 | } |
| 42 | |
| 43 | const ThreadList& |
| 44 | ThreadList::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 Clayton | bef1583 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 50 | Mutex::Locker locker_lhs(m_threads_mutex); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | 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 Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 55 | m_selected_tid = rhs.m_selected_tid; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | } |
| 57 | return *this; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | ThreadList::~ThreadList() |
| 62 | { |
| 63 | } |
| 64 | |
| 65 | |
| 66 | uint32_t |
| 67 | ThreadList::GetStopID () const |
| 68 | { |
| 69 | return m_stop_id; |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | ThreadList::SetStopID (uint32_t stop_id) |
| 74 | { |
| 75 | m_stop_id = stop_id; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void |
| 80 | ThreadList::AddThread (ThreadSP &thread_sp) |
| 81 | { |
| 82 | Mutex::Locker locker(m_threads_mutex); |
| 83 | m_threads.push_back(thread_sp); |
| 84 | } |
| 85 | |
| 86 | uint32_t |
| 87 | ThreadList::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 | |
| 95 | ThreadSP |
| 96 | ThreadList::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 | |
| 108 | ThreadSP |
| 109 | ThreadList::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 | |
| 130 | ThreadSP |
| 131 | ThreadList::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 | |
| 154 | ThreadSP |
| 155 | ThreadList::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 | |
| 175 | bool |
| 176 | ThreadList::ShouldStop (Event *event_ptr) |
| 177 | { |
| 178 | Mutex::Locker locker(m_threads_mutex); |
| 179 | |
| 180 | // Running events should never stop, obviously... |
| 181 | |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 182 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 183 | |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 184 | bool should_stop = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | m_process->UpdateThreadListIfNeeded(); |
| 186 | |
| 187 | collection::iterator pos, end = m_threads.end(); |
| 188 | |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 189 | if (log) |
| 190 | log->Printf ("%s %zu threads\n", __FUNCTION__, m_threads.size()); |
| 191 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | // Run through the threads and ask whether we should stop. Don't ask |
| 193 | // suspended threads, however, it makes more sense for them to preserve their |
| 194 | // state across the times the process runs but they don't get a chance to. |
| 195 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 196 | { |
| 197 | ThreadSP thread_sp(*pos); |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 198 | |
| 199 | if (log) |
| 200 | log->Printf ("%s thread 0x%4.4x: pc = 0x%16.16llx ", __FUNCTION__, thread_sp->GetID (), thread_sp->GetRegisterContext()->GetPC()); |
| 201 | |
| 202 | if (thread_sp->GetResumeState () == eStateSuspended) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 203 | { |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 204 | if (log) |
| 205 | log->Printf("ignore: thread was suspended\n", thread_sp->GetID (), thread_sp->GetRegisterContext()->GetPC()); |
| 206 | continue; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 207 | } |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 208 | |
| 209 | if (thread_sp->ThreadStoppedForAReason() == false) |
| 210 | { |
| 211 | if (log) |
| 212 | log->Printf("ignore: no stop reason\n", thread_sp->GetID (), thread_sp->GetRegisterContext()->GetPC()); |
| 213 | continue; |
| 214 | |
| 215 | } |
| 216 | |
| 217 | const bool thread_should_stop = thread_sp->ShouldStop(event_ptr); |
| 218 | if (log) |
| 219 | log->Printf("should_stop = %i\n", thread_sp->GetID (), thread_sp->GetRegisterContext()->GetPC(), thread_should_stop); |
| 220 | if (thread_should_stop) |
| 221 | should_stop |= true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 222 | } |
Jim Ingham | 5a47e8b | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 223 | |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 224 | if (log) |
| 225 | log->Printf ("%s overall should_stop = %i\n", __FUNCTION__, should_stop); |
| 226 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 227 | if (should_stop) |
| 228 | { |
| 229 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 230 | { |
| 231 | ThreadSP thread_sp(*pos); |
| 232 | thread_sp->WillStop (); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | return should_stop; |
| 237 | } |
| 238 | |
| 239 | Vote |
| 240 | ThreadList::ShouldReportStop (Event *event_ptr) |
| 241 | { |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 242 | Mutex::Locker locker(m_threads_mutex); |
| 243 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 244 | Vote result = eVoteNoOpinion; |
| 245 | m_process->UpdateThreadListIfNeeded(); |
| 246 | collection::iterator pos, end = m_threads.end(); |
| 247 | |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 248 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); |
| 249 | |
| 250 | if (log) |
| 251 | log->Printf ("%s %zu threads\n", __FUNCTION__, m_threads.size()); |
| 252 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 253 | // Run through the threads and ask whether we should report this event. |
| 254 | // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion. |
| 255 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 256 | { |
| 257 | ThreadSP thread_sp(*pos); |
| 258 | if (thread_sp->ThreadStoppedForAReason() && (thread_sp->GetResumeState () != eStateSuspended)) |
| 259 | { |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 260 | const lldb::Vote vote = thread_sp->ShouldReportStop (event_ptr); |
| 261 | if (log) |
| 262 | log->Printf ("%s thread 0x%4.4x: pc = 0x%16.16llx vote: %s\n", |
| 263 | __FUNCTION__, |
| 264 | thread_sp->GetID (), |
| 265 | thread_sp->GetRegisterContext()->GetPC(), |
| 266 | GetVoteAsCString (vote)); |
| 267 | switch (vote) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 268 | { |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 269 | case eVoteNoOpinion: |
| 270 | continue; |
| 271 | |
| 272 | case eVoteYes: |
| 273 | result = eVoteYes; |
| 274 | break; |
| 275 | |
| 276 | case eVoteNo: |
| 277 | if (result == eVoteNoOpinion) |
| 278 | { |
| 279 | result = eVoteNo; |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | if (log) |
| 284 | log->Printf ("%s thread 0x%4.4x: pc = 0x%16.16llx voted %s, but lost out because result was %s\n", |
| 285 | __FUNCTION__, |
| 286 | thread_sp->GetID (), |
| 287 | thread_sp->GetRegisterContext()->GetPC(), |
| 288 | GetVoteAsCString (vote), |
| 289 | GetVoteAsCString (result)); |
| 290 | } |
| 291 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | } |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 295 | if (log) |
| 296 | log->Printf ("%s returning %s\n", __FUNCTION__, GetVoteAsCString (result)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 297 | return result; |
| 298 | } |
| 299 | |
| 300 | Vote |
| 301 | ThreadList::ShouldReportRun (Event *event_ptr) |
| 302 | { |
Greg Clayton | 5205f0b | 2010-09-03 17:10:42 +0000 | [diff] [blame^] | 303 | |
| 304 | Mutex::Locker locker(m_threads_mutex); |
| 305 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 306 | Vote result = eVoteNoOpinion; |
| 307 | m_process->UpdateThreadListIfNeeded(); |
| 308 | collection::iterator pos, end = m_threads.end(); |
| 309 | |
| 310 | // Run through the threads and ask whether we should report this event. |
| 311 | // The rule is NO vote wins over everything, a YES vote wins over no opinion. |
| 312 | |
| 313 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 314 | { |
| 315 | ThreadSP thread_sp(*pos); |
| 316 | if (thread_sp->GetResumeState () != eStateSuspended) |
| 317 | |
| 318 | switch (thread_sp->ShouldReportRun (event_ptr)) |
| 319 | { |
| 320 | case eVoteNoOpinion: |
| 321 | continue; |
| 322 | case eVoteYes: |
| 323 | if (result == eVoteNoOpinion) |
| 324 | result = eVoteYes; |
| 325 | break; |
| 326 | case eVoteNo: |
| 327 | result = eVoteNo; |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | return result; |
| 332 | } |
| 333 | |
| 334 | void |
| 335 | ThreadList::Clear() |
| 336 | { |
| 337 | m_stop_id = 0; |
| 338 | m_threads.clear(); |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 339 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void |
| 343 | ThreadList::RefreshStateAfterStop () |
| 344 | { |
| 345 | Mutex::Locker locker(m_threads_mutex); |
| 346 | |
| 347 | m_process->UpdateThreadListIfNeeded(); |
| 348 | |
| 349 | collection::iterator pos, end = m_threads.end(); |
| 350 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 351 | (*pos)->RefreshStateAfterStop (); |
| 352 | } |
| 353 | |
| 354 | void |
| 355 | ThreadList::DiscardThreadPlans () |
| 356 | { |
| 357 | // You don't need to update the thread list here, because only threads |
| 358 | // that you currently know about have any thread plans. |
| 359 | Mutex::Locker locker(m_threads_mutex); |
| 360 | |
| 361 | collection::iterator pos, end = m_threads.end(); |
| 362 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 363 | (*pos)->DiscardThreadPlans (true); |
| 364 | |
| 365 | } |
| 366 | |
| 367 | bool |
| 368 | ThreadList::WillResume () |
| 369 | { |
| 370 | // Run through the threads and perform their momentary actions. |
| 371 | // But we only do this for threads that are running, user suspended |
| 372 | // threads stay where they are. |
| 373 | bool success = true; |
| 374 | |
| 375 | Mutex::Locker locker(m_threads_mutex); |
| 376 | m_process->UpdateThreadListIfNeeded(); |
| 377 | |
| 378 | collection::iterator pos, end = m_threads.end(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 379 | |
Jim Ingham | a99afce | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 380 | // See if any thread wants to run stopping others. If it does, then we won't |
| 381 | // setup the other threads for resume, since they aren't going to get a chance |
| 382 | // to run. This is necessary because the SetupForResume might add "StopOthers" |
| 383 | // plans which would then get to be part of the who-gets-to-run negotiation, but |
| 384 | // they're coming in after the fact, and the threads that are already set up should |
| 385 | // take priority. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | |
Jim Ingham | a99afce | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 387 | bool wants_solo_run = false; |
| 388 | |
| 389 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 390 | { |
| 391 | if ((*pos)->GetResumeState() != eStateSuspended && |
| 392 | (*pos)->GetCurrentPlan()->StopOthers()) |
| 393 | { |
| 394 | wants_solo_run = true; |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | |
| 400 | // Give all the threads that are likely to run a last chance to set up their state before we |
| 401 | // negotiate who is actually going to get a chance to run... |
| 402 | // Don't set to resume suspended threads, and if any thread wanted to stop others, only |
| 403 | // call setup on the threads that request StopOthers... |
| 404 | |
| 405 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 406 | { |
| 407 | if ((*pos)->GetResumeState() != eStateSuspended |
| 408 | && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers())) |
| 409 | { |
| 410 | (*pos)->SetupForResume (); |
| 411 | } |
| 412 | } |
| 413 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 414 | // Now go through the threads and see if any thread wants to run just itself. |
| 415 | // if so then pick one and run it. |
Jim Ingham | a99afce | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 417 | ThreadList run_me_only_list (m_process); |
| 418 | |
| 419 | run_me_only_list.SetStopID(m_process->GetStopID()); |
| 420 | |
| 421 | ThreadSP immediate_thread_sp; |
| 422 | bool run_only_current_thread = false; |
| 423 | |
| 424 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 425 | { |
| 426 | ThreadSP thread_sp(*pos); |
| 427 | if (thread_sp->GetCurrentPlan()->IsImmediate()) |
| 428 | { |
| 429 | // We first do all the immediate plans, so if we find one, set |
| 430 | // immediate_thread_sp and break out, and we'll pick it up first thing |
| 431 | // when we're negotiating which threads get to run. |
| 432 | immediate_thread_sp = thread_sp; |
| 433 | break; |
| 434 | } |
| 435 | else if (thread_sp->GetResumeState() != eStateSuspended && |
| 436 | thread_sp->GetCurrentPlan()->StopOthers()) |
| 437 | { |
| 438 | // You can't say "stop others" and also want yourself to be suspended. |
| 439 | assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended); |
| 440 | |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 441 | if (thread_sp == GetSelectedThread()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 442 | { |
| 443 | run_only_current_thread = true; |
| 444 | run_me_only_list.Clear(); |
| 445 | run_me_only_list.AddThread (thread_sp); |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | run_me_only_list.AddThread (thread_sp); |
| 450 | } |
| 451 | |
| 452 | } |
| 453 | |
| 454 | if (immediate_thread_sp) |
| 455 | { |
| 456 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 457 | { |
| 458 | ThreadSP thread_sp(*pos); |
| 459 | if (thread_sp.get() == immediate_thread_sp.get()) |
| 460 | thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState()); |
| 461 | else |
| 462 | thread_sp->WillResume (eStateSuspended); |
| 463 | } |
| 464 | } |
| 465 | else if (run_me_only_list.GetSize (false) == 0) |
| 466 | { |
| 467 | // Everybody runs as they wish: |
| 468 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 469 | { |
| 470 | ThreadSP thread_sp(*pos); |
| 471 | thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState()); |
| 472 | } |
| 473 | } |
| 474 | else |
| 475 | { |
| 476 | ThreadSP thread_to_run; |
| 477 | |
| 478 | if (run_only_current_thread) |
| 479 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 480 | thread_to_run = GetSelectedThread(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 481 | } |
| 482 | else if (run_me_only_list.GetSize (false) == 1) |
| 483 | { |
| 484 | thread_to_run = run_me_only_list.GetThreadAtIndex (0); |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | int random_thread = (int) |
| 489 | ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0)); |
| 490 | thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread); |
| 491 | } |
| 492 | |
| 493 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 494 | { |
| 495 | ThreadSP thread_sp(*pos); |
| 496 | if (thread_sp == thread_to_run) |
| 497 | thread_sp->WillResume(thread_sp->GetCurrentPlan()->RunState()); |
| 498 | else |
| 499 | thread_sp->WillResume (eStateSuspended); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | return success; |
| 504 | } |
| 505 | |
| 506 | void |
| 507 | ThreadList::DidResume () |
| 508 | { |
| 509 | collection::iterator pos, end = m_threads.end(); |
| 510 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 511 | { |
| 512 | // Don't clear out threads that aren't going to get a chance to run, rather |
| 513 | // leave their state for the next time around. |
| 514 | ThreadSP thread_sp(*pos); |
| 515 | if (thread_sp->GetResumeState() != eStateSuspended) |
| 516 | thread_sp->DidResume (); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | ThreadSP |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 521 | ThreadList::GetSelectedThread () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 522 | { |
| 523 | Mutex::Locker locker(m_threads_mutex); |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 524 | return FindThreadByID(m_selected_tid); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | bool |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 528 | ThreadList::SetSelectedThreadByID (lldb::tid_t tid) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 529 | { |
| 530 | Mutex::Locker locker(m_threads_mutex); |
| 531 | if (FindThreadByID(tid).get()) |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 532 | m_selected_tid = tid; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 533 | else |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 534 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 535 | |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 536 | return m_selected_tid != LLDB_INVALID_THREAD_ID; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | bool |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 540 | ThreadList::SetSelectedThreadByIndexID (uint32_t index_id) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 541 | { |
| 542 | Mutex::Locker locker(m_threads_mutex); |
| 543 | ThreadSP thread_sp (FindThreadByIndexID(index_id)); |
| 544 | if (thread_sp.get()) |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 545 | m_selected_tid = thread_sp->GetID(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 546 | else |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 547 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 548 | |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 549 | return m_selected_tid != LLDB_INVALID_THREAD_ID; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 550 | } |
| 551 | |