/
sched.c
45 lines (39 loc) · 1.02 KB
/
sched.c
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
#include <inc/assert.h>
#include <kern/env.h>
#include <kern/pmap.h>
#include <kern/monitor.h>
// Choose a user environment to run and run it.
void
sched_yield(void)
{
// Implement simple round-robin scheduling.
// Search through 'envs' for a runnable environment,
// in circular fashion starting after the previously running env,
// and switch to the first such environment found.
// It's OK to choose the previously running env if no other env
// is runnable.
// But never choose envs[0], the idle environment,
// unless NOTHING else is runnable.
uint32_t i, n = 0;
if (curenv)
i = curenv - envs + 1;
else
i = 1;
for (; n < NENV - 1; n++) {
if (envs[i].env_status == ENV_RUNNABLE) {
env_run(&envs[i]);
return;
}
i++;
if (i == NENV)
i = 1;
}
// Run the special idle environment when nothing else is runnable.
if (envs[0].env_status == ENV_RUNNABLE)
env_run(&envs[0]);
else {
cprintf("Destroyed all environments - nothing more to do!\n");
while (1)
monitor(NULL);
}
}