1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6#include "Thread.h"
7
8#include <stdio.h>
9
10#include "CpuState.h"
11#include "StackTrace.h"
12#include "Team.h"
13
14
15Thread::Thread(Team* team, thread_id threadID)
16	:
17	fTeam(team),
18	fID(threadID),
19	fState(THREAD_STATE_UNKNOWN),
20	fExecutedSubroutine(false),
21	fSubroutineAddress(0),
22	fStoppedReason(THREAD_STOPPED_UNKNOWN),
23	fCpuState(NULL),
24	fStackTrace(NULL)
25{
26}
27
28
29Thread::~Thread()
30{
31	if (fCpuState != NULL)
32		fCpuState->ReleaseReference();
33	if (fStackTrace != NULL)
34		fStackTrace->ReleaseReference();
35}
36
37
38status_t
39Thread::Init()
40{
41	return B_OK;
42}
43
44
45bool
46Thread::IsMainThread() const
47{
48	return fID == fTeam->ID();
49}
50
51
52void
53Thread::SetName(const BString& name)
54{
55	fName = name;
56}
57
58
59void
60Thread::SetState(uint32 state, uint32 reason, const BString& info)
61{
62	if (state == fState && reason == fStoppedReason)
63		return;
64
65	fState = state;
66	fStoppedReason = reason;
67	fStoppedReasonInfo = info;
68
69	// unset CPU state and stack trace, if the thread isn't stopped
70	if (fState != THREAD_STATE_STOPPED) {
71		SetCpuState(NULL);
72		SetStackTrace(NULL);
73		fExecutedSubroutine = false;
74		fSubroutineAddress = 0;
75	}
76
77	fTeam->NotifyThreadStateChanged(this);
78}
79
80
81void
82Thread::SetCpuState(CpuState* state)
83{
84	if (state == fCpuState)
85		return;
86
87	if (fCpuState != NULL)
88		fCpuState->ReleaseReference();
89
90	fCpuState = state;
91
92	if (fCpuState != NULL)
93		fCpuState->AcquireReference();
94
95	fTeam->NotifyThreadCpuStateChanged(this);
96}
97
98
99void
100Thread::SetStackTrace(StackTrace* trace)
101{
102	if (trace == fStackTrace)
103		return;
104
105	if (fStackTrace != NULL)
106		fStackTrace->ReleaseReference();
107
108	fStackTrace = trace;
109
110	if (fStackTrace != NULL)
111		fStackTrace->AcquireReference();
112
113	fTeam->NotifyThreadStackTraceChanged(this);
114}
115
116
117void
118Thread::SetExecutedSubroutine(target_addr_t address)
119{
120	fExecutedSubroutine = true;
121	fSubroutineAddress = address;
122}
123
124