aboutsummaryrefslogtreecommitdiff
path: root/src/sys/schedule.c
blob: 9540d3d4b8cbe5d33f23454bb702e6554f35911c (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
#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++) {
		for (unsigned long t = 0; t < TQUEUE_MAX; t++) {
			/// Ready Queue
			scheduler.ready[p].entry[t].thread = 0;
			if (t == 0)
				scheduler.ready[p].entry[t].prev = &scheduler.ready[p].entry[TQUEUE_MAX-1];
			else
				scheduler.ready[p].entry[t].prev = &scheduler.ready[p].entry[(t-1)%TQUEUE_MAX];
			scheduler.ready[p].entry[t].next = &scheduler.ready[p].entry[(t+1)%TQUEUE_MAX];
			/// Mutex Wait Queue
			scheduler.mwait[p].entry[t].thread = 0;
			if (t == 0)
				scheduler.mwait[p].entry[t].prev = &scheduler.mwait[p].entry[TQUEUE_MAX-1];
			else
				scheduler.mwait[p].entry[t].prev = &scheduler.mwait[p].entry[(t-1)%TQUEUE_MAX];
			scheduler.mwait[p].entry[t].next = &scheduler.mwait[p].entry[(t+1)%TQUEUE_MAX];
			/// Signal Wait Queue
			scheduler.swait[p].entry[t].thread = 0;
			if (t == 0)
				scheduler.swait[p].entry[t].prev = &scheduler.swait[p].entry[TQUEUE_MAX-1];
			else
				scheduler.swait[p].entry[t].prev = &scheduler.swait[p].entry[(t-1)%TQUEUE_MAX];
			scheduler.swait[p].entry[t].next = &scheduler.swait[p].entry[(t+1)%TQUEUE_MAX];
		}
		// Ready Queue
		scheduler.ready[p].read.entry  = &scheduler.ready[p].entry[0];
		scheduler.ready[p].write.entry = &scheduler.ready[p].entry[0];
		// Mutex Wait Queue
		scheduler.mwait[p].read.entry  = &scheduler.mwait[p].entry[0];
		scheduler.mwait[p].write.entry = &scheduler.mwait[p].entry[0];
		// Signal Wait Queue
		scheduler.swait[p].read.entry  = &scheduler.swait[p].entry[0];
		scheduler.swait[p].write.entry = &scheduler.swait[p].entry[0];
	}

	// 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;
		thread_table[i] = 0;
	}
}

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;
}

void add_thread(void* pc, void* arg, unsigned char priority)
{
	struct Thread* thread = get_unused_thread();
	thread_table[thread->offset] = 1;
	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;
	}
	if (priority >= PRIORITIES)
		thread->priority = PRIORITIES - 1;
	else
		thread->priority = priority;
	thread->old_priority = -1;
	thread->preempt = 0;
	// Get the Ready Queue
	struct ThreadQueue* ready_queue = &scheduler.ready[thread->priority];
	// Get the write pointer
	struct ThreadEntryIterator* ready_write = &ready_queue->write;
	// TODO: Check if it is possible to add
	//if (ready_write->queue->thread == 0) {
		// Add the thread to the write pointer
		ready_write->entry->thread = thread;
		// Move the write pointer to the next entry
		ready_write->entry = ready_write->entry->next;
	//}
	// Schedule if this was called in usermode
	unsigned long mode = getmode() & 0x1F;
	if (mode == 0x10) {
		sys0(SYS_YIELD_HIGH);
	}
}

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 ThreadEntryIterator iter;

		struct ThreadQueue* rq = &scheduler.ready[p];
		uart_string("Ready Queue\n");
		iter.entry = rq->read.entry;
		while (iter.entry != rq->write.entry) {
			uart_hex((unsigned long)iter.entry->thread);
			uart_char(' ');
			kmemshow32((void*)iter.entry->thread, 9);
			iter.entry = iter.entry->next;
		}

		struct ThreadQueue* mq = &scheduler.mwait[p];
		uart_string("Mutex Wait Queue\n");
		iter.entry = mq->read.entry;
		while (iter.entry != mq->write.entry) {
			uart_hex((unsigned long)iter.entry->thread);
			uart_char(' ');
			kmemshow32((void*)iter.entry->thread, 9);
			iter.entry = iter.entry->next;
		}

		struct ThreadQueue* sq = &scheduler.swait[p];
		uart_string("Signal Wait Queue\n");
		iter.entry = sq->read.entry;
		while (iter.entry != sq->write.entry) {
			uart_hex((unsigned long)iter.entry->thread);
			uart_char(' ');
			kmemshow32((void*)iter.entry->thread, 9);
			iter.entry = iter.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->read.entry == rq->write.entry)
			continue;
		return rq->read.entry->thread;
	}
	// No thread found, use basic usrloopthread while waiting for new thread
	return &usrloopthread;
}

void c_cleanup(void)
{
	struct Thread* rt = scheduler.rthread;
	struct ThreadEntryIterator* read = &scheduler.ready[rt->priority].read;
	// Clear the thread pointer
	read->entry->thread = 0;
	// Move read pointer forward
	read->entry = read->entry->next;
	// 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];
	trq->read.entry->thread = 0;
	trq->read.entry = trq->read.entry->next;
	trq->write.entry->thread = rthread;
	trq->write.entry = trq->write.entry->next;
}

void sched_mutex_yield(void* m)
{
	struct Thread* rthread = scheduler.rthread;
	// 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->read.entry = trq->read.entry->next;
	// Add thread to waiting queue
	tmq->write.entry->thread = rthread;
	tmq->write.entry = tmq->write.entry->next;
	// Find the thread with the mutex
	struct ThreadEntryIterator iter;
	// Search through each priority
	for (unsigned long p = 0; p < PRIORITIES; p++) {
		struct ThreadQueue* rq = &scheduler.ready[p];
		iter = rq->read;
		while (iter.entry != rq->write.entry) {
			// Check if it is the Mutex's thread
			if (iter.entry->thread->pid == ((struct Mutex*)m)->pid) {
				// Promote the thread's priority
				if (iter.entry->thread->priority > priority) {
					// Add it to the higher priority queue
					scheduler.ready[priority].write.entry->thread = iter.entry->thread;
					// Move the Write Iterator Forward
					scheduler.ready[priority].write.entry = scheduler.ready[priority].write.entry->next;
					// Set the old priority if not set
					if (iter.entry->thread->old_priority == 0xFF)
						iter.entry->thread->old_priority = p;

					// Set the new priority
					iter.entry->thread->priority = priority;
					
					// Prune the old priority entry
					struct ThreadEntry* prev = iter.entry->prev;
					struct ThreadEntry* next = iter.entry->next;
					prev->next = next;
					next->prev = prev;
					// Put the newly freed entry at end of write queue
					//iter.entry->prev = rq->write.entry;
					//iter.entry->next = rq->write.entry->next;
					if (iter.entry == rq->read.entry)
						rq->read.entry = rq->read.entry->next;
					rq->write.entry->next->prev = iter.entry;
					rq->write.entry->next = iter.entry;
					iter.entry->thread = 0;
				}
				return;
			}
			iter.entry = iter.entry->next;
		}
	}
}

void sched_mutex_resurrect(void* m)
{
	// Look through each priority
	for (int p = 0; p < PRIORITIES; p++) {
		struct ThreadQueue* tmq = &scheduler.mwait[p];
		struct ThreadEntryIterator iter;
		iter = tmq->read;
		// Look through the lock wait queue
		while (iter.entry != tmq->write.entry) {
			// Check if the thread is waiting for the released mutex
			if (iter.entry->thread->mptr == m) {
				// Ressurect the thread
				iter.entry->thread->mptr = 0;
				scheduler.ready[iter.entry->thread->priority].write.entry->thread = iter.entry->thread;
				scheduler.ready[iter.entry->thread->priority].write.entry = scheduler.ready[iter.entry->thread->priority].write.entry->next;
				// Move the read pointer ahead
				if (iter.entry == tmq->read.entry)
					tmq->read.entry = tmq->read.entry->next;
				iter.entry->prev->next = iter.entry->next;
				iter.entry->next->prev = iter.entry->prev;
				tmq->write.entry->next->prev = iter.entry;
				tmq->write.entry->next = iter.entry;
				struct Thread* rthread = scheduler.rthread;
				// Move the current thread to its old priority if it was promoted earlier
				if (rthread->old_priority != 0xFF) {
					struct ThreadQueue* cur_trq = &scheduler.ready[rthread->priority];
					struct ThreadQueue* old_trq = &scheduler.ready[rthread->old_priority];
					// Prune from the current ready queue
					cur_trq->read.entry->thread = 0;
					cur_trq->read.entry = cur_trq->read.entry->next;
					// Prepend to original ready queue
					old_trq->read.entry = old_trq->read.entry->prev;
					old_trq->read.entry->thread = rthread;
					// Reset Priority
					rthread->priority = rthread->old_priority;
					rthread->old_priority = -1;
				}
				return;
			}
			iter.entry = iter.entry->next;
		}
	}
}

// TODO: Check offsets