forked from xcore/tool_axe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Thread.h
298 lines (253 loc) · 6.03 KB
/
Thread.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright (c) 2011-12, Richard Osborne, All rights reserved
// This software is freely distributable under a derivative of the
// University of Illinois/NCSA Open Source License posted in
// LICENSE.txt and at <http://github.xcore.com/>
#ifndef _Thread_h_
#define _Thread_h_
#include <stdint.h>
#include <bitset>
#include "Runnable.h"
#include "RunnableQueue.h"
#include "Resource.h"
#include "Register.h"
class Synchroniser;
class ExitException {
unsigned status;
public:
ExitException(unsigned s) : status(s) {}
unsigned getStatus() const { return status; }
};
extern const char *registerNames[];
inline const char *getRegisterName(unsigned RegNum) {
if (RegNum < Register::NUM_REGISTERS) {
return registerNames[RegNum];
}
return "?";
}
class EventableResourceIterator :
public std::iterator<std::forward_iterator_tag, int> {
public:
EventableResourceIterator() : p(0) {}
EventableResourceIterator(EventableResource *res) : p(res) {}
bool operator==(const EventableResourceIterator &it) { return p == it.p; }
bool operator!=(const EventableResourceIterator &it) { return p != it.p; }
const EventableResourceIterator &operator++() {
p = p->next;
return *this;
}
EventableResource *&operator*() {
return p;
}
private:
EventableResource *p;
};
class EventableResourceList {
public:
EventableResourceList() : head(0) {}
void add(EventableResource *res)
{
res->next = head;
res->prev = 0;
if (head)
head->prev = res;
head = res;
}
void remove(EventableResource *res)
{
if (res->prev) {
res->prev->next = res->next;
} else {
head = res->next;
}
if (res->next) {
res->next->prev = res->prev;
}
}
typedef EventableResourceIterator iterator;
iterator begin() { return EventableResourceIterator(head); }
iterator end() { return EventableResourceIterator(); }
private:
EventableResource *head;
};
class Core;
class RunnableQueue;
class Thread : public Runnable, public Resource {
bool ssync;
Synchroniser *sync;
/// Resources owned by the thread with events enabled.
EventableResourceList eventEnabledResources;
/// Resources owned by the thread with interrupts enabled.
EventableResourceList interruptEnabledResources;
/// Parent core.
Core *parent;
/// The scheduler.
RunnableQueue *scheduler;
public:
enum SRBit {
EEBLE = 0,
IEBLE = 1,
INENB = 2,
ININT = 3,
INK = 4,
// TODO When is this set?
SINK = 5,
WAITING = 6,
FAST = 7
};
typedef std::bitset<8> sr_t;
uint32_t regs[Register::NUM_REGISTERS];
/// The program counter. Note that the pc will not be valid if the thread is
/// executing since it is cached in the dispatch loop.
uint32_t pc;
/// The time for the thread. This approximates the XCore's 400 MHz processor
/// clock.
ticks_t time;
// Instructions executed count
long count;
sr_t sr;
/// When executing some pseduo instructions placed at the end this holds the
/// real pc.
uint32_t pendingPc;
/// The resource on which the thread is paused on.
Resource *pausedOn;
Thread();
bool hasTimeSliceExpired() const {
if (scheduler->empty())
return false;
return time > scheduler->front().wakeUpTime;
}
bool alloc(Thread &CurrentThread)
{
alloc(CurrentThread.time);
return true;
}
bool free()
{
setInUse(false);
return true;
}
void setParent(Core &p) { parent = &p; }
Core &getParent() { return *parent; }
const Core &getParent() const { return *parent; }
void finalize();
void addEventEnabledResource(EventableResource *res)
{
eventEnabledResources.add(res);
}
void removeEventEnabledResource(EventableResource *res)
{
eventEnabledResources.remove(res);
}
void addInterruptEnabledResource(EventableResource *res)
{
interruptEnabledResources.add(res);
}
void removeInterruptEnabledResource(EventableResource *res)
{
interruptEnabledResources.remove(res);
}
void alloc(ticks_t t)
{
assert(!isInUse() && "Trying to allocate in use thread");
setInUse(true);
sync = 0;
ssync = true;
time = t;
pausedOn = 0;
}
void setSync(Synchroniser &s)
{
assert(!sync && "Synchroniser set twice");
sync = &s;
}
bool inSSync() const
{
return ssync;
}
void setSSync(bool value)
{
ssync = value;
}
Synchroniser *getSync()
{
return sync;
}
uint32_t ®(unsigned RegNum)
{
return regs[RegNum];
}
sr_t::reference ieble() {
return sr[IEBLE];
}
bool ieble() const {
return sr[IEBLE];
}
sr_t::reference eeble() {
return sr[EEBLE];
}
bool eeble() const {
return sr[EEBLE];
}
sr_t::reference ink() {
return sr[INK];
}
sr_t::reference inint() {
return sr[ININT];
}
sr_t::reference inenb() {
return sr[INENB];
}
sr_t::reference waiting() {
return sr[WAITING];
}
bool waiting() const {
return sr[WAITING];
}
public:
void dump() const;
void schedule();
void takeEvent();
bool hasPendingEvent() const;
/// Enable for events on the current thread.
/// \return true if there is a pending event, false otherwise.
bool enableEvents()
{
sr_t newSR = sr;
newSR[EEBLE] = true;
return setSR(newSR);
}
/// Set the SR register.
/// \return true if there is a pending event, false otherwise.
bool setSR(sr_t value)
{
sr_t enabled = value & (sr ^ value);
sr = value;
if (!enabled[EEBLE] && !enabled[IEBLE])
return false;
return setSRSlowPath(enabled);
}
public:
void clre()
{
eeble() = false;
inenb() = false;
EventableResourceList &EER = eventEnabledResources;
for (EventableResourceList::iterator it = EER.begin(),
end = EER.end(); it != end; ++it) {
(*it)->eventDisable(*this);
}
}
bool isExecuting() const;
void run(ticks_t time);
bool setC(ticks_t time, ResourceID resID, uint32_t val);
private:
bool setSRSlowPath(sr_t old);
};
struct PendingEvent {
EventableResource *res;
bool set;
bool interrupt;
ticks_t time;
};
void initInstructionCache(Core &c);
#endif // _Thread_h_