nsbaci 1.0
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1
9
10#ifndef NSBACI_SERVICES_RUNTIME_THREAD_H
11#define NSBACI_SERVICES_RUNTIME_THREAD_H
12
13#include <cstdint>
14#include <queue>
15#include <vector>
16
17#include "runtimeTypes.h"
18
24
31class Thread {
32 public:
33 Thread()
34 : id(nextThreadId++),
36 priority(0),
37 pc(0),
38 bp(0),
39 sp(0) {}
40 ~Thread() = default;
41
46 nsbaci::types::ThreadID getId() const;
47
53
59
64 nsbaci::types::Priority getPriority() const;
65
70 void setPriority(nsbaci::types::Priority newPriority);
71
72 // ============== Stack Operations ==============
73
77 void push(int32_t value);
78
82 int32_t pop();
83
87 int32_t top() const;
88
89 // ============== Program Counter ==============
90
94 uint32_t getPC() const { return pc; }
95
99 void setPC(uint32_t addr) { pc = addr; }
100
104 void advancePC() { ++pc; }
105
106 // ============== Base/Stack Pointers ==============
107
108 uint32_t getBP() const { return bp; }
109 void setBP(uint32_t addr) { bp = addr; }
110
111 uint32_t getSP() const { return sp; }
112 void setSP(uint32_t addr) { sp = addr; }
113
114 // ============== Call Stack ==============
115
119 void pushReturnAddress(uint32_t addr) { callStack.push_back(addr); }
120
125 uint32_t popReturnAddress() {
126 if (callStack.empty()) return 0;
127 uint32_t addr = callStack.back();
128 callStack.pop_back();
129 return addr;
130 }
131
135 bool callStackEmpty() const { return callStack.empty(); }
136
140 void pushFrame(const std::vector<int32_t>& savedLocals) {
141 frameStack.push_back(savedLocals);
142 }
143
147 std::vector<int32_t> popFrame() {
148 if (frameStack.empty()) return {};
149 auto frame = frameStack.back();
150 frameStack.pop_back();
151 return frame;
152 }
153
157 bool frameStackEmpty() const { return frameStack.empty(); }
158
159 private:
160 nsbaci::types::ThreadID id;
162 nsbaci::types::Priority priority;
163
164 // Program counter - index into instruction stream
165 uint32_t pc;
166 // Base pointer - for stack frames
167 uint32_t bp;
168 // Stack pointer
169 uint32_t sp;
170
171 // Thread-local stack
172 std::vector<int32_t> stack;
173
174 // Call stack for return addresses
175 std::vector<uint32_t> callStack;
176
177 // Frame stack for saving local variable values during recursion
178 std::vector<std::vector<int32_t>> frameStack;
179
180 static nsbaci::types::ThreadID nextThreadId;
181
182 // friend the scheduler
183};
184
185} // namespace nsbaci::services::runtime
186
191namespace nsbaci::types {
192
194using ThreadQueue = std::queue<nsbaci::services::runtime::Thread>;
195
196} // namespace nsbaci::types
197
198#endif // NSBACI_SERVICES_RUNTIME_THREAD_H
nsbaci::types::ThreadState getState() const
Gets the current state of the thread.
Definition thread.cpp:22
uint32_t getPC() const
Get the program counter.
Definition thread.h:94
void setPC(uint32_t addr)
Set the program counter.
Definition thread.h:99
nsbaci::types::Priority getPriority() const
Gets the priority of the thread.
Definition thread.cpp:26
void setPriority(nsbaci::types::Priority newPriority)
Sets the priority of the thread.
Definition thread.cpp:28
bool callStackEmpty() const
Check if call stack is empty.
Definition thread.h:135
void setState(nsbaci::types::ThreadState newState)
Sets the state of the thread.
Definition thread.cpp:24
nsbaci::types::ThreadID getId() const
Gets the thread ID.
Definition thread.cpp:20
void pushReturnAddress(uint32_t addr)
Push return address onto call stack.
Definition thread.h:119
int32_t pop()
Pop a value from the thread's stack.
Definition thread.cpp:35
void advancePC()
Increment the program counter.
Definition thread.h:104
void push(int32_t value)
Push a value onto the thread's stack.
Definition thread.cpp:30
uint32_t popReturnAddress()
Pop return address from call stack.
Definition thread.h:125
int32_t top() const
Peek at the top of the stack without removing.
Definition thread.cpp:45
void pushFrame(const std::vector< int32_t > &savedLocals)
Push a frame with saved local variable values.
Definition thread.h:140
bool frameStackEmpty() const
Check if frame stack is empty.
Definition thread.h:157
std::vector< int32_t > popFrame()
Pop a frame and get saved local variable values.
Definition thread.h:147
Runtime services namespace for nsbaci.
Type definitions namespace for nsbaci (runtime-specific).
std::queue< nsbaci::services::runtime::Thread > ThreadQueue
Queue of threads for scheduler operations.
Definition thread.h:194
ThreadState
Definition runtimeTypes.h:26
@ Ready
Thread is ready to run.
Definition runtimeTypes.h:27
Type definitions for runtime-related operations.