1 //===-- DNBThreadResumeActions.cpp ------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  Created by Greg Clayton on 03/13/2010
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "DNBThreadResumeActions.h"
15 
16 DNBThreadResumeActions::DNBThreadResumeActions()
17     : m_actions(), m_signal_handled() {}
18 
19 DNBThreadResumeActions::DNBThreadResumeActions(
20     const DNBThreadResumeAction *actions, size_t num_actions)
21     : m_actions(), m_signal_handled() {
22   if (actions && num_actions) {
23     m_actions.assign(actions, actions + num_actions);
24     m_signal_handled.assign(num_actions, false);
25   }
26 }
27 
28 DNBThreadResumeActions::DNBThreadResumeActions(nub_state_t default_action,
29                                                int signal)
30     : m_actions(), m_signal_handled() {
31   SetDefaultThreadActionIfNeeded(default_action, signal);
32 }
33 
34 void DNBThreadResumeActions::Append(const DNBThreadResumeAction &action) {
35   m_actions.push_back(action);
36   m_signal_handled.push_back(false);
37 }
38 
39 void DNBThreadResumeActions::AppendAction(nub_thread_t tid, nub_state_t state,
40                                           int signal, nub_addr_t addr) {
41   DNBThreadResumeAction action = {tid, state, signal, addr};
42   Append(action);
43 }
44 
45 const DNBThreadResumeAction *
46 DNBThreadResumeActions::GetActionForThread(nub_thread_t tid,
47                                            bool default_ok) const {
48   const size_t num_actions = m_actions.size();
49   for (size_t i = 0; i < num_actions; ++i) {
50     if (m_actions[i].tid == tid)
51       return &m_actions[i];
52   }
53   if (default_ok && tid != INVALID_NUB_THREAD)
54     return GetActionForThread(INVALID_NUB_THREAD, false);
55   return NULL;
56 }
57 
58 size_t DNBThreadResumeActions::NumActionsWithState(nub_state_t state) const {
59   size_t count = 0;
60   const size_t num_actions = m_actions.size();
61   for (size_t i = 0; i < num_actions; ++i) {
62     if (m_actions[i].state == state)
63       ++count;
64   }
65   return count;
66 }
67 
68 bool DNBThreadResumeActions::SetDefaultThreadActionIfNeeded(nub_state_t action,
69                                                             int signal) {
70   if (GetActionForThread(INVALID_NUB_THREAD, true) == NULL) {
71     // There isn't a default action so we do need to set it.
72     DNBThreadResumeAction default_action = {INVALID_NUB_THREAD, action, signal,
73                                             INVALID_NUB_ADDRESS};
74     m_actions.push_back(default_action);
75     m_signal_handled.push_back(false);
76     return true; // Return true as we did add the default action
77   }
78   return false;
79 }
80 
81 void DNBThreadResumeActions::SetSignalHandledForThread(nub_thread_t tid) const {
82   if (tid != INVALID_NUB_THREAD) {
83     const size_t num_actions = m_actions.size();
84     for (size_t i = 0; i < num_actions; ++i) {
85       if (m_actions[i].tid == tid)
86         m_signal_handled[i] = true;
87     }
88   }
89 }
90