blob: 4366b1a1f12b16434d9b9de095be7a44e3f18f98 (
plain)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
#include <cpu.h>
#include <globals.h>
#include <graphics/lfb.h>
#include <drivers/uart.h>
#include <lib/kmem.h>
#include <sys/schedule.h>
#include <util/mutex.h>
extern void kernel_usr_task_loop(void);
void init_scheduler(void)
{
// Set rthread to usrloopthread - an infinitely running thread so that the pointer will never be null
usrloopthread.pc = (void*)kernel_usr_task_loop;
usrloopthread.sp = (void*)0x5FC8;
*(unsigned long**)usrloopthread.sp = (unsigned long*)kernel_usr_task_loop;
usrloopthread.sp_base = -1;
usrloopthread.mptr = 0;
usrloopthread.pid = -1;
usrloopthread.priority = -1;
usrloopthread.old_priority = -1;
usrloopthread.status = THREAD_READY;
usrloopthread.offset = -1;
scheduler.rthread = &usrloopthread;
// Initialize Scheduling Queues
for (unsigned long p = 0; p < PRIORITIES; p++) {
// Ready Init
scheduler.ready[p].start.thread = 0;
scheduler.ready[p].start.next = &scheduler.ready[p].end;
scheduler.ready[p].start.entry_type = START_ENTRY;
scheduler.ready[p].end.thread = 0;
scheduler.ready[p].end.next = &scheduler.ready[p].start;
scheduler.ready[p].end.entry_type = END_ENTRY;
// Mutex Wait Init
scheduler.mwait[p].start.thread = 0;
scheduler.mwait[p].start.next = &scheduler.mwait[p].end;
scheduler.mwait[p].start.entry_type = START_ENTRY;
scheduler.mwait[p].end.thread = 0;
scheduler.mwait[p].end.next = &scheduler.mwait[p].start;
scheduler.mwait[p].end.entry_type = END_ENTRY;
// Signal Wait Init
scheduler.swait[p].start.thread = 0;
scheduler.swait[p].start.next = &scheduler.swait[p].end;
scheduler.swait[p].start.entry_type = START_ENTRY;
scheduler.swait[p].end.thread = 0;
scheduler.swait[p].end.next = &scheduler.swait[p].start;
scheduler.swait[p].end.entry_type = END_ENTRY;
}
// Initialize nextpid
nextpid = FIRST_AVAIL_PID;
// Initialize Threads - Stack Base and Offsets
for (unsigned long i = 0; i < MAX_THREADS; i++) {
struct Thread* t = &threads[i];
t->offset = i;
t->sp_base = 0x20000000 - STACK_SIZE*i;
// Clear the stack use
thread_table[i] = 0;
struct ThreadEntry* te = &thread_entries[i];
te->thread = t;
// Initialize To No Next Entry Initially
te->next = 0;
te->entry_type = THREAD_ENTRY;
}
}
struct Thread* get_unused_thread(void)
{
for(unsigned long i = 0; i < MAX_THREADS; i++) {
if (thread_table[i] == 0)
return &threads[i];
}
return 0;
}
unsigned char add_thread(void* pc, void* arg, unsigned char priority)
{
struct Thread* thread = get_unused_thread();
// The only point-of-failure is not having a thread available
if (thread == 0)
return 1;
/// Mark the Stack Space as In-Use
thread_table[thread->offset] = 1;
/// Thread Setup
thread->pc = pc;
unsigned long* argp = (void*)thread->sp_base;
argp -= 13;
*argp = (unsigned long)arg; // Set r0 to the argument
argp -= 1;
*(unsigned long**)argp = (unsigned long*)cleanup; // Set lr to the cleanup function
thread->sp = argp;
thread->status = THREAD_READY;
thread->mptr = (void*)0;
thread->pid = nextpid++;
// Reset next pid on overflow
if (nextpid < FIRST_AVAIL_PID) {
nextpid = FIRST_AVAIL_PID;
}
// Cap Priority Level
if (priority >= PRIORITIES)
thread->priority = PRIORITIES - 1;
else
thread->priority = priority;
// This thread is new
thread->old_priority = -1;
// Reserved for non-preemptible tasking
thread->preempt = 0;
/// Add Thread to Scheduler
// Get the Ready Queue
struct ThreadQueue* ready_queue = &scheduler.ready[thread->priority];
// Get thread's entry
struct ThreadEntry* new_t_entry = &thread_entries[thread->offset];
// Append to the end of the thread
ready_queue->end.next->next = new_t_entry;
ready_queue->end.next = new_t_entry;
// Link thread's next entry to end of queue
new_t_entry->next = &ready_queue->end;
// Schedule if this was called in usermode
unsigned long mode = getmode() & 0x1F;
if (mode == 0x10) {
sys0(SYS_YIELD_HIGH);
}
return 0;
}
void uart_scheduler(void)
{
uart_string("Scheduler Info\n==============\nCurrent\n");
uart_hex((unsigned long)scheduler.rthread);
uart_char(' ');
kmemshow32((void*)scheduler.rthread, 9);
for(int p = 0; p < PRIORITIES; p++) {
uart_string("Priority ");
uart_10(p);
uart_char('\n');
struct ThreadQueue* queue;
struct ThreadEntry* entry;
queue = &scheduler.ready[p];
uart_string("Ready Queue\n");
entry = queue->start.next;
while (entry->entry_type != END_ENTRY) {
uart_hex((unsigned long)entry->thread);
uart_char(' ');
kmemshow32((void*)entry->thread, 9);
entry = entry->next;
}
queue = &scheduler.mwait[p];
uart_string("Mutex Wait Queue\n");
entry = queue->start.next;
while (entry->entry_type != END_ENTRY) {
uart_hex((unsigned long)entry->thread);
uart_char(' ');
kmemshow32((void*)entry->thread, 9);
entry = entry->next;
}
queue = &scheduler.swait[p];
uart_string("Signal Wait Queue\n");
entry = queue->start.next;
while (entry->entry_type != END_ENTRY) {
uart_hex((unsigned long)entry->thread);
uart_char(' ');
kmemshow32((void*)entry->thread, 9);
entry = entry->next;
}
}
uart_string("==============\n");
}
struct Thread* next_thread(void)
{
// Recurse through all priorities to try to find a ready thread
for (int p = 0; p < PRIORITIES; p++) {
struct ThreadQueue* rq = &scheduler.ready[p];
if (rq->start.next->entry_type == END_ENTRY)
continue;
return rq->start.next->thread;
}
// No thread found, use basic usrloopthread while waiting for new thread
return &usrloopthread;
}
void c_cleanup(void)
{
struct Thread* rt = scheduler.rthread;
struct ThreadEntry* rte = &thread_entries[rt->offset];
struct ThreadQueue* queue = &scheduler.ready[rt->priority];
// Move head forward
queue->start.next = rte->next;
if (rte->next->entry_type == END_ENTRY)
rte->next->next = &queue->start;
// Clear thread entry's next
rte->next = 0;
// Mark Thread Unused
thread_table[rt->offset] = 0;
}
void yield(void)
{
struct Thread* rthread = scheduler.rthread;
// usrloopthread should not be yielded
if (rthread == &usrloopthread)
return;
// Put current thread at the end of its ready queue,
// thus any threads of the same priority can be run first
unsigned char priority = rthread->priority;
struct ThreadQueue* trq = &scheduler.ready[priority];
struct ThreadEntry* te = &thread_entries[rthread->offset];
// Move head forward
trq->start.next = te->next;
if (te->next->entry_type == END_ENTRY)
te->next->next = &trq->start;
// Add to tail
trq->end.next->next = te;
trq->end.next = te;
te->next = &trq->end;
}
// TODO: Figure out why two things are appearing in the wait queue by the end of this
void sched_mutex_yield(void* m)
{
struct Thread* rthread = scheduler.rthread;
struct ThreadEntry* rthread_e = &thread_entries[rthread->offset];
// usrloopthread should not be yielded
if (rthread == &usrloopthread)
return;
unsigned char priority = rthread->priority;
// Signify which lock this thread is waiting for
rthread->mptr = m;
struct ThreadQueue* trq = &scheduler.ready[priority];
struct ThreadQueue* tmq = &scheduler.mwait[priority];
// Move to next thread in the current thread priority's ready queue
trq->start.next = trq->start.next->next;
if (trq->start.next->next->entry_type == END_ENTRY)
trq->end.next = &trq->start;
// Add thread to waiting queue
tmq->end.next->next = rthread_e;
tmq->end.next = rthread_e;
rthread_e->next = &tmq->end;
/// Find the thread with the mutex
// Search through each priority
for (unsigned long p = 0; p < PRIORITIES; p++) {
struct ThreadQueue* queue = &scheduler.ready[p];
// Keep track of the previous entry
struct ThreadEntry* prev = &queue->start;
struct ThreadEntry* entry = prev->next;
while (entry->entry_type != END_ENTRY) {
// Check if it is the Mutex's thread
if (entry->thread->pid == ((struct Mutex*)m)->pid) {
uart_scheduler();
// Promote the thread's priority
if (entry->thread->priority > priority) {
struct ThreadQueue* new_queue = &scheduler.ready[priority];
// Add it to the higher priority queue
new_queue->end.next->next = entry;
new_queue->end.next = entry;
// Set the old priority if not set
if (entry->thread->old_priority == 0xFF)
entry->thread->old_priority = p;
// Set the new priority
entry->thread->priority = priority;
// Remove it from the lower priority queue
prev->next = entry->next;
if (entry->next->entry_type == END_ENTRY) {
entry->next->next = prev;
}
// Set the entry's next entry
entry->next = &new_queue->end;
}
return;
}
// Continue forward
prev = entry;
entry = entry->next;
}
}
}
void sched_mutex_resurrect(void* m)
{
// Look through each priority
for (int p = 0; p < PRIORITIES; p++) {
struct ThreadQueue* wait_queue = &scheduler.mwait[p];
struct ThreadEntry* prev = &wait_queue->start;
struct ThreadEntry* entry = prev->next;
// Look through the lock wait queue
while (entry->entry_type != END_ENTRY) {
// Check if the thread is waiting for the release mutex
if (entry->thread->mptr == m) {
// Resurrect the thread
entry->thread->mptr = 0;
struct ThreadEntry* end = &scheduler.ready[entry->thread->priority].end;
// Remove from old list
prev->next = entry->next;
if (entry->next->entry_type == END_ENTRY) {
prev->next->next = prev;
}
// Add to new list
end->next->next = entry;
end->next = entry;
entry->next = end;
struct Thread* rthread = scheduler.rthread;
struct ThreadEntry* rthread_e = &thread_entries[rthread->offset];
// Move current thread to old thread if applicable
if (rthread->old_priority != 0xFF) {
struct ThreadQueue* cqueue = &scheduler.ready[rthread->priority];
struct ThreadQueue* oqueue = &scheduler.ready[rthread->old_priority];
// Move current queue up
cqueue->start.next = cqueue->start.next->next;
if (cqueue->start.next->next->entry_type == END_ENTRY)
cqueue->end.next = &cqueue->start;
// Add to old queue
oqueue->end.next->next = rthread_e;
oqueue->end.next = rthread_e;
rthread_e->next = &oqueue->end;
// Reset priority
rthread->priority = rthread->old_priority;
rthread->old_priority = 0xFF;
}
return;
}
prev = entry;
entry = entry->next;
}
}
}
|