kern

view src/sched.c @ 56:0be4615594df

finally, runqueues, blocking, waking up, idle loop etc, all seem to work fine on a single user process... Next up: try forking another one :)
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 15 Aug 2011 06:17:58 +0300
parents 88a6c4e192f9
children 437360696883
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include "sched.h"
4 #include "proc.h"
5 #include "intr.h"
6 #include "asmops.h"
7 #include "config.h"
9 #define EMPTY(q) ((q)->head == 0)
11 struct proc_list {
12 struct process *head, *tail;
13 };
15 static void idle_proc(void);
16 static void ins_back(struct proc_list *list, struct process *proc);
17 static void ins_front(struct proc_list *list, struct process *proc);
18 static void remove(struct proc_list *list, struct process *proc);
19 static int hash_addr(void *addr);
21 static struct proc_list runq;
22 static struct proc_list zombieq;
24 #define HTBL_SIZE 101
25 static struct proc_list wait_htable[HTBL_SIZE];
28 void schedule(void)
29 {
30 disable_intr();
32 if(EMPTY(&runq)) {
33 if(!get_current_proc()) {
34 /* we're already in the idle process, don't reenter it
35 * or you'll fill up the stack very quickly.
36 */
37 return;
38 }
40 idle_proc();
41 return;
42 }
44 /* if the current process exhausted its timeslice,
45 * move it to the back of the queue.
46 */
47 if(runq.head->ticks_left <= 0) {
48 if(runq.head->next) {
49 struct process *proc = runq.head;
50 remove(&runq, proc);
51 ins_back(&runq, proc);
52 }
54 /* start a new timeslice */
55 runq.head->ticks_left = TIMESLICE_TICKS;
56 }
58 /* always enter context_switch with interrupts disabled */
59 context_switch(runq.head->id);
60 }
62 void add_proc(int pid)
63 {
64 int istate;
65 struct process *proc;
67 istate = get_intr_state();
68 disable_intr();
70 proc = get_process(pid);
72 ins_back(&runq, proc);
73 proc->state = STATE_RUNNABLE;
75 set_intr_state(istate);
76 }
78 /* block the process until we get a wakeup call for address ev */
79 void wait(void *wait_addr)
80 {
81 struct process *p;
82 int hash_idx;
84 disable_intr();
86 p = get_current_proc();
87 assert(p);
89 /* remove it from the runqueue ... */
90 remove(&runq, p);
92 /* and place it in the wait hash table based on sleep_addr */
93 hash_idx = hash_addr(wait_addr);
94 ins_back(wait_htable + hash_idx, p);
96 p->state = STATE_BLOCKED;
97 p->wait_addr = wait_addr;
99 /* call the scheduler to give time to another process */
100 schedule();
101 }
103 /* wake up all the processes sleeping on this address */
104 void wakeup(void *wait_addr)
105 {
106 int hash_idx;
107 struct process *iter;
108 struct proc_list *list;
110 hash_idx = hash_addr(wait_addr);
111 list = wait_htable + hash_idx;
113 iter = list->head;
114 while(iter) {
115 if(iter->wait_addr == wait_addr) {
116 /* found one, remove it, and make it runnable */
117 struct process *p = iter;
118 iter = iter->next;
120 remove(list, p);
121 p->state = STATE_RUNNABLE;
122 ins_back(&runq, p);
123 } else {
124 iter = iter->next;
125 }
126 }
127 }
129 static void idle_proc(void)
130 {
131 /* make sure we send any pending EOIs if needed.
132 * end_of_irq will actually check if it's needed first.
133 */
134 struct intr_frame *ifrm = get_intr_frame();
135 end_of_irq(INTR_TO_IRQ(ifrm->inum));
137 set_current_pid(0);
139 /* make sure interrupts are enabled before halting */
140 while(EMPTY(&runq)) {
141 enable_intr();
142 halt_cpu();
143 disable_intr();
144 }
145 }
148 /* list operations */
149 static void ins_back(struct proc_list *list, struct process *proc)
150 {
151 if(EMPTY(list)) {
152 list->head = proc;
153 } else {
154 list->tail->next = proc;
155 }
157 proc->next = 0;
158 proc->prev = list->tail;
159 list->tail = proc;
160 }
162 static void ins_front(struct proc_list *list, struct process *proc)
163 {
164 if(EMPTY(list)) {
165 list->tail = proc;
166 } else {
167 list->head->prev = proc;
168 }
170 proc->next = list->head;
171 proc->prev = 0;
172 list->head = proc;
173 }
175 static void remove(struct proc_list *list, struct process *proc)
176 {
177 if(proc->prev) {
178 proc->prev->next = proc->next;
179 }
180 if(proc->next) {
181 proc->next->prev = proc->prev;
182 }
183 if(list->head == proc) {
184 list->head = proc->next;
185 }
186 if(list->tail == proc) {
187 list->tail = proc->prev;
188 }
189 }
191 static int hash_addr(void *addr)
192 {
193 return (uint32_t)addr % HTBL_SIZE;
194 }