headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / debugger / model / Thread.cpp
blob040e429ff5d5528f81dfc793271cf439c174acf8
1 /*
2 * Copyright 2013-2016, Rene Gollent, rene@gollent.com.
3 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
4 * Distributed under the terms of the MIT License.
5 */
7 #include "model/Thread.h"
9 #include <stdio.h>
11 #include "CpuState.h"
12 #include "StackTrace.h"
13 #include "Team.h"
16 Thread::Thread(Team* team, thread_id threadID)
18 fTeam(team),
19 fID(threadID),
20 fState(THREAD_STATE_UNKNOWN),
21 fReturnValueInfos(NULL),
22 fStopRequestPending(false),
23 fStoppedReason(THREAD_STOPPED_UNKNOWN),
24 fCpuState(NULL),
25 fStackTrace(NULL)
30 Thread::~Thread()
32 if (fCpuState != NULL)
33 fCpuState->ReleaseReference();
34 if (fStackTrace != NULL)
35 fStackTrace->ReleaseReference();
37 ClearReturnValueInfos();
38 delete fReturnValueInfos;
42 status_t
43 Thread::Init()
45 fReturnValueInfos = new(std::nothrow) ReturnValueInfoList;
46 if (fReturnValueInfos == NULL)
47 return B_NO_MEMORY;
49 return B_OK;
53 bool
54 Thread::IsMainThread() const
56 return fID == fTeam->ID();
60 void
61 Thread::SetName(const BString& name)
63 fName = name;
67 void
68 Thread::SetState(uint32 state, uint32 reason, const BString& info)
70 if (state == fState && reason == fStoppedReason)
71 return;
73 fState = state;
74 fStoppedReason = reason;
75 fStoppedReasonInfo = info;
77 // unset CPU state and stack trace, if the thread isn't stopped
78 if (fState != THREAD_STATE_STOPPED) {
79 SetCpuState(NULL);
80 SetStackTrace(NULL);
81 ClearReturnValueInfos();
82 fStopRequestPending = false;
85 fTeam->NotifyThreadStateChanged(this);
89 void
90 Thread::SetCpuState(CpuState* state)
92 if (state == fCpuState)
93 return;
95 if (fCpuState != NULL)
96 fCpuState->ReleaseReference();
98 fCpuState = state;
100 if (fCpuState != NULL)
101 fCpuState->AcquireReference();
103 fTeam->NotifyThreadCpuStateChanged(this);
107 void
108 Thread::SetStackTrace(StackTrace* trace)
110 if (trace == fStackTrace)
111 return;
113 if (fStackTrace != NULL)
114 fStackTrace->ReleaseReference();
116 fStackTrace = trace;
118 if (fStackTrace != NULL)
119 fStackTrace->AcquireReference();
121 fTeam->NotifyThreadStackTraceChanged(this);
124 void
125 Thread::SetStopRequestPending()
127 fStopRequestPending = true;
131 status_t
132 Thread::AddReturnValueInfo(ReturnValueInfo* info)
134 if (!fReturnValueInfos->AddItem(info))
135 return B_NO_MEMORY;
137 info->AcquireReference();
138 return B_OK;
142 void
143 Thread::ClearReturnValueInfos()
145 for (int32 i = 0; i < fReturnValueInfos->CountItems(); i++)
146 fReturnValueInfos->ItemAt(i)->ReleaseReference();
148 fReturnValueInfos->MakeEmpty();